97 lines
2.4 KiB
PHP
97 lines
2.4 KiB
PHP
|
<?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
|
||
|
]
|
||
|
];
|
||
|
}
|
||
|
}
|