124 lines
3.2 KiB
PHP
124 lines
3.2 KiB
PHP
|
<?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()
|
||
|
];
|
||
|
}
|
||
|
}
|