This commit is contained in:
2022-11-25 20:52:52 -03:00
parent dd0410a0fb
commit efed50cd7f
39 changed files with 2777 additions and 5 deletions

View File

@ -0,0 +1,124 @@
<?php
namespace ProVM\Emails\Model;
use ProVM\Common\Define\Model;
class Attachment implements Model
{
protected int $id;
protected Message $message;
protected string $filename;
public function getId(): int
{
return $this->id;
}
public function getMessage(): Message
{
return $this->message;
}
public function getFilename(): string
{
return $this->filename;
}
public function setId(int $id): Attachment
{
$this->id = $id;
return $this;
}
public function setMessage(Message $message): Attachment
{
$this->message = $message;
return $this;
}
public function setFilename(string $filename): Attachment
{
$this->filename = $filename;
return $this;
}
protected \ProVM\Emails\Repository\State\Attachment $stateRepository;
public function getStateRepository(): \ProVM\Emails\Repository\State\Attachment
{
return $this->stateRepository;
}
public function setStateRepository(\ProVM\Emails\Repository\State\Attachment $repository): Attachment
{
$this->stateRepository = $repository;
return $this;
}
protected array $states;
public function getStates(): array
{
if (!isset($this->states)) {
$this->setStates($this->getStateRepository()->fetchByAttachment($this->getId()));
}
return $this->states;
}
public function getState(string $name): State\Attachment
{
return $this->getStates()[$name];
}
public function addState(State\Attachment $state): Attachment
{
$this->states[$state->getName()] = $state;
return $this;
}
public function setStates(array $states): Attachment
{
foreach ($states as $state) {
$this->addState($state);
}
return $this;
}
protected function newState(string $name): Attachment
{
$this->addState((new State\Attachment())
->setName($name)
->setAttachment($this)
);
return $this;
}
public function isEncrypted(): bool
{
return $this->getState('encrypted')->getValue() ?? false;
}
public function isDecrypted(): bool
{
return $this->getState('encrypted')->getValue() ?? false;
}
public function itIsEncrypted(): Attachment
{
try {
$this->getState('encrypted')->setValue(true);
} catch (\Exception $e) {
$this->newState('encrypted');
$this->getState('encrypted')->setValue(true);
}
return $this;
}
public function itIsDecrypted(): Attachment
{
try {
$this->getState('decrypted')->setValue(true);
} catch (\Exception $e) {
$this->newState('encrypted');
$this->getState('decrypted')->setValue(true);
}
return $this;
}
public function toArray(): array
{
return [
'id' => $this->getId(),
'message' => $this->getMessage()->toArray(),
'filename' => $this->getFilename(),
'encrypted' => $this->isEncrypted(),
'decrypted' => $this->isDecrypted()
];
}
}

97
api/src/Model/Mailbox.php Normal file
View File

@ -0,0 +1,97 @@
<?php
namespace ProVM\Emails\Model;
use DateTimeInterface;
use ProVM\Common\Define\Model;
use ProVM\Common\Exception\Database\BlankResult;
use Safe\DateTimeImmutable;
class Mailbox implements Model
{
protected int $id;
protected string $name;
public function getId(): int
{
return $this->id;
}
public function getName(): string
{
return $this->name;
}
public function setId(int $id): Mailbox
{
$this->id = $id;
return $this;
}
public function setName(string $name): Mailbox
{
$this->name = $name;
return $this;
}
protected \ProVM\Emails\Repository\State\Mailbox $stateRepository;
public function getStateRepository(): \ProVM\Emails\Repository\State\Mailbox
{
return $this->stateRepository;
}
public function setStateRepository(\ProVM\Emails\Repository\State\Mailbox $repository): Mailbox
{
$this->stateRepository = $repository;
return $this;
}
protected array $states;
public function getStates(): array
{
if (!isset($this->states)) {
try {
$this->setStates($this->getStateRepository()->fetchByMailbox($this->getId()));
} catch (BlankResult $e) {
return [];
}
}
return $this->states;
}
public function addState(\ProVM\Emails\Model\State\Mailbox $state): Mailbox
{
$this->states []= $state;
return $this;
}
public function setStates(array $states): Mailbox
{
foreach ($states as $state) {
$this->addState($state);
}
return $this;
}
public function lastChecked(): ?DateTimeInterface
{
if (count($this->getStates()) == 0) {
return null;
}
return $this->getStates()[array_key_last($this->getStates())]->getDateTime();
}
public function lastCount(): int
{
if (count($this->getStates()) == 0) {
return 0;
}
return $this->getStates()[array_key_last($this->getStates())]->getCount();
}
public function toArray(): array
{
return [
'id' => $this->getId(),
'name' => $this->getName(),
'last_checked' => [
'date' => $this->lastChecked() ?? 'never',
'count' => $this->lastCount() ?? 0
]
];
}
}

212
api/src/Model/Message.php Normal file
View File

@ -0,0 +1,212 @@
<?php
namespace ProVM\Emails\Model;
use DateTimeInterface;
use ProVM\Common\Define\Model;
use ProVM\Common\Exception\Database\BlankResult;
class Message implements Model
{
protected int $id;
protected Mailbox $mailbox;
protected int $position;
protected string $uid;
protected string $subject;
protected string $from;
protected DateTimeInterface $dateTime;
public function getId(): int
{
return $this->id;
}
public function getMailbox(): Mailbox
{
return $this->mailbox;
}
public function getPosition(): int
{
return $this->position;
}
public function getUID(): string
{
return $this->uid;
}
public function getSubject(): string
{
return $this->subject;
}
public function getFrom(): string
{
return $this->from;
}
public function getDateTime(): DateTimeInterface
{
return $this->dateTime;
}
public function setId(int $id): Message
{
$this->id = $id;
return $this;
}
public function setMailbox(Mailbox $mailbox): Message
{
$this->mailbox = $mailbox;
return $this;
}
public function setPosition(int $position): Message
{
$this->position = $position;
return $this;
}
public function setUID(string $uid): Message
{
$this->uid = $uid;
return $this;
}
public function setSubject(string $subject): Message
{
$this->subject = $subject;
return $this;
}
public function setFrom(string $from): Message
{
$this->from = $from;
return $this;
}
public function setDateTime(DateTimeInterface $dateTime): Message
{
$this->dateTime = $dateTime;
return $this;
}
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 array $states;
public function getStates(): array
{
if (!isset($this->states)) {
try {
$this->setStates($this->getFactory()->find(\ProVM\Emails\Model\State\Message::class)->fetchByMessage($this->getId()));
} catch (BlankResult $e) {
return [];
}
}
return $this->states;
}
public function getState(string $name): \ProVM\Emails\Model\State\Message
{
return $this->getStates()[$name];
}
public function addState(\ProVM\Emails\Model\State\Message $state): Message
{
$this->states[$state->getName()] = $state;
return $this;
}
public function setStates(array $states): Message
{
foreach ($states as $state) {
$this->addState($state);
}
return $this;
}
protected function newState(string $name): Message
{
$this->addState((new \ProVM\Emails\Model\State\Message())
->setName($name)
->setMessage($this)
);
return $this;
}
public function hasAttachments(): bool
{
return $this->getState('has_attachments')->getValue() ?? false;
}
public function hasValidAttachments(): bool
{
return $this->getState('valid_attachments')->getValue() ?? false;
}
public function hasDownloadedAttachments(): bool
{
return $this->getState('downloaded_attachments')->getValue() ?? false;
}
public function doesHaveAttachments(): Message
{
if (!isset($this->getStates()['has_attachments'])) {
$this->newState('has_attachments');
}
$this->getState('has_attachments')->setValue(true);
return $this;
}
public function doesHaveValidAttachments(): Message
{
if (!isset($this->getStates()['valid_attachments'])) {
$this->newState('valid_attachments');
}
$this->getState('valid_attachments')->setValue(true);
return $this;
}
public function doesHaveDownloadedAttachments(): Message
{
if (!isset($this->getStates()['downloaded_attachments'])) {
$this->newState('downloaded_attachments');
}
$this->getState('downloaded_attachments')->setValue(true);
return $this;
}
protected array $attachments;
public function getAttachments(): array
{
if (!isset($this->attachments)) {
try {
$this->setAttachments($this->getFactory()->find(Attachment::class)->fetchByMessage($this->getId()));
} catch (BlankResult $e) {
return [];
}
}
return $this->attachments ?? [];
}
public function addAttachment(Attachment $attachment): Message
{
$this->attachments []= $attachment;
return $this;
}
public function setAttachments(array $attachments): Message
{
foreach ($attachments as $attachment) {
$this->addAttachment($attachment);
}
return $this;
}
public function toArray(): array
{
return [
'id' => $this->getId(),
'uid' => $this->getUID(),
'subject' => $this->getSubject(),
'from' => $this->getFrom(),
'date_time' => $this->getDateTime()->format('Y-m-d H:i:s'),
'states' => [
'has_attachments' => $this->hasAttachments(),
'valid_attachments' => $this->hasValidAttachments(),
'downloaded_attachments' => $this->hasDownloadedAttachments()
]
];
}
}

View File

@ -0,0 +1,50 @@
<?php
namespace ProVM\Emails\Model\State;
use ProVM\Common\Define\Model;
class Attachment implements Model
{
protected int $id;
protected \ProVM\Emails\Model\Attachment $attachment;
protected string $name;
protected bool $value;
public function getId(): int
{
return $this->id;
}
public function getAttachment(): \ProVM\Emails\Model\Attachment
{
return $this->attachment;
}
public function getName(): string
{
return $this->name;
}
public function getValue(): bool
{
return $this->value;
}
public function setId(int $id): Attachment
{
$this->id = $id;
return $this;
}
public function setAttachment(\ProVM\Emails\Model\Attachment $attachment): Attachment
{
$this->attachment = $attachment;
return $this;
}
public function setName(string $name): Attachment
{
$this->name = $name;
return $this;
}
public function setValue(bool $value): Attachment
{
$this->value = $value;
return $this;
}
}

View File

@ -0,0 +1,68 @@
<?php
namespace ProVM\Emails\Model\State;
use DateTimeInterface;
use ProVM\Common\Define\Model;
class Mailbox implements Model
{
protected int $id;
protected \ProVM\Emails\Model\Mailbox $mailbox;
protected DateTimeInterface $dateTime;
protected int $count;
protected array $uids;
public function getId(): int
{
return $this->id;
}
public function getMailbox(): \ProVM\Emails\Model\Mailbox
{
return $this->mailbox;
}
public function getDateTime(): DateTimeInterface
{
return $this->dateTime;
}
public function getCount(): int
{
return $this->count;
}
public function getUIDs(): array
{
return $this->uids;
}
public function setId(int $id): Mailbox
{
$this->id = $id;
return $this;
}
public function setMailbox(\ProVM\Emails\Model\Mailbox $mailbox): Mailbox
{
$this->mailbox = $mailbox;
return $this;
}
public function setDateTime(DateTimeInterface $dateTime): Mailbox
{
$this->dateTime = $dateTime;
return $this;
}
public function setCount(int $count): Mailbox
{
$this->count = $count;
return $this;
}
public function addUID(string $uid): Mailbox
{
$this->uids []= $uid;
return $this;
}
public function setUIDs(array $uids): Mailbox
{
foreach ($uids as $uid) {
$this->addUID($uid);
}
return $this;
}
}

View File

@ -0,0 +1,50 @@
<?php
namespace ProVM\Emails\Model\State;
use ProVM\Common\Define\Model;
class Message implements Model
{
protected int $id;
protected \ProVM\Emails\Model\Message $message;
protected string $name;
protected bool $value;
public function getId(): int
{
return $this->id;
}
public function getMessage(): \ProVM\Emails\Model\Message
{
return $this->message;
}
public function getName(): string
{
return $this->name;
}
public function getValue(): bool
{
return $this->value;
}
public function setId(int $id): Message
{
$this->id = $id;
return $this;
}
public function setMessage(\ProVM\Emails\Model\Message $message): Message
{
$this->message = $message;
return $this;
}
public function setName(string $name): Message
{
$this->name = $name;
return $this;
}
public function setValue(bool $value): Message
{
$this->value = $value;
return $this;
}
}

View File

@ -0,0 +1,95 @@
<?php
namespace ProVM\Emails\Repository;
use PDO;
use Psr\Log\LoggerInterface;
use ProVM\Common\Define\Model as ModelInterface;
use ProVM\Common\Implement\Repository;
class Attachment extends Repository
{
public function __construct(PDO $connection, LoggerInterface $logger, State\Attachment $stateRepository)
{
parent::__construct($connection, $logger);
$this->setStateRepository($stateRepository)
->setTable('attachments');
}
protected State\Attachment $stateRepository;
public function getStateRepository(): State\Attachment
{
return $this->stateRepository;
}
public function setStateRepository(State\Attachment $repository): Attachment
{
$this->stateRepository = $repository;
return $this;
}
protected function idField(): string
{
return 'filename';
}
protected function fieldsForInsert(): array
{
return array_merge($this->fieldsForUpdate(), ['filename']);
}
protected function fieldsForUpdate(): array
{
return [
'message_id'
];
}
protected function fieldsForCreate(): array
{
return $this->fieldsForInsert();
}
protected function valuesForUpdate(ModelInterface $model): array
{
return [
$model->getMessage()->getId(),
$model->getFilename()
];
}
protected function valuesForInsert(ModelInterface $model): array
{
return $this->valuesForUpdate($model);
}
protected function defaultFind(ModelInterface $model): ModelInterface
{
return $this->fetchByFilename($model->getFilename());
}
protected function valuesForCreate(array $data): array
{
return [
$data['message_id'],
$data['filename']
];
}
protected function defaultSearch(array $data): ModelInterface
{
return $this->fetchByFilename($data['filename']);
}
public function load(array $row): \ProVM\Emails\Model\Attachment
{
$model = new \ProVM\Emails\Model\Attachment();
$model
->setFilename($row['filename'])
->setStateRepository($this->getStateRepository());
return $model;
}
public function fetchByFilename(string $filename): \ProVM\Emails\Model\Attachment
{
$query = "SELECT * FROM {$this->getTable()} WHERE filename = ?";
return $this->fetchOne($query, [$filename]);
}
public function fetchByMessage(int $message_id): array
{
$query = "SELECT * FROM {$this->getTable()} WHERE message_id = ?";
return $this->fetchMany($query, [$message_id]);
}
}

View File

@ -0,0 +1,85 @@
<?php
namespace ProVM\Emails\Repository;
use PDO;
use ProVM\Common\Define\Model;
use Psr\Log\LoggerInterface;
use ProVM\Common\Implement\Repository;
class Mailbox extends Repository
{
public function __construct(PDO $connection, LoggerInterface $logger, State\Mailbox $states)
{
parent::__construct($connection, $logger);
$this->setStates($states)
->setTable('mailboxes');
}
protected State\Mailbox $stateRepository;
public function getStates(): State\Mailbox
{
return $this->stateRepository;
}
public function setStates(State\Mailbox $states): Mailbox
{
$this->stateRepository = $states;
return $this;
}
protected function fieldsForUpdate(): array
{
return $this->fieldsForInsert();
}
protected function fieldsForInsert(): array
{
return [
'name'
];
}
protected function fieldsForCreate(): array
{
return $this->fieldsForInsert();
}
protected function valuesForInsert(Model $model): array
{
return [
$model->getName()
];
}
protected function defaultFind(Model $model): Model
{
return $this->fetchByName($model->getName());
}
protected function valuesForUpdate(Model $model): array
{
return array_merge($this->valuesForInsert($model), [
$model->getId()
]);
}
protected function valuesForCreate(array $data): array
{
return [
$data['name']
];
}
protected function defaultSearch(array $data): Model
{
return $this->fetchByName($data['name']);
}
public function load(array $row): \ProVM\Emails\Model\Mailbox
{
return (new \ProVM\Emails\Model\Mailbox())
->setId($row['id'])
->setName($row['name'])
->setStateRepository($this->getStates());
}
public function fetchByName(string $name): \ProVM\Emails\Model\Mailbox
{
$query = "SELECT * FROM `{$this->getTable()}` WHERE `name` = ?";
return $this->fetchOne($query, [$name]);
}
}

View File

@ -0,0 +1,155 @@
<?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]);
}
}

View File

@ -0,0 +1,80 @@
<?php
namespace ProVM\Emails\Repository\State;
use PDO;
use ProVM\Common\Define\Model;
use ProVM\Common\Implement\Repository;
use Psr\Log\LoggerInterface;
class Attachment extends Repository
{
public function __construct(PDO $connection, LoggerInterface $logger)
{
parent::__construct($connection, $logger);
$this->setTable('attachments_states');
}
protected function fieldsForInsert(): array
{
return [
'attachment_id',
'name',
'value'
];
}
protected function valuesForInsert(Model $model): array
{
return [
$model->getAttachment()->getId(),
$model->getName(),
$model->getValue() ? 1 : 0
];
}
protected function fieldsForUpdate(): array
{
return $this->fieldsForInsert();
}
protected function defaultFind(Model $model): Model
{
return $this->fetchByAttachmentAndName($model->getAttachment()->getId(), $model->getName());
}
protected function valuesForUpdate(Model $model): array
{
return array_merge($this->valuesForInsert($model), [$model->getId()]);
}
protected function fieldsForCreate(): array
{
return $this->fieldsForInsert();
}
protected function valuesForCreate(array $data): array
{
return [
$data['attachment_id'],
$data['name'],
$data['value'] ? 1 : 0
];
}
protected function defaultSearch(array $data): Model
{
return $this->fetchByAttachmentAndName($data['attachment_id'], $data['name']);
}
public function load(array $row): \ProVM\Emails\Model\State\Attachment
{
return (new \ProVM\Emails\Model\State\Attachment())
->setId($row['id'])
->setName($row['name'])
->setValue($row['value'] !== 0);
}
public function fetchByAttachment(int $attachment_id): array
{
$query = "SELECT * FROM `{$this->getTable()}` WHERE `attachment_id` = ?";
return $this->fetchMany($query, [$attachment_id]);
}
public function fetchByAttachmentAndName(int $attachment_id, string $name): \ProVM\Emails\Model\State\Attachment
{
$query = "SELECT * FROM `{$this->getTable()}` WHERE `attachment_id` = ? AND `name` = ?";
return $this->fetchOne($query, [$attachment_id, $name]);
}
}

View 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]);
}
}

View File

@ -0,0 +1,80 @@
<?php
namespace ProVM\Emails\Repository\State;
use PDO;
use ProVM\Common\Define\Model;
use Psr\Log\LoggerInterface;
use ProVM\Common\Implement\Repository;
class Message extends Repository
{
public function __construct(PDO $connection, LoggerInterface $logger)
{
parent::__construct($connection, $logger);
$this->setTable('messages_states');
}
protected function fieldsForUpdate(): array
{
return $this->fieldsForInsert();
}
protected function valuesForUpdate(Model $model): array
{
return array_merge($this->valuesForInsert($model), [$model->getId()]);
}
public function fieldsForInsert(): array
{
return [
'message_id',
'name',
'value'
];
}
protected function valuesForInsert(Model $model): array
{
return [
$model->getMessage()->getId(),
$model->getName(),
$model->getValue() ? 1 : 0
];
}
protected function defaultFind(Model $model): Model
{
return $this->fetchByMessageAndName($model->getMessage()->getId(), $model->getName());
}
protected function fieldsForCreate(): array
{
return $this->fieldsForInsert();
}
protected function valuesForCreate(array $data): array
{
return [
$data['message_id'],
$data['name'],
$data['value'] ? 1 : 0
];
}
protected function defaultSearch(array $data): Model
{
return $this->fetchByMessageAndName($data['message_id'], $data['name']);
}
public function load(array $row): \ProVM\Emails\Model\State\Message
{
return (new \ProVM\Emails\Model\State\Message())
->setId($row['id'])
->setName($row['name'])
->setValue(($row['value'] ?? 0) !== 0);
}
public function fetchByMessage(int $message_id): array
{
$query = "SELECT * FROM `{$this->getTable()}` WHERE `message_id` = ?";
return $this->fetchMany($query, [$message_id]);
}
public function fetchByMessageAndName(int $message_id, string $name): \ProVM\Emails\Model\State\Message
{
$query = "SELECT * FROM `{$this->getTable()}` WHERE `message_id` = ? AND `name` = ?";
return $this->fetchOne($query, [$message_id, $name]);
}
}