120 lines
3.6 KiB
PHP
120 lines
3.6 KiB
PHP
<?php
|
|
namespace ProVM\Emails\Repository\State;
|
|
|
|
use PDO;
|
|
use Psr\Log\LoggerInterface;
|
|
use Safe\DateTimeImmutable;
|
|
use ProVM\Common\Define\Model;
|
|
use ProVM\Common\Implement\Repository;
|
|
|
|
class Mailbox extends Repository
|
|
{
|
|
public function __construct(PDO $connection, LoggerInterface $logger, \ProVM\Common\Factory\Model $factory)
|
|
{
|
|
parent::__construct($connection, $logger);
|
|
$this->setTable('mailboxes_states')
|
|
->setFactory($factory);
|
|
}
|
|
|
|
protected \ProVM\Common\Factory\Model $factory;
|
|
|
|
public function getFactory(): \ProVM\Common\Factory\Model
|
|
{
|
|
return $this->factory;
|
|
}
|
|
public function setFactory(\ProVM\Common\Factory\Model $factory): Mailbox
|
|
{
|
|
$this->factory = $factory;
|
|
return $this;
|
|
}
|
|
|
|
public function install(): void
|
|
{
|
|
$query = "
|
|
CREATE TABLE {$this->getTable()} (
|
|
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
|
|
`mailbox_id` INT UNSIGNED NOT NULL,
|
|
`date_time` DATETIME NOT NULL,
|
|
`count` INT UNSIGNED NOT NULL,
|
|
`uids` TEXT NOT NULL,
|
|
PRIMARY KEY (`id`),
|
|
FOREIGN KEY `fk_mailboxes_{$this->getTable()}` (`mailbox_id`)
|
|
REFERENCES `mailboxes` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
|
|
)
|
|
";
|
|
$this->getConnection()->query($query);
|
|
}
|
|
|
|
protected function fieldsForUpdate(): array
|
|
{
|
|
return $this->fieldsForInsert();
|
|
}
|
|
protected function valuesForUpdate(Model $model): array
|
|
{
|
|
return array_merge($this->valuesForInsert($model), [$model->getId()]);
|
|
}
|
|
protected function fieldsForInsert(): array
|
|
{
|
|
return [
|
|
'mailbox_id',
|
|
'date_time',
|
|
'count',
|
|
'uids'
|
|
];
|
|
}
|
|
protected function valuesForInsert(Model $model): array
|
|
{
|
|
return [
|
|
$model->getMailbox()->getId(),
|
|
$model->getDateTime()->format('Y-m-d H:i:s'),
|
|
$model->getCount(),
|
|
serialize($model->getUIDs())
|
|
];
|
|
}
|
|
protected function defaultFind(Model $model): Model
|
|
{
|
|
return $this->fetchByMailboxAndDate($model->getMailbox()->getId(), $model->getDateTime()->format('Y-m-d H:i:s'));
|
|
}
|
|
protected function fieldsForCreate(): array
|
|
{
|
|
return $this->fieldsForInsert();
|
|
}
|
|
protected function valuesForCreate(array $data): array
|
|
{
|
|
return [
|
|
$data['mailbox_id'],
|
|
$data['date_time'],
|
|
$data['count'],
|
|
$data['uids']
|
|
];
|
|
}
|
|
protected function defaultSearch(array $data): Model
|
|
{
|
|
return $this->fetchByMailboxAndDate($data['mailbox_id'], $data['date_time']);
|
|
}
|
|
|
|
public function load(array $row): \ProVM\Emails\Model\State\Mailbox
|
|
{
|
|
return (new \ProVM\Emails\Model\State\Mailbox())
|
|
->setId($row['id'])
|
|
->setMailbox($this->getFactory()->find(\ProVM\Emails\Model\Mailbox::class)->fetchById($row['mailbox_id']))
|
|
->setDateTime(new DateTimeImmutable($row['date_time']))
|
|
->setCount($row['count'])
|
|
->setUIDs(unserialize($row['uids']));
|
|
}
|
|
|
|
public function fetchByMailbox(int $mailbox_id): array
|
|
{
|
|
$query = "SELECT * FROM `{$this->getTable()}` WHERE `mailbox_id` = ?";
|
|
return $this->fetchMany($query, [$mailbox_id]);
|
|
}
|
|
public function fetchByMailboxAndDate(int $mailbox_id, \DateTimeInterface | string $date_time): \ProVM\Emails\Model\State\Mailbox
|
|
{
|
|
if (!is_string($date_time)) {
|
|
$date_time = $date_time->format('Y-m-d H:i:s');
|
|
}
|
|
$query = "SELECT * FROM `{$this->getTable()}` WHERE `mailbox_id` = ? AND `date_time` = ?";
|
|
return $this->fetchOne($query, [$mailbox_id, $date_time]);
|
|
}
|
|
}
|