Files
emails/api/src/Model/Attachment.php

177 lines
4.9 KiB
PHP
Raw Normal View History

2022-11-25 20:52:52 -03:00
<?php
namespace ProVM\Emails\Model;
use ProVM\Common\Define\Model;
2022-11-28 22:56:21 -03:00
use ProVM\Common\Exception\Database\BlankResult;
2022-11-25 20:52:52 -03:00
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)) {
2022-11-28 22:56:21 -03:00
try {
$this->setStates($this->getStateRepository()->fetchByAttachment($this->getId()));
} catch (BlankResult $e) {
return [];
}
2022-11-25 20:52:52 -03:00
}
return $this->states;
}
public function getState(string $name): State\Attachment
{
2022-11-28 22:56:21 -03:00
try {
return $this->getStates()[$name];
} catch (\Exception $e) {
$this->newState($name);
return $this->getStates()[$name];
}
2022-11-25 20:52:52 -03:00
}
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())
->setAttachment($this)
2022-11-28 22:56:21 -03:00
->setName($name)
2022-11-25 20:52:52 -03:00
);
return $this;
}
2022-11-28 22:56:21 -03:00
public function getFullFilename(): string
{
return implode(' - ', [
$this->getMessage()->getDateTime()->format('Y-m-d His'),
2023-06-12 21:14:07 -04:00
$this->getMessage()->getSubject(),
$this->getFilename(),
2022-11-28 22:56:21 -03:00
]);
}
public function isDownloaded(): bool
{
return $this->getState('downloaded')?->getValue() ?? false;
}
2022-11-25 20:52:52 -03:00
public function isEncrypted(): bool
{
2022-11-28 22:56:21 -03:00
return $this->getState('encrypted')?->getValue() ?? false;
2022-11-25 20:52:52 -03:00
}
public function isDecrypted(): bool
{
2022-11-28 22:56:21 -03:00
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;
2022-11-25 20:52:52 -03:00
}
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) {
2022-11-28 22:56:21 -03:00
$this->newState('decrypted');
2022-11-25 20:52:52 -03:00
$this->getState('decrypted')->setValue(true);
}
return $this;
}
public function toArray(): array
{
return [
'id' => $this->getId(),
2022-11-28 22:56:21 -03:00
'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')
],
2022-11-25 20:52:52 -03:00
'filename' => $this->getFilename(),
2022-11-29 11:12:06 -03:00
'fullname' => $this->getFullFilename(),
2022-11-28 22:56:21 -03:00
'downloaded' => $this->isDownloaded(),
2022-11-25 20:52:52 -03:00
'encrypted' => $this->isEncrypted(),
'decrypted' => $this->isDecrypted()
];
}
2023-06-08 20:49:27 -04:00
public function jsonSerialize(): mixed
{
return $this->toArray();
}
}