155 lines
4.6 KiB
PHP
155 lines
4.6 KiB
PHP
|
<?php
|
||
|
namespace ProVM\Emails\Repository;
|
||
|
|
||
|
use PDO;
|
||
|
use PDOException;
|
||
|
use Exception;
|
||
|
use ProVM\Common\Define\Model;
|
||
|
use ProVM\Common\Exception\Database\BlankResult;
|
||
|
use Psr\Log\LoggerInterface;
|
||
|
use ProVM\Common\Implement\Repository;
|
||
|
use Safe\DateTimeImmutable;
|
||
|
use Safe\Exceptions\ErrorfuncException;
|
||
|
|
||
|
class Message extends Repository
|
||
|
{
|
||
|
public function __construct(PDO $connection, LoggerInterface $logger, \ProVM\Common\Factory\Model $factory)
|
||
|
{
|
||
|
parent::__construct($connection, $logger);
|
||
|
$this->setTable('messages')
|
||
|
->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): Message
|
||
|
{
|
||
|
$this->factory = $factory;
|
||
|
return $this;
|
||
|
}
|
||
|
|
||
|
protected function fieldsForUpdate(): array
|
||
|
{
|
||
|
return $this->fieldsForInsert();
|
||
|
}
|
||
|
protected function fieldsForInsert(): array
|
||
|
{
|
||
|
return [
|
||
|
'uid',
|
||
|
'mailbox_id',
|
||
|
'position',
|
||
|
'subject',
|
||
|
'from',
|
||
|
'date_time'
|
||
|
];
|
||
|
}
|
||
|
protected function fieldsForCreate(): array
|
||
|
{
|
||
|
return $this->fieldsForUpdate();
|
||
|
}
|
||
|
protected function valuesForUpdate(Model $model): array
|
||
|
{
|
||
|
return array_merge($this->valuesForInsert($model), [
|
||
|
$model->getId()
|
||
|
]);
|
||
|
}
|
||
|
protected function valuesForInsert(Model $model): array
|
||
|
{
|
||
|
return [
|
||
|
$model->getUID(),
|
||
|
$model->getMailbox()->getId(),
|
||
|
$model->getPosition(),
|
||
|
$model->getSubject(),
|
||
|
$model->getFrom(),
|
||
|
$model->getDateTime()->format('Y-m-d H:i:s')
|
||
|
];
|
||
|
}
|
||
|
protected function defaultFind(Model $model): Model
|
||
|
{
|
||
|
return $this->fetchByUID($model->getUID());
|
||
|
}
|
||
|
|
||
|
protected function valuesForCreate(array $data): array
|
||
|
{
|
||
|
return [
|
||
|
$data['uid'],
|
||
|
$data['mailbox_id'],
|
||
|
$data['position'],
|
||
|
$data['subject'],
|
||
|
$data['from'],
|
||
|
$data['date_time']
|
||
|
];
|
||
|
}
|
||
|
protected function defaultSearch(array $data): Model
|
||
|
{
|
||
|
return $this->fetchByUID($data['uid']);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @param array $row
|
||
|
* @return \ProVM\Emails\Model\Message
|
||
|
*/
|
||
|
public function load(array $row): \ProVM\Emails\Model\Message
|
||
|
{
|
||
|
$model = new \ProVM\Emails\Model\Message();
|
||
|
$model
|
||
|
->setId($row['id'])
|
||
|
->setUID($row['uid'])
|
||
|
->setMailbox($this->getFactory()->find(\ProVM\Emails\Model\Mailbox::class)->fetchById($row['mailbox_id']))
|
||
|
->setPosition($row['position'])
|
||
|
->setSubject($row['subject'])
|
||
|
->setFrom($row['from'])
|
||
|
->setFactory($this->getFactory());
|
||
|
try {
|
||
|
$model->setDateTime(new DateTimeImmutable($row['date_time']));
|
||
|
} catch (Exception | ErrorfuncException $e) {
|
||
|
$this->getLogger()->error($e);
|
||
|
}
|
||
|
return $model;
|
||
|
}
|
||
|
|
||
|
public function save(Model &$model): void
|
||
|
{
|
||
|
parent::save($model);
|
||
|
$valid_states = [
|
||
|
'has_attachments',
|
||
|
'valid_attachments',
|
||
|
'downloaded_attachments'
|
||
|
];
|
||
|
foreach ($valid_states as $state_name) {
|
||
|
try {
|
||
|
$model->getState($state_name);
|
||
|
} catch (\Exception $e) {
|
||
|
$data = [
|
||
|
'message_id' => $model->getId(),
|
||
|
'name' => $state_name
|
||
|
];
|
||
|
$state = $this->getFactory()->find(\ProVM\Emails\Model\State\Message::class)->create($data);
|
||
|
$model->addState($state);
|
||
|
}
|
||
|
}
|
||
|
foreach ($model->getStates() as $state) {
|
||
|
$state->setMessage($model);
|
||
|
$this->getFactory()->find(\ProVM\Emails\Model\State\Message::class)->save($state);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public function fetchByUID(string $uid): \ProVM\Emails\Model\Message
|
||
|
{
|
||
|
$query = "SELECT * FROM `{$this->getTable()}` WHERE `uid` = ?";
|
||
|
return $this->fetchOne($query, [$uid]);
|
||
|
}
|
||
|
public function fetchByMailbox(int $mailbox_id): array
|
||
|
{
|
||
|
$query = "SELECT * FROM {$this->getTable()} WHERE mailbox_id = ?";
|
||
|
return $this->fetchMany($query, [$mailbox_id]);
|
||
|
}
|
||
|
public function fetchByMailboxAndPosition(int $mailbox_id, int $start, int $amount): array
|
||
|
{
|
||
|
$query = "SELECT * FROM {$this->getTable()} WHERE mailbox_id = ? AND position BETWEEN ? AND ? LIMIT {$amount}";
|
||
|
return $this->fetchMany($query, [$mailbox_id, $start, $start + $amount]);
|
||
|
}
|
||
|
}
|