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,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);
}