1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
BotData.php
См. документацию.
1<?php
2
4
10
11class BotData implements RestEntity
12{
13 private const CACHE_PATH = '/bx/im/bot/new_cache_v1/';
14 private const CACHE_TTL = 31536000;
15 private const CACHE_KEY = 'bot_data_';
16
17 private const BOT_TYPE = [
18 'TYPE_HUMAN' => 'H',
19 'TYPE_NETWORK' => 'N',
20 'TYPE_OPENLINE' => 'O',
21 'TYPE_SUPERVISOR' => 'S',
22 ];
23
24 private static array $staticCache = [];
25
26 private int $id;
27 private array $botData = [];
28 private array $commands = [];
29
30 private function __construct()
31 {
32 }
33
34 public static function getInstance(?int $id): ?self
35 {
36 if (!isset($id))
37 {
38 return null;
39 }
40
41 if (isset(self::$staticCache[$id]))
42 {
43 return self::$staticCache[$id];
44 }
45
46 self::$staticCache[$id] = new self;
47 self::$staticCache[$id]->id = $id;
48 self::$staticCache[$id]->fillBotData();
49
50 return self::$staticCache[$id];
51 }
52
53 private function fillBotData(): void
54 {
55 $botId = $this->getId();
56
57 $cache = $this->getSavedCache($botId);
58 $cachedBot = $cache->getVars();
59
60 if ($cachedBot !== false)
61 {
62 $this->botData = $cachedBot;
63
64 return;
65 }
66
67 $botData = $this->getBotDataFromDb($botId);
68 if ($botData === null)
69 {
70 $this->botData = [];
71
72 return;
73 }
74
75 $this->saveInCache($cache, $botData);
76
77 $this->botData = $botData;
78 }
79
80 public function getId(): int
81 {
82 return $this->id;
83 }
84
85 public function getCode(): string
86 {
87 return $this->botData['CODE'] ?? '';
88 }
89
90 public function getBotData(): array
91 {
92 return $this->botData;
93 }
94
95 public static function getRestEntityName(): string
96 {
97 return 'botData';
98 }
99
100 public function toRestFormat(array $option = []): array
101 {
102 if (empty($this->botData))
103 {
104 return [];
105 }
106
107 $type = 'bot';
108 $code = $this->botData['CODE'];
109
110 if ($this->botData['TYPE'] === self::BOT_TYPE['TYPE_HUMAN'])
111 {
112 $type = 'human';
113 }
114 else if ($this->botData['TYPE'] === self::BOT_TYPE['TYPE_NETWORK'])
115 {
116 $type = 'network';
117
118 if ($this->botData['CLASS'] === 'Bitrix\ImBot\Bot\Support24')
119 {
120 $type = 'support24';
121 $code = 'network_cloud';
122 }
123 else if ($this->botData['CLASS'] === 'Bitrix\ImBot\Bot\Partner24')
124 {
125 $type = 'support24';
126 $code = 'network_partner';
127 }
128 else if ($this->botData['CLASS'] === 'Bitrix\ImBot\Bot\SupportBox')
129 {
130 $type = 'support24';
131 $code = 'network_box';
132 }
133 }
134 else if ($this->botData['TYPE'] === self::BOT_TYPE['TYPE_OPENLINE'])
135 {
136 $type = 'openline';
137 }
138 else if ($this->botData['TYPE'] === self::BOT_TYPE['TYPE_SUPERVISOR'])
139 {
140 $type = 'supervisor';
141 }
142
143 return [
144 'code' => $code,
145 'type' => $type,
146 'appId' => $this->botData['APP_ID'],
147 'isHidden' => $this->botData['HIDDEN'] === 'Y',
148 'isSupportOpenline' => $this->botData['OPENLINE'] === 'Y',
149 'backgroundId' => $this->botData['BACKGROUND_ID'] ?? null,
150 ];
151 }
152
153 public function getCommands(): array
154 {
155 if (empty($this->commands))
156 {
157 $this->commands = (new Command($this->getId()))->toRestFormat();
158 }
159
160 return $this->commands;
161 }
162
163 private function getSavedCache(int $id): Cache
164 {
165 $cache = Application::getInstance()->getCache();
166
167 $cacheDir = self::getCacheDir($id);
168 $cache->initCache(self::CACHE_TTL, self::getCacheKey($id), $cacheDir);
169
170 return $cache;
171 }
172
173 private function getBotDataFromDb(int $id): ?array
174 {
175 $query = BotTable::query()
176 ->setSelect(['*'])
177 ->setLimit(1)
178 ->where('BOT_ID', $id)
179 ;
180
181 $result = $query->fetch();
182
183 return $result ?: null;
184 }
185
186 private function saveInCache(Cache $cache, array $userData): void
187 {
188 $cache->startDataCache();
189 $cache->endDataCache($userData);
190 }
191
192 private static function getCacheDir(int $id): string
193 {
194 $cacheSubDir = substr(md5(self::getCacheKey($id)),2,2);
195
196 return self::CACHE_PATH . "{$cacheSubDir}/" . self::getCacheKey($id) . "/";
197 }
198
199 private static function getCacheKey($id): string
200 {
201 return self::CACHE_KEY . $id;
202 }
203
204 public static function cleanCache(int $id): void
205 {
206 unset(self::$staticCache[$id]);
207 Application::getInstance()->getCache()->cleanDir(self::getCacheDir($id));
208 }
209}
$type
Определения options.php:106
Определения bot.php:38
static getRestEntityName()
Определения BotData.php:95
toRestFormat(array $option=[])
Определения BotData.php:100
static getInstance(?int $id)
Определения BotData.php:34
static cleanCache(int $id)
Определения BotData.php:204
static getInstance()
Определения servicelocator.php:33
endDataCache($vars=false)
Определения cache.php:404
startDataCache($TTL=false, $uniqueString=false, $initDir=false, $vars=array(), $baseDir='cache')
Определения cache.php:345
$code
Определения entity.php:53
getCacheDir()
Определения entity.php:1403
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$result
Определения get_property_values.php:14
$query
Определения get_search.php:11
$option
Определения options.php:1711