API
This commit is contained in:
95
api/src/Repository/Attachment.php
Normal file
95
api/src/Repository/Attachment.php
Normal 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]);
|
||||
}
|
||||
}
|
85
api/src/Repository/Mailbox.php
Normal file
85
api/src/Repository/Mailbox.php
Normal 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]);
|
||||
}
|
||||
}
|
155
api/src/Repository/Message.php
Normal file
155
api/src/Repository/Message.php
Normal 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]);
|
||||
}
|
||||
}
|
80
api/src/Repository/State/Attachment.php
Normal file
80
api/src/Repository/State/Attachment.php
Normal 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]);
|
||||
}
|
||||
}
|
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]);
|
||||
}
|
||||
}
|
80
api/src/Repository/State/Message.php
Normal file
80
api/src/Repository/State/Message.php
Normal 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]);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user