20 private static array $instances = [];
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;
31 private function __construct()
37 if (isset(static::$instances[$id]))
39 return static::$instances[$id];
42 $instance =
new static();
47 $loadResult = $instance->
load($id);
48 if (!$loadResult->isSuccess())
53 static::$instances[$id] = $instance;
60 $instance =
new static();
62 $loadResult = $instance->loadByUserId($userId);
63 if (!$loadResult->isSuccess())
68 self::$instances[$instance->id] = $instance;
75 $defaultPresetId = Configuration::getDefaultPresetId();
77 return static::getInstance($defaultPresetId);
82 $this->
id = Configuration::createUserPreset($userId);
83 $this->sort = Configuration::USER_PRESET_SORT;
84 $this->personalUserId = $userId;
86 $this->notify =
new Notify($this->
id);
87 $this->general =
new General($this->
id);
89 $this->general->fillDataBase();
90 $this->notify->fillDataBase($this->general->isSimpleNotifySchema(), $this->general->getSimpleNotifyScheme());
92 static::$instances[$this->id] = $this;
126 return $this->personalUserId;
131 return $this->personalUserId === $userId;
136 return $this->isExist;
155 if (in_array(self::BIND_GENERAL, $bindingConfiguration,
true))
157 $binding[
'GENERAL_GROUP_ID'] = $this->id;
159 if (in_array(self::BIND_NOTIFY, $bindingConfiguration,
true))
161 $binding[
'NOTIFY_GROUP_ID'] = $this->id;
169 $updateResult = OptionUserTable::update($userId, $binding);
171 if (!$updateResult->isSuccess())
173 return $result->addErrors($updateResult->getErrors());
176 return $result->setResult(
true);
182 'id' => $this->
getId(),
186 'general' => $this->general->toRestFormat(),
187 'notify' => $this->notify->toRestFormat(),
197 $this->notify =
new Notify($presetId);
198 $this->general =
new General($presetId);
200 $result = $this->loadFromCache($presetId);
201 if ($result->isSuccess())
208 $result = $this->loadFromDB($presetId);
209 if ($result->isSuccess())
223 private function loadFromCache(
int $id):
Result
227 $presetValue = $cache->getValue();
229 if (!empty($presetValue))
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'] ?? []);
238 return $result->setResult(
true);
248 private function loadFromDB(
int $id):
Result
252 OptionGroupTable::query()
253 ->setSelect([
'ID',
'NAME',
'SORT',
'USER_ID'])
258 $presetValue = $query->fetch();
259 if ($presetValue ===
false)
265 $this->name = $presetValue[
'NAME'];
266 $this->sort = $presetValue[
'SORT'];
267 $this->personalUserId = $presetValue[
'USER_ID'];
269 $this->saveInCache();
271 return $result->setResult(
true);
274 private function loadByUserId(
int $userId): Result
276 $result =
new Result();
277 $query = OptionGroupTable::query()
278 ->setSelect([
'ID',
'NAME',
'SORT'])
279 ->where(
'USER_ID', $userId)
282 $row = $query->fetch();
288 $this->
id = $row[
'ID'];
289 $this->sort = $row[
'SORT'];
290 $this->name = $row[
'NAME'];
291 $this->personalUserId = $userId;
293 $this->general =
new General($this->
id);
294 $this->notify =
new Notify($this->
id);
296 return $result->setResult(
true);
299 private function saveInCache(): Preset
304 'name' => $this->name,
305 'sort' => $this->sort,
306 'userId' => $this->personalUserId,
307 'notify' => $this->notify->getSettings()->getResult(),
308 'general' => $this->general->getSettings()->getResult(),