Full implemantation

This commit is contained in:
2022-11-28 22:56:21 -03:00
parent 30ef4c6a35
commit c53eb4c7a6
55 changed files with 1505 additions and 1011 deletions

View File

@ -2,6 +2,7 @@
namespace ProVM\Emails\Model;
use ProVM\Common\Define\Model;
use ProVM\Common\Exception\Database\BlankResult;
class Attachment implements Model
{
@ -53,13 +54,22 @@ class Attachment implements Model
public function getStates(): array
{
if (!isset($this->states)) {
$this->setStates($this->getStateRepository()->fetchByAttachment($this->getId()));
try {
$this->setStates($this->getStateRepository()->fetchByAttachment($this->getId()));
} catch (BlankResult $e) {
return [];
}
}
return $this->states;
}
public function getState(string $name): State\Attachment
{
return $this->getStates()[$name];
try {
return $this->getStates()[$name];
} catch (\Exception $e) {
$this->newState($name);
return $this->getStates()[$name];
}
}
public function addState(State\Attachment $state): Attachment
{
@ -76,19 +86,47 @@ class Attachment implements Model
protected function newState(string $name): Attachment
{
$this->addState((new State\Attachment())
->setName($name)
->setAttachment($this)
->setName($name)
);
return $this;
}
public function getFullFilename(): string
{
return implode(' - ', [
$this->getMessage()->getSubject(),
$this->getMessage()->getDateTime()->format('Y-m-d His'),
$this->getFilename()
]);
}
public function isDownloaded(): bool
{
return $this->getState('downloaded')?->getValue() ?? false;
}
public function isEncrypted(): bool
{
return $this->getState('encrypted')->getValue() ?? false;
return $this->getState('encrypted')?->getValue() ?? false;
}
public function isDecrypted(): bool
{
return $this->getState('encrypted')->getValue() ?? false;
try {
return $this->getState('decrypted')?->getValue() ?? false;
} catch (\Exception $e) {
$this->newState('decrypted');
return $this->getState('decrypted')?->getValue() ?? false;
}
}
public function itIsDownloaded(): Attachment
{
try {
$this->getState('downloaded')->setValue(true);
} catch (\Exception $e) {
$this->newState('downloaded');
$this->getState('downloaded')->setValue(true);
}
return $this;
}
public function itIsEncrypted(): Attachment
{
@ -105,7 +143,7 @@ class Attachment implements Model
try {
$this->getState('decrypted')->setValue(true);
} catch (\Exception $e) {
$this->newState('encrypted');
$this->newState('decrypted');
$this->getState('decrypted')->setValue(true);
}
return $this;
@ -115,8 +153,17 @@ class Attachment implements Model
{
return [
'id' => $this->getId(),
'message' => $this->getMessage()->toArray(),
'message' => [
'id' => $this->getMessage()->getId(),
'mailbox' => $this->getMessage()->getMailbox()->toArray(),
'position' => $this->getMessage()->getPosition(),
'uid' => $this->getMessage()->getUID(),
'subject' => $this->getMessage()->getSubject(),
'from' => $this->getMessage()->getFrom(),
'date_time' => $this->getMessage()->getDateTime()->format('Y-m-d H:i:s')
],
'filename' => $this->getFilename(),
'downloaded' => $this->isDownloaded(),
'encrypted' => $this->isEncrypted(),
'decrypted' => $this->isDecrypted()
];

61
api/src/Model/Job.php Normal file
View File

@ -0,0 +1,61 @@
<?php
namespace ProVM\Emails\Model;
use DateTimeInterface;
use ProVM\Common\Define\Model;
class Job implements Model
{
protected int $id;
protected Message $message;
protected DateTimeInterface $dateTime;
protected bool $executed;
public function getId(): int
{
return $this->id;
}
public function getMessage(): Message
{
return $this->message;
}
public function getDateTime(): DateTimeInterface
{
return $this->dateTime;
}
public function isExecuted(): bool
{
return $this->executed ?? false;
}
public function setId(int $id): Job
{
$this->id = $id;
return $this;
}
public function setMessage(Message $message): Job
{
$this->message = $message;
return $this;
}
public function setDateTime(DateTimeInterface $dateTime): Job
{
$this->dateTime = $dateTime;
return $this;
}
public function wasExecuted(): Job
{
$this->executed = true;
return $this;
}
public function toArray(): array
{
return [
'id' => $this->getId(),
'message' => $this->getMessage()->toArray(),
'date_time' => $this->getDateTime()->format('Y-m-d H:i:s'),
'executed' => $this->isExecuted()
];
}
}

View File

@ -4,12 +4,15 @@ namespace ProVM\Emails\Model;
use DateTimeInterface;
use ProVM\Common\Define\Model;
use ProVM\Common\Exception\Database\BlankResult;
use ProVM\Common\Exception\EmptyMailbox;
use ProVM\Common\Exception\Mailbox\Stateless;
use Safe\DateTimeImmutable;
class Mailbox implements Model
{
protected int $id;
protected string $name;
protected int $validity;
public function getId(): int
{
@ -19,6 +22,10 @@ class Mailbox implements Model
{
return $this->name;
}
public function getValidity(): int
{
return $this->validity;
}
public function setId(int $id): Mailbox
{
@ -30,6 +37,11 @@ class Mailbox implements Model
$this->name = $name;
return $this;
}
public function setValidity(int $uidvalidity): Mailbox
{
$this->validity = $uidvalidity;
return $this;
}
protected \ProVM\Emails\Repository\State\Mailbox $stateRepository;
@ -68,19 +80,31 @@ class Mailbox implements Model
return $this;
}
public function lastState(): State\Mailbox
{
if (count($this->getStates()) === 0) {
throw new Stateless($this);
}
return $this->getStates()[array_key_last($this->getStates())];
}
public function lastChecked(): ?DateTimeInterface
{
if (count($this->getStates()) == 0) {
return null;
}
return $this->getStates()[array_key_last($this->getStates())]->getDateTime();
return $this->lastState()->getDateTime();
}
public function lastCount(): int
{
if (count($this->getStates()) == 0) {
return 0;
}
return $this->getStates()[array_key_last($this->getStates())]->getCount();
return $this->lastState()->getCount();
}
public function lastPosition(): int
{
$state = $this->lastState()->getUIDs();
return array_key_last($state);
}
public function toArray(): array
@ -88,6 +112,7 @@ class Mailbox implements Model
return [
'id' => $this->getId(),
'name' => $this->getName(),
//'validity' => $this->getValidity(),
'last_checked' => [
'date' => $this->lastChecked() ?? 'never',
'count' => $this->lastCount() ?? 0

View File

@ -146,7 +146,7 @@ class Message implements Model
public function doesHaveAttachments(): Message
{
if (!isset($this->getStates()['has_attachments'])) {
if (!isset($this->getState()['has_attachments'])) {
$this->newState('has_attachments');
}
$this->getState('has_attachments')->setValue(true);
@ -206,7 +206,10 @@ class Message implements Model
'has_attachments' => $this->hasAttachments(),
'valid_attachments' => $this->hasValidAttachments(),
'downloaded_attachments' => $this->hasDownloadedAttachments()
]
],
'attachments' => $this->hasValidAttachments() ? array_map(function(Attachment $attachment) {
return $attachment->toArray();
}, $this->getAttachments()) : []
];
}
}

View File

@ -24,7 +24,7 @@ class Attachment implements Model
}
public function getValue(): bool
{
return $this->value;
return $this->value ?? false;
}
public function setId(int $id): Attachment

View File

@ -2,64 +2,62 @@
namespace ProVM\Emails\Repository;
use PDO;
use ProVM\Common\Exception\Database\BlankResult;
use Psr\Log\LoggerInterface;
use ProVM\Common\Define\Model as ModelInterface;
use ProVM\Common\Factory\Model;
use ProVM\Common\Implement\Repository;
class Attachment extends Repository
{
public function __construct(PDO $connection, LoggerInterface $logger, State\Attachment $stateRepository)
public function __construct(PDO $connection, LoggerInterface $logger, Model $factory)
{
parent::__construct($connection, $logger);
$this->setStateRepository($stateRepository)
$this->setFactory($factory)
->setTable('attachments');
}
protected State\Attachment $stateRepository;
public function getStateRepository(): State\Attachment
protected Model $factory;
public function getFactory(): Model
{
return $this->stateRepository;
return $this->factory;
}
public function setStateRepository(State\Attachment $repository): Attachment
public function setFactory(Model $factory): Attachment
{
$this->stateRepository = $repository;
$this->factory = $factory;
return $this;
}
protected function idField(): string
{
return 'filename';
}
protected function fieldsForInsert(): array
{
return array_merge($this->fieldsForUpdate(), ['filename']);
return [
'message_id',
'filename'
];
}
protected function fieldsForUpdate(): array
{
return [
'message_id'
];
return $this->fieldsForInsert();
}
protected function fieldsForCreate(): array
{
return $this->fieldsForInsert();
}
protected function valuesForUpdate(ModelInterface $model): array
{
return $this->valuesForInsert($model);
}
protected function valuesForInsert(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());
return $this->fetchByMessageAndFilename($model->getMessage()->getId(), $model->getFilename());
}
protected function valuesForCreate(array $data): array
{
@ -70,22 +68,47 @@ class Attachment extends Repository
}
protected function defaultSearch(array $data): ModelInterface
{
return $this->fetchByFilename($data['filename']);
return $this->fetchByMessageAndFilename($data['message_id'], $data['filename']);
}
public function load(array $row): \ProVM\Emails\Model\Attachment
{
$model = new \ProVM\Emails\Model\Attachment();
$model
return (new \ProVM\Emails\Model\Attachment())
->setId($row['id'])
->setMessage($this->getFactory()->find(\ProVM\Emails\Model\Message::class)->fetchById($row['message_id']))
->setFilename($row['filename'])
->setStateRepository($this->getStateRepository());
return $model;
->setStateRepository($this->getFactory()->find(\ProVM\Emails\Model\State\Attachment::class));
}
public function fetchByFilename(string $filename): \ProVM\Emails\Model\Attachment
public function save(ModelInterface &$model): void
{
$query = "SELECT * FROM {$this->getTable()} WHERE filename = ?";
return $this->fetchOne($query, [$filename]);
parent::save($model);
$states_names = [
'downloaded',
'encrypted',
'decrypted'
];
foreach ($states_names as $name) {
try {
$state = $model->getState($name);
$this->getFactory()->find(\ProVM\Emails\Model\State\Attachment::class)->save($state);
} catch (BlankResult $e) {
$this->getLogger()->error($e);
$data = [
'attachment_id' => $model->getId(),
'name' => $name,
'value' => false
];
$state = $this->getFactory()->find(\ProVM\Emails\Model\State\Attachment::class)->create($data);
$this->getFactory()->find(\ProVM\Emails\Model\State\Attachment::class)->save($state);
}
}
}
public function fetchByMessageAndFilename(int $message_id, string $filename): \ProVM\Emails\Model\Attachment
{
$query = "SELECT * FROM {$this->getTable()} WHERE message_id = ? AND filename = ?";
return $this->fetchOne($query, [$message_id, $filename]);
}
public function fetchByMessage(int $message_id): array
{

108
api/src/Repository/Job.php Normal file
View File

@ -0,0 +1,108 @@
<?php
namespace ProVM\Emails\Repository;
use PDO;
use ProVM\Common\Factory\Model;
use Psr\Log\LoggerInterface;
use Safe\DateTimeImmutable;
use ProVM\Common\Define\Model as ModelInterface;
use ProVM\Common\Implement\Repository;
use ProVM\Emails\Model\Job as BaseModel;
class Job extends Repository
{
public function __construct(PDO $connection, LoggerInterface $logger, Model $factory)
{
parent::__construct($connection, $logger);
$this->setFactory($factory)
->setTable('attachments_jobs');
}
protected \ProVM\Common\Factory\Model $factory;
public function getFactory(): \ProVM\Common\Factory\Model
{
return $this->factory;
}
public function setFactory(\ProVM\Common\Factory\Model $factory): Job
{
$this->factory = $factory;
return $this;
}
protected function fieldsForUpdate(): array
{
return $this->fieldsForInsert();
}
protected function valuesForUpdate(ModelInterface $model): array
{
return $this->valuesForInsert($model);
}
protected function fieldsForInsert(): array
{
return [
'message_id',
'date_time',
'executed'
];
}
protected function valuesForInsert(ModelInterface $model): array
{
return [
$model->getMessage()->getId(),
$model->getDateTime()->format('Y-m-d H:i:s'),
$model->isExecuted() ? 1 : 0
];
}
protected function defaultFind(ModelInterface $model): ModelInterface
{
return $this->fetchByMessageAndDate($model->getMessage()->getId(), $model->getDateTime()->format('Y-m-d H:i:s'));
}
protected function fieldsForCreate(): array
{
return [
'message_id',
'date_time',
'executed'
];
}
protected function valuesForCreate(array $data): array
{
return [
$data['message_id'],
$data['date_time'],
$data['executed'] ?? 0
];
}
protected function defaultSearch(array $data): ModelInterface
{
return $this->fetchByMessageAndDate($data['message_id'], $data['date_time']);
}
public function load(array $row): ModelInterface
{
$model = (new BaseModel())
->setId($row['id'])
->setMessage($this->getFactory()->find(\ProVM\Emails\Model\Message::class)->fetchById($row['message_id']))
->setDateTime(new DateTimeImmutable($row['date_time']));
if ($row['executed'] ?? 0 === 1) {
$model->wasExecuted();
}
return $model;
}
public function fetchAllPending(): array
{
$query = "SELECT * FROM {$this->getTable()} WHERE `executed` = 0";
return $this->fetchMany($query);
}
public function fetchByMessageAndDate(int $message_id, string $date_time): \ProVM\Emails\Model\Job
{
$query = "SELECT * FROM {$this->getTable()} WHERE `message_id` = ? AND `date_time` = ?";
return $this->fetchOne($query, [$message_id, $date_time]);
}
public function fetchPendingByMessage(int $message_id): \ProVM\Emails\Model\Job
{
$query = "SELECT * FROM {$this->getTable()} WHERE `message_id` = ? AND `executed` = 0";
return $this->fetchOne($query, [$message_id]);
}
}

View File

@ -35,7 +35,8 @@ class Mailbox extends Repository
protected function fieldsForInsert(): array
{
return [
'name'
'name',
'validity'
];
}
protected function fieldsForCreate(): array
@ -45,7 +46,8 @@ class Mailbox extends Repository
protected function valuesForInsert(Model $model): array
{
return [
$model->getName()
$model->getName(),
$model->getValidity()
];
}
protected function defaultFind(Model $model): Model
@ -61,7 +63,8 @@ class Mailbox extends Repository
protected function valuesForCreate(array $data): array
{
return [
$data['name']
$data['name'],
$data['validity']
];
}
protected function defaultSearch(array $data): Model
@ -74,6 +77,7 @@ class Mailbox extends Repository
return (new \ProVM\Emails\Model\Mailbox())
->setId($row['id'])
->setName($row['name'])
->setValidity($row['validity'])
->setStateRepository($this->getStates());
}

View File

@ -1,11 +1,11 @@
<?php
namespace ProVM\Emails\Repository;
use DateTimeInterface;
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;
@ -75,9 +75,9 @@ class Message extends Repository
protected function valuesForCreate(array $data): array
{
return [
$data['uid'],
$data['mailbox_id'],
$data['position'],
$data['uid'],
$data['subject'],
$data['from'],
$data['date_time']
@ -94,21 +94,15 @@ class Message extends Repository
*/
public function load(array $row): \ProVM\Emails\Model\Message
{
$model = new \ProVM\Emails\Model\Message();
$model
return (new \ProVM\Emails\Model\Message())
->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'])
->setDateTime(new DateTimeImmutable($row['date_time']))
->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
@ -144,12 +138,26 @@ class Message extends Repository
}
public function fetchByMailbox(int $mailbox_id): array
{
$query = "SELECT * FROM {$this->getTable()} WHERE mailbox_id = ?";
$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}";
$query = "SELECT * FROM `{$this->getTable()}` WHERE `mailbox_id` = ? AND `position` BETWEEN ? AND ? LIMIT {$amount}";
return $this->fetchMany($query, [$mailbox_id, $start, $start + $amount]);
}
public function fetchValidByMailbox(int $mailbox_id): array
{
$query = "SELECT a.*
FROM `{$this->getTable()}` a
JOIN `messages_states` b ON b.`message_id` = a.`id`
WHERE a.`mailbox_id` = ? AND b.`name` = 'valid_attachments' AND b.`value` = 1";
return $this->fetchMany($query, [$mailbox_id]);
}
public function fetchByMailboxSubjectFromAndDate(int $mailbox_id, string $subject, string $from, DateTimeInterface $dateTime): \ProVM\Emails\Model\Message
{
$query = "SELECT * FROM `{$this->getTable()}`
WHERE `mailbox_id` = ? `subject` = ? AND `from` = ? AND `date_time` = ?";
return $this->fetchOne($query, [$mailbox_id, $subject, $from, $dateTime->format('Y-m-d H:i:s')]);
}
}

View File

@ -8,10 +8,22 @@ use Psr\Log\LoggerInterface;
class Attachment extends Repository
{
public function __construct(PDO $connection, LoggerInterface $logger)
public function __construct(PDO $connection, LoggerInterface $logger, \ProVM\Common\Factory\Model $factory)
{
parent::__construct($connection, $logger);
$this->setTable('attachments_states');
$this->setTable('attachments_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): Attachment
{
$this->factory = $factory;
return $this;
}
protected function fieldsForInsert(): array
@ -40,7 +52,7 @@ class Attachment extends Repository
}
protected function valuesForUpdate(Model $model): array
{
return array_merge($this->valuesForInsert($model), [$model->getId()]);
return $this->valuesForInsert($model);
}
protected function fieldsForCreate(): array
{
@ -63,6 +75,7 @@ class Attachment extends Repository
{
return (new \ProVM\Emails\Model\State\Attachment())
->setId($row['id'])
->setAttachment($this->getFactory()->find(\ProVM\Emails\Model\Attachment::class)->fetchById($row['attachment_id']))
->setName($row['name'])
->setValue($row['value'] !== 0);
}

View File

@ -8,10 +8,22 @@ use ProVM\Common\Implement\Repository;
class Message extends Repository
{
public function __construct(PDO $connection, LoggerInterface $logger)
public function __construct(PDO $connection, LoggerInterface $logger, \ProVM\Common\Factory\Model $factory)
{
parent::__construct($connection, $logger);
$this->setTable('messages_states');
$this->setTable('messages_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): Message
{
$this->factory = $factory;
return $this;
}
protected function fieldsForUpdate(): array
@ -64,6 +76,7 @@ class Message extends Repository
return (new \ProVM\Emails\Model\State\Message())
->setId($row['id'])
->setName($row['name'])
->setMessage($this->getFactory()->find(\ProVM\Emails\Model\Message::class)->fetchById($row['message_id']))
->setValue(($row['value'] ?? 0) !== 0);
}