API
This commit is contained in:
102
api/src/Repository/State/Mailbox.php
Normal file
102
api/src/Repository/State/Mailbox.php
Normal file
@ -0,0 +1,102 @@
|
||||
<?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;
|
||||
}
|
||||
|
||||
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]);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user