Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
Preset.php
1<?php
2
4
13
14class Preset implements RestConvertible
15{
16 public const BIND_GENERAL = 'toGeneral';
17 public const BIND_NOTIFY = 'toNotify';
18
20 private static array $instances = [];
21
22 public ?Notify $notify = null;
23 public ?General $general = null;
24
25 private ?int $id = null;
26 private ?int $sort = null;
27 private ?string $name = null;
28 private ?int $personalUserId = null;
29 private bool $isExist = false;
30
31 private function __construct()
32 {
33 }
34
35 public static function getInstance(?int $id = null): Preset
36 {
37 if (isset(static::$instances[$id]))
38 {
39 return static::$instances[$id];
40 }
41
42 $instance = new static();
43 if (!$id)
44 {
45 return $instance;
46 }
47 $loadResult = $instance->load($id);
48 if (!$loadResult->isSuccess())
49 {
50 return $instance;
51 }
52
53 static::$instances[$id] = $instance;
54
55 return $instance;
56 }
57
58 public static function getPersonal(int $userId): Preset
59 {
60 $instance = new static();
61
62 $loadResult = $instance->loadByUserId($userId);
63 if (!$loadResult->isSuccess())
64 {
65 return $instance;
66 }
67
68 self::$instances[$instance->id] = $instance;
69
70 return $instance;
71 }
72
73 public static function getDefaultPreset(): Preset
74 {
75 $defaultPresetId = Configuration::getDefaultPresetId();
76
77 return static::getInstance($defaultPresetId);
78 }
79
80 public function initPersonal(int $userId): Preset
81 {
82 $this->id = Configuration::createUserPreset($userId);
83 $this->sort = Configuration::USER_PRESET_SORT;
84 $this->personalUserId = $userId;
85
86 $this->notify = new Notify($this->id);
87 $this->general = new General($this->id);
88
89 $this->general->fillDataBase();
90 $this->notify->fillDataBase($this->general->isSimpleNotifySchema(), $this->general->getSimpleNotifyScheme());
91
92 static::$instances[$this->id] = $this;
93
94 return $this;
95 }
96
100 public function getId(): ?int
101 {
102 return $this->id;
103 }
104
108 public function getSort(): ?int
109 {
110 return $this->sort;
111 }
112
116 public function getName(): ?string
117 {
118 return $this->name;
119 }
120
124 public function getPersonalUserId(): ?int
125 {
126 return $this->personalUserId;
127 }
128
129 public function isPersonal(int $userId): bool
130 {
131 return $this->personalUserId === $userId;
132 }
133
134 public function isExist(): bool
135 {
136 return $this->isExist;
137 }
138
139 public static function getRestEntityName(): string
140 {
141 return 'preset';
142 }
143
150 public function bindToUser(int $userId, array $bindingConfiguration): Result
151 {
152 $result = new Result();
153
154 $binding = [];
155 if (in_array(self::BIND_GENERAL, $bindingConfiguration, true))
156 {
157 $binding['GENERAL_GROUP_ID'] = $this->id;
158 }
159 if (in_array(self::BIND_NOTIFY, $bindingConfiguration, true))
160 {
161 $binding['NOTIFY_GROUP_ID'] = $this->id;
162 }
163
164 if (empty($binding))
165 {
166 return $result->addError(new PresetError(PresetError::BINDING_NOT_SPECIFIED));
167 }
168
169 $updateResult = OptionUserTable::update($userId, $binding);
170
171 if (!$updateResult->isSuccess())
172 {
173 return $result->addErrors($updateResult->getErrors());
174 }
175
176 return $result->setResult(true);
177 }
178
179 public function toRestFormat(array $option = []): array
180 {
181 return [
182 'id' => $this->getId(),
183 'name' => $this->getName(),
184 'sort' => $this->getSort(),
185 'userId' => $this->getPersonalUserId(),
186 'general' => $this->general->toRestFormat(),
187 'notify' => $this->notify->toRestFormat(),
188 ];
189 }
190
195 public function load(int $presetId): Result
196 {
197 $this->notify = new Notify($presetId);
198 $this->general = new General($presetId);
199
200 $result = $this->loadFromCache($presetId);
201 if ($result->isSuccess())
202 {
203 $this->isExist = true;
204
205 return $result;
206 }
207
208 $result = $this->loadFromDB($presetId);
209 if ($result->isSuccess())
210 {
211 $this->isExist = true;
212
213 return $result;
214 }
215
216 return $result;
217 }
218
223 private function loadFromCache(int $id): Result
224 {
225 $result = new Result();
226 $cache = CacheManager::getPresetCache($id);
227 $presetValue = $cache->getValue();
228
229 if (!empty($presetValue))
230 {
231 $this->id = $id;
232 $this->name = $presetValue['name'] ?? null;
233 $this->sort = $presetValue['sort'];
234 $this->personalUserId = $presetValue['userId'] ?? null;
235 $this->notify->load($presetValue['notify'] ?? []);
236 $this->general->load($presetValue['general'] ?? []);
237
238 return $result->setResult(true);
239 }
240
241 return $result->addError(new PresetError(PresetError::NOT_FOUND));
242 }
243
248 private function loadFromDB(int $id): Result
249 {
250 $result = new Result();
251 $query =
252 OptionGroupTable::query()
253 ->setSelect(['ID', 'NAME', 'SORT', 'USER_ID'])
254 ->where('ID', $id)
255 ->setLimit(1)
256 ;
257
258 $presetValue = $query->fetch();
259 if ($presetValue === false)
260 {
261 return $result->addError(new PresetError(PresetError::NOT_FOUND));
262 }
263
264 $this->id = $id;
265 $this->name = $presetValue['NAME'];
266 $this->sort = $presetValue['SORT'];
267 $this->personalUserId = $presetValue['USER_ID'];
268
269 $this->saveInCache();
270
271 return $result->setResult(true);
272 }
273
274 private function loadByUserId(int $userId): Result
275 {
276 $result = new Result();
277 $query = OptionGroupTable::query()
278 ->setSelect(['ID', 'NAME', 'SORT'])
279 ->where('USER_ID', $userId)
280 ->setLimit(1)
281 ;
282 $row = $query->fetch();
283 if (!$row)
284 {
285 return $result->addError(new PresetError(PresetError::NOT_FOUND));
286 }
287
288 $this->id = $row['ID'];
289 $this->sort = $row['SORT'];
290 $this->name = $row['NAME'];
291 $this->personalUserId = $userId;
292 $this->isExist = true;
293 $this->general = new General($this->id);
294 $this->notify = new Notify($this->id);
295
296 return $result->setResult(true);
297 }
298
299 private function saveInCache(): Preset
300 {
301 $cache = CacheManager::getPresetCache($this->id);
302 $cache->setValue([
303 'id' => $this->id,
304 'name' => $this->name,
305 'sort' => $this->sort,
306 'userId' => $this->personalUserId,
307 'notify' => $this->notify->getSettings()->getResult(),
308 'general' => $this->general->getSettings()->getResult(),
309 ]);
310
311 return $this;
312 }
313}
static getPresetCache(?int $presetId=null)
bindToUser(int $userId, array $bindingConfiguration)
Definition Preset.php:150
static getPersonal(int $userId)
Definition Preset.php:58
static getInstance(?int $id=null)
Definition Preset.php:35