Various updates

This commit is contained in:
2023-06-08 20:49:27 -04:00
parent 3ed5acf75e
commit 9307ba330c
45 changed files with 864 additions and 188 deletions

View File

@ -169,4 +169,8 @@ class Attachment implements Model
'decrypted' => $this->isDecrypted()
];
}
}
public function jsonSerialize(): mixed
{
return $this->toArray();
}
}

View File

@ -58,4 +58,8 @@ class Job implements Model
'executed' => $this->isExecuted()
];
}
}
public function jsonSerialize(): mixed
{
return $this->toArray();
}
}

View File

@ -104,6 +104,9 @@ class Mailbox implements Model
public function lastPosition(): int
{
$state = $this->lastState()->getUIDs();
if (count($state) === 0) {
return 0;
}
return array_key_last($state);
}
@ -119,4 +122,8 @@ class Mailbox implements Model
]
];
}
}
public function jsonSerialize(): mixed
{
return $this->toArray();
}
}

View File

@ -99,14 +99,14 @@ class Message implements Model
{
if (!isset($this->states)) {
try {
$this->setStates($this->getFactory()->find(\ProVM\Emails\Model\State\Message::class)->fetchByMessage($this->getId()));
$this->setStates($this->getFactory()->find(State\Message::class)->fetchByMessage($this->getId()));
} catch (BlankResult $e) {
return [];
}
}
return $this->states;
}
public function getState(string $name): \ProVM\Emails\Model\State\Message
public function getState(string $name): State\Message
{
try {
return $this->getStates()[$name];
@ -115,7 +115,7 @@ class Message implements Model
return $this->getStates()[$name];
}
}
public function addState(\ProVM\Emails\Model\State\Message $state): Message
public function addState(State\Message $state): Message
{
$this->states[$state->getName()] = $state;
return $this;
@ -129,7 +129,7 @@ class Message implements Model
}
protected function newState(string $name): Message
{
$this->addState((new \ProVM\Emails\Model\State\Message())
$this->addState((new State\Message())
->setName($name)
->setMessage($this)
->setValue(false)
@ -219,4 +219,8 @@ class Message implements Model
}, $this->getAttachments()) : []
];
}
public function jsonSerialize(): mixed
{
return $this->toArray();
}
}

View File

@ -47,4 +47,18 @@ class Attachment implements Model
$this->value = $value;
return $this;
}
}
public function toArray(): array
{
return [
'id' => $this->getId(),
'attachment_id' => $this->getAttachment()->getId(),
'name' => $this->getName(),
'value' => $this->getValue()
];
}
public function jsonSerialize(): mixed
{
return $this->toArray();
}
}

View File

@ -30,7 +30,7 @@ class Mailbox implements Model
}
public function getUIDs(): array
{
return $this->uids;
return $this->uids ?? [];
}
public function setId(int $id): Mailbox
@ -65,4 +65,19 @@ class Mailbox implements Model
}
return $this;
}
}
public function toArray(): array
{
return [
'id' => $this->getId(),
'mailbox_id' => $this->getMailbox()->getId(),
'date_time' => $this->getDateTime()->format('Y-m-d H:i:s'),
'count' => $this->getCount(),
'uids' => $this->getUIDs()
];
}
public function jsonSerialize(): mixed
{
return $this->toArray();
}
}

View File

@ -47,4 +47,18 @@ class Message implements Model
$this->value = $value;
return $this;
}
}
public function toArray(): array
{
return [
'id' => $this->getId(),
'message_id' => $this->getMessage()->getId(),
'name' => $this->getName(),
'value' => $this->getValue()
];
}
public function jsonSerialize(): mixed
{
return $this->toArray();
}
}

View File

@ -28,6 +28,20 @@ class Attachment extends Repository
return $this;
}
public function install(): void
{
$query = "
CREATE TABLE {$this->getTable()} (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`message_id` INT UNSIGNED NOT NULL,
`filename` VARCHAR(255) NOT NULL,
PRIMARY KEY (`id`),
FOREIGN KEY `fk_messages_{$this->getTable()}` (`message_id`)
REFERENCES `messages` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
)";
$this->getConnection()->query($query);
}
protected function fieldsForInsert(): array
{
return [
@ -115,4 +129,11 @@ class Attachment extends Repository
$query = "SELECT * FROM {$this->getTable()} WHERE message_id = ?";
return $this->fetchMany($query, [$message_id]);
}
}
public function fetchDownloaded(): array
{
$query = "SELECT a.*
FROM `{$this->getTable()}` a JOIN `attachments_states` `as` ON `as`.attachment_id = a.id
WHERE `as`.name = 'downloaded' AND `as`.value = 1";
return $this->fetchMany($query);
}
}

View File

@ -29,6 +29,21 @@ class Job extends Repository
return $this;
}
public function install(): void
{
$query = "
CREATE TABLE {$this->getTable()} (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`message_id` INT UNSIGNED NOT NULL,
`date_time` DATETIME NOT NULL,
`executed` INT(1) UNSIGNED DEFAULT 0,
PRIMARY KEY (`id`),
FOREIGN KEY `fk_messages_{$this->getTable()}` (`message_id`)
REFERENCES `messages` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
)";
$this->getConnection()->query($query);
}
protected function fieldsForUpdate(): array
{
return $this->fieldsForInsert();
@ -105,4 +120,4 @@ class Job extends Repository
$query = "SELECT * FROM {$this->getTable()} WHERE `message_id` = ? AND `executed` = 0";
return $this->fetchOne($query, [$message_id]);
}
}
}

View File

@ -28,6 +28,18 @@ class Mailbox extends Repository
return $this;
}
public function install(): void
{
$query = "
CREATE TABLE {$this->getTable()} (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`name` VARCHAR(100) NOT NULL,
`validity` INT UNSIGNED NOT NULL,
PRIMARY KEY (`id`)
)";
$this->getConnection()->query($query);
}
protected function fieldsForUpdate(): array
{
return $this->fieldsForInsert();
@ -86,4 +98,4 @@ class Mailbox extends Repository
$query = "SELECT * FROM `{$this->getTable()}` WHERE `name` = ?";
return $this->fetchOne($query, [$name]);
}
}
}

View File

@ -3,34 +3,50 @@ namespace ProVM\Emails\Repository;
use DateTimeInterface;
use PDO;
use PDOException;
use Exception;
use ProVM\Common\Define\Model;
use Psr\Log\LoggerInterface;
use ProVM\Common\Implement\Repository;
use Safe\DateTimeImmutable;
use Safe\Exceptions\ErrorfuncException;
use ProVM\Common\Define\Model;
use ProVM\Common\Implement\Repository;
use ProVM\Common\Factory;
class Message extends Repository
{
public function __construct(PDO $connection, LoggerInterface $logger, \ProVM\Common\Factory\Model $factory)
public function __construct(PDO $connection, LoggerInterface $logger, 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
protected Factory\Model $factory;
public function getFactory(): Factory\Model
{
return $this->factory;
}
public function setFactory(\ProVM\Common\Factory\Model $factory): Message
public function setFactory(Factory\Model $factory): Message
{
$this->factory = $factory;
return $this;
}
public function install(): void
{
$query = "
CREATE TABLE {$this->getTable()} (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`uid` VARCHAR(255) NOT NULL,
`mailbox_id` INT UNSIGNED NOT NULL,
`position` INT UNSIGNED NOT NULL,
`subject` VARCHAR(255) NOT NULL,
`from` VARCHAR(255) NOT NULL,
`date_time` DATETIME NOT NULL,
PRIMARY KEY (`id`),
FOREIGN KEY `fk_mailboxes_{$this->getTable()}` (`mailbox_id`)
REFERENCES `mailboxes` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
)";
$this->getConnection()->query($query);
}
protected function fieldsForUpdate(): array
{
return $this->fieldsForInsert();
@ -114,21 +130,23 @@ class Message extends Repository
'downloaded_attachments',
'scheduled_downloads'
];
$stateRepository = $this->getFactory()->find(\ProVM\Emails\Model\State\Message::class);
foreach ($valid_states as $state_name) {
try {
$model->getState($state_name);
} catch (\Exception $e) {
$this->getLogger()->warning($e);
$data = [
'message_id' => $model->getId(),
'name' => $state_name
];
$state = $this->getFactory()->find(\ProVM\Emails\Model\State\Message::class)->create($data);
$state = $stateRepository->create($data);
$model->addState($state);
}
}
foreach ($model->getStates() as $state) {
$state->setMessage($model);
$this->getFactory()->find(\ProVM\Emails\Model\State\Message::class)->save($state);
//$state->setMessage($model);
$stateRepository->save($state);
}
}
@ -161,4 +179,9 @@ class Message extends Repository
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')]);
}
public function fetchAllBySubjectAndDate(string $subject, DateTimeInterface $dateTime): array
{
$query = "SELECT * FROM `{$this->getTable()}` WHERE `subject` = ? AND `date_time` BETWEEN ? AND ?";
return $this->fetchMany($query, [$subject, $dateTime->format('Y-m-d 00:00:00'), $dateTime->format('Y-m-d 23:59:59')]);
}
}

View File

@ -26,6 +26,22 @@ class Attachment extends Repository
return $this;
}
public function install(): void
{
$query = "
CREATE TABLE {$this->getTable()} (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`attachment_id` INT UNSIGNED NOT NULL,
`name` VARCHAR(100) NOT NULL,
`value` INT(1) NOT NULL DEFAULT 0,
PRIMARY KEY (`id`),
FOREIGN KEY `fk_attachments_{$this->getTable()}` (`attachment_id`)
REFERENCES `attachments` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
)
";
$this->getConnection()->query($query);
}
protected function fieldsForInsert(): array
{
return [
@ -90,4 +106,4 @@ class Attachment extends Repository
$query = "SELECT * FROM `{$this->getTable()}` WHERE `attachment_id` = ? AND `name` = ?";
return $this->fetchOne($query, [$attachment_id, $name]);
}
}
}

View File

@ -28,6 +28,23 @@ class Mailbox extends Repository
return $this;
}
public function install(): void
{
$query = "
CREATE TABLE {$this->getTable()} (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`mailbox_id` INT UNSIGNED NOT NULL,
`date_time` DATETIME NOT NULL,
`count` INT UNSIGNED NOT NULL,
`uids` TEXT NOT NULL,
PRIMARY KEY (`id`),
FOREIGN KEY `fk_mailboxes_{$this->getTable()}` (`mailbox_id`)
REFERENCES `mailboxes` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
)
";
$this->getConnection()->query($query);
}
protected function fieldsForUpdate(): array
{
return $this->fieldsForInsert();
@ -99,4 +116,4 @@ class Mailbox extends Repository
$query = "SELECT * FROM `{$this->getTable()}` WHERE `mailbox_id` = ? AND `date_time` = ?";
return $this->fetchOne($query, [$mailbox_id, $date_time]);
}
}
}

View File

@ -26,6 +26,22 @@ class Message extends Repository
return $this;
}
public function install(): void
{
$query = "
CREATE TABLE {$this->getTable()} (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`message_id` INT UNSIGNED NOT NULL,
`name` VARCHAR(100) NOT NULL,
`value` INT(1) NOT NULL DEFAULT 0,
PRIMARY KEY (`id`),
FOREIGN KEY `fk_messages_{$this->getTable()}` (`message_id`)
REFERENCES `messages` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
)
";
$this->getConnection()->query($query);
}
protected function fieldsForUpdate(): array
{
return $this->fieldsForInsert();
@ -90,4 +106,4 @@ class Message extends Repository
$query = "SELECT * FROM `{$this->getTable()}` WHERE `message_id` = ? AND `name` = ?";
return $this->fetchOne($query, [$message_id, $name]);
}
}
}