Full implemantation
This commit is contained in:
@ -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
61
api/src/Model/Job.php
Normal 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()
|
||||
];
|
||||
}
|
||||
}
|
@ -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
|
||||
|
@ -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()) : []
|
||||
];
|
||||
}
|
||||
}
|
@ -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
|
||||
|
Reference in New Issue
Block a user