Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
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 ];
150 }
151
152 public function getCommands(): array
153 {
154 if (empty($this->commands))
155 {
156 $this->commands = (new Command($this->getId()))->toRestFormat();
157 }
158
159 return $this->commands;
160 }
161
162 private function getSavedCache(int $id): Cache
163 {
164 $cache = Application::getInstance()->getCache();
165
166 $cacheDir = self::getCacheDir($id);
167 $cache->initCache(self::CACHE_TTL, self::getCacheKey($id), $cacheDir);
168
169 return $cache;
170 }
171
172 private function getBotDataFromDb(int $id): ?array
173 {
174 $query = BotTable::query()
175 ->setSelect(['*'])
176 ->setLimit(1)
177 ->where('BOT_ID', $id)
178 ;
179
180 $result = $query->fetch();
181
182 return $result ?: null;
183 }
184
185 private function saveInCache(Cache $cache, array $userData): void
186 {
187 $cache->startDataCache();
188 $cache->endDataCache($userData);
189 }
190
191 private static function getCacheDir(int $id): string
192 {
193 $cacheSubDir = substr(md5(self::getCacheKey($id)),2,2);
194
195 return self::CACHE_PATH . "{$cacheSubDir}/" . self::getCacheKey($id) . "/";
196 }
197
198 private static function getCacheKey($id): string
199 {
200 return self::CACHE_KEY . $id;
201 }
202
203 public static function cleanCache(int $id): void
204 {
205 Application::getInstance()->getCache()->cleanDir(self::getCacheDir($id));
206 }
207}
endDataCache($vars=false)
Definition cache.php:407
startDataCache($TTL=false, $uniqueString=false, $initDir=false, $vars=array(), $baseDir='cache')
Definition cache.php:348