API
This commit is contained in:
124
api/src/Model/Attachment.php
Normal file
124
api/src/Model/Attachment.php
Normal 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
97
api/src/Model/Mailbox.php
Normal 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
212
api/src/Model/Message.php
Normal 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()
|
||||
]
|
||||
];
|
||||
}
|
||||
}
|
50
api/src/Model/State/Attachment.php
Normal file
50
api/src/Model/State/Attachment.php
Normal 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;
|
||||
}
|
||||
}
|
68
api/src/Model/State/Mailbox.php
Normal file
68
api/src/Model/State/Mailbox.php
Normal 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;
|
||||
}
|
||||
}
|
50
api/src/Model/State/Message.php
Normal file
50
api/src/Model/State/Message.php
Normal 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;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user