Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
Uuid.php
1<?php
2
4
10
11class Uuid
12{
13 private const TTL = 3600;
14
16 private $tableName;
17
19 private $uuid;
20
22 private $dateCreate;
23
25 private $messageId;
26
28 private $messageLoaded = false;
29
31 protected $connection;
32
34 protected $sqlHelper;
35
39 public function __construct(string $uuid)
40 {
41 $this->uuid = $uuid;
42 $this->connection = Application::getInstance()->getConnection();
43 $this->sqlHelper = $this->connection->getSqlHelper();
44 $this->tableName = $this->sqlHelper->forSql(MessageUuidTable::getTableName());
45 }
46
47 private function setMessageId(?int $messageId): void
48 {
49 $this->messageId = $messageId;
50 }
51
52 private function setDateCreate(DateTime $dateCreate): void
53 {
54 $this->dateCreate = $dateCreate;
55 }
56
63 public function add(): bool
64 {
65 $preparedData = $this->sqlHelper->prepareInsert($this->tableName, [
66 'DATE_CREATE' => (new DateTime()),
67 'UUID' => $this->uuid,
68 ]);
69
70 $this->connection->queryExecute(
71 $this->sqlHelper->getInsertIgnore(
72 $this->sqlHelper->quote($this->tableName),
73 " ({$preparedData[0]}) ",
74 " VALUES ({$preparedData[1]})"
75 )
76 );
77 $rowsAffected = $this->connection->getAffectedRowsCount();
78
79 return $rowsAffected > 0;
80 }
81
87 public static function validate(string $uuid): bool
88 {
89 $regex = '/^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/';
90
91 return preg_match($regex, $uuid) === 1;
92 }
93
99 private function loadMessage(): void
100 {
101 $uuidRecord = MessageUuidTable::getRow([
102 'select' => ['MESSAGE_ID', 'DATE_CREATE'],
103 'filter' => [
104 '=UUID' => $this->uuid,
105 ]
106 ]);
107
108 if (is_null($uuidRecord))
109 {
110 return;
111 }
112
113 $this->messageLoaded = true;
114 $this->setMessageId($uuidRecord['MESSAGE_ID']);
115 $this->setDateCreate($uuidRecord['DATE_CREATE']);
116 }
117
123 public function getMessageId(): ?int
124 {
125 if (!$this->messageLoaded)
126 {
127 $this->loadMessage();
128 }
129
130 return $this->messageId;
131 }
132
136 private function updateDateCreate(): void
137 {
138 MessageUuidTable::update($this->uuid, [
139 'DATE_CREATE' => new DateTime()
140 ]);
141 }
142
148 public function updateMessageId(int $messageId): void
149 {
150 MessageUuidTable::update($this->uuid, [
151 'MESSAGE_ID' => $messageId
152 ]);
153 }
154
160 public function delete(): bool
161 {
162 $deleteResult = MessageUuidTable::delete($this->uuid);
163
164 return $deleteResult->isSuccess();
165 }
166
172 private function isExpired(): bool
173 {
174 if (!$this->messageLoaded)
175 {
176 $this->loadMessage();
177 }
178
179 if ($this->dateCreate instanceof DateTime)
180 {
181 $dateCreateTimestamp = $this->dateCreate->getTimestamp();
182
183 return (time() - $dateCreateTimestamp) > self::TTL;
184 }
185
186 return false;
187 }
188
194 public function updateIfExpired(): bool
195 {
196 if ($this->isExpired())
197 {
198 $this->updateDateCreate();
199
200 return true;
201 }
202
203 return false;
204 }
205
211 public static function cleanOldRecords(): string
212 {
213 $daysBeforeExpire = 31;
214 $connection = Application::getInstance()->getConnection();
215 $sqlHelper = $connection->getSqlHelper();
216
217 $tableName = $sqlHelper->forSql(MessageUuidTable::getTableName());
218 $expiredDateTime = (new DateTime())->add("-$daysBeforeExpire days");
219 $expiredDateTimePrepared = $sqlHelper->convertToDbDateTime($expiredDateTime);
220
221 $query = "DELETE FROM $tableName WHERE DATE_CREATE < $expiredDateTimePrepared;";
222 $connection->queryExecute($query);
223
224 return __METHOD__. '();';
225 }
226}
static cleanOldRecords()
Definition Uuid.php:211
__construct(string $uuid)
Definition Uuid.php:39
updateMessageId(int $messageId)
Definition Uuid.php:148
static validate(string $uuid)
Definition Uuid.php:87