13 private const TTL = 3600;
28 private $messageLoaded =
false;
43 $this->sqlHelper = $this->connection->getSqlHelper();
47 private function setMessageId(?
int $messageId): void
49 $this->messageId = $messageId;
52 private function setDateCreate(
DateTime $dateCreate): void
54 $this->dateCreate = $dateCreate;
63 public function add(): bool
65 $preparedData = $this->sqlHelper->prepareInsert($this->tableName, [
67 'UUID' => $this->uuid,
70 $this->connection->queryExecute(
71 $this->sqlHelper->getInsertIgnore(
72 $this->sqlHelper->quote($this->tableName),
73 " ({$preparedData[0]}) ",
74 " VALUES ({$preparedData[1]})"
77 $rowsAffected = $this->connection->getAffectedRowsCount();
79 return $rowsAffected > 0;
87 public static function validate(
string $uuid): bool
89 $regex =
'/^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/';
91 return preg_match($regex, $uuid) === 1;
99 private function loadMessage(): void
101 $uuidRecord = MessageUuidTable::getRow([
102 'select' => [
'MESSAGE_ID',
'DATE_CREATE'],
104 '=UUID' => $this->uuid,
108 if (is_null($uuidRecord))
113 $this->messageLoaded =
true;
114 $this->setMessageId($uuidRecord[
'MESSAGE_ID']);
115 $this->setDateCreate($uuidRecord[
'DATE_CREATE']);
125 if (!$this->messageLoaded)
127 $this->loadMessage();
130 return $this->messageId;
136 private function updateDateCreate(): void
138 MessageUuidTable::update($this->uuid, [
150 MessageUuidTable::update($this->uuid, [
151 'MESSAGE_ID' => $messageId
160 public function delete(): bool
162 $deleteResult = MessageUuidTable::delete($this->uuid);
164 return $deleteResult->isSuccess();
172 private function isExpired(): bool
174 if (!$this->messageLoaded)
176 $this->loadMessage();
179 if ($this->dateCreate instanceof
DateTime)
181 $dateCreateTimestamp = $this->dateCreate->getTimestamp();
183 return (time() - $dateCreateTimestamp) > self::TTL;
196 if ($this->isExpired())
198 $this->updateDateCreate();
213 $daysBeforeExpire = 31;
218 $expiredDateTime = (
new DateTime())->
add(
"-$daysBeforeExpire days");
219 $expiredDateTimePrepared =
$sqlHelper->convertToDbDateTime($expiredDateTime);
221 $query =
"DELETE FROM $tableName WHERE DATE_CREATE < $expiredDateTimePrepared;";
224 return __METHOD__.
'();';