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)) { try { $this->setStates($this->getStateRepository()->fetchByAttachment($this->getId())); } catch (BlankResult $e) { return []; } } return $this->states; } public function getState(string $name): State\Attachment { try { return $this->getStates()[$name]; } catch (\Exception $e) { $this->newState($name); 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()) ->setAttachment($this) ->setName($name) ); return $this; } public function getFullFilename(): string { return implode(' - ', [ $this->getMessage()->getDateTime()->format('Y-m-d His'), $this->getMessage()->getSubject(), $this->getFilename(), ]); } public function isDownloaded(): bool { return $this->getState('downloaded')?->getValue() ?? false; } public function isEncrypted(): bool { return $this->getState('encrypted')?->getValue() ?? false; } public function isDecrypted(): bool { 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 { 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('decrypted'); $this->getState('decrypted')->setValue(true); } return $this; } public function toArray(): array { return [ 'id' => $this->getId(), '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(), 'fullname' => $this->getFullFilename(), 'downloaded' => $this->isDownloaded(), 'encrypted' => $this->isEncrypted(), 'decrypted' => $this->isDecrypted() ]; } public function jsonSerialize(): mixed { return $this->toArray(); } }