154 lines
4.8 KiB
PHP
154 lines
4.8 KiB
PHP
<?php
|
|
namespace ProVM\Common\Service;
|
|
|
|
use Ddeboer\Imap\MailboxInterface;
|
|
use PDOException;
|
|
use Psr\Log\LoggerInterface;
|
|
use ProVM\Common\Exception\Database\BlankResult;
|
|
use ProVM\Emails\Repository\Mailbox;
|
|
use ProVM\Common\Service\Remote;
|
|
use ProVM\Emails\Repository\State;
|
|
|
|
class Mailboxes extends Base
|
|
{
|
|
public function __construct(Mailbox $repository, Remote\Mailboxes $remoteService, State\Mailbox $states, LoggerInterface $logger, protected int $max_update_days)
|
|
{
|
|
$this->setRepository($repository)
|
|
->setRemoteService($remoteService)
|
|
->setStatesRepository($states)
|
|
->setLogger($logger);
|
|
}
|
|
|
|
protected Mailbox $repository;
|
|
protected Remote\Mailboxes $remoteService;
|
|
protected State\Mailbox $statesRepository;
|
|
|
|
public function getRepository(): Mailbox
|
|
{
|
|
return $this->repository;
|
|
}
|
|
public function getRemoteService(): Remote\Mailboxes
|
|
{
|
|
return $this->remoteService;
|
|
}
|
|
public function getStatesRepository(): State\Mailbox
|
|
{
|
|
return $this->statesRepository;
|
|
}
|
|
public function setRepository(Mailbox $repository): Mailboxes
|
|
{
|
|
$this->repository = $repository;
|
|
return $this;
|
|
}
|
|
public function setRemoteService(Remote\Mailboxes $service): Mailboxes
|
|
{
|
|
$this->remoteService = $service;
|
|
return $this;
|
|
}
|
|
public function setStatesRepository(State\Mailbox $repository): Mailboxes
|
|
{
|
|
$this->statesRepository = $repository;
|
|
return $this;
|
|
}
|
|
|
|
public function getLocalMailbox(string $mailbox_name): \ProVM\Emails\Model\Mailbox
|
|
{
|
|
return $this->getRepository()->fetchByName($mailbox_name);
|
|
}
|
|
public function getRemoteMailbox(string $mailbox_name): MailboxInterface
|
|
{
|
|
return $this->getRemoteService()->get($mailbox_name);
|
|
}
|
|
|
|
public function getAll(): array
|
|
{
|
|
return $this->getRemoteService()->getAll();
|
|
}
|
|
public function getRegistered(): array
|
|
{
|
|
return $this->getRepository()->fetchAll();
|
|
}
|
|
public function get(int $mailbox_id): \ProVM\Emails\Model\Mailbox
|
|
{
|
|
return $this->getRepository()->fetchById($mailbox_id);
|
|
}
|
|
|
|
public function isRegistered(string $mailbox_name): bool
|
|
{
|
|
try {
|
|
$mailbox = $this->getRepository()->fetchByName($mailbox_name);
|
|
return true;
|
|
} catch (BlankResult $e) {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public function register(string $mailbox_name): bool
|
|
{
|
|
$remote_mailbox = $this->getRemoteMailbox($mailbox_name);
|
|
$name = $remote_mailbox->getName();
|
|
$validity = $remote_mailbox->getStatus()->uidvalidity;
|
|
try {
|
|
$mailbox = $this->getRepository()->create(compact('name', 'validity'));
|
|
$this->getRepository()->save($mailbox);
|
|
return true;
|
|
} catch (PDOException $e) {
|
|
return false;
|
|
}
|
|
}
|
|
public function updateState(\ProVM\Emails\Model\Mailbox $mailbox, array $messages, \DateTimeInterface $dateTime): bool
|
|
{
|
|
$data = [
|
|
'mailbox_id' => $mailbox->getId(),
|
|
'date_time' => $dateTime->format('Y-m-d H:i:s'),
|
|
'count' => count($messages),
|
|
'uids' => serialize($messages)
|
|
];
|
|
try {
|
|
$state = $this->getStatesRepository()->create($data);
|
|
$this->getStatesRepository()->save($state);
|
|
return true;
|
|
} catch (PDOException $e) {
|
|
$this->getLogger()->error($e);
|
|
return false;
|
|
}
|
|
}
|
|
public function unregister(string $mailbox_name): bool
|
|
{
|
|
try {
|
|
$mailbox = $this->getRepository()->fetchByName($mailbox_name);
|
|
} catch (BlankResult $e) {
|
|
$this->getLogger()->error($e);
|
|
// It's already unregistered
|
|
return true;
|
|
}
|
|
try {
|
|
$this->getRepository()->delete($mailbox);
|
|
return true;
|
|
} catch (PDOException $e) {
|
|
return false;
|
|
}
|
|
}
|
|
public function validate(string $mailbox_name): bool
|
|
{
|
|
$mailbox = $this->getLocalMailbox($mailbox_name);
|
|
|
|
if (!$this->getRemoteService()->validate($mailbox_name, $mailbox->getValidity())) {
|
|
$remote_mailbox = $this->getRemoteMailbox($mailbox_name);
|
|
$mailbox->setValidity($remote_mailbox->getStatus()->uidvalidity);
|
|
$this->getRepository()->save($mailbox);
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
public function isUpdated(\ProVM\Emails\Model\Mailbox $mailbox): bool
|
|
{
|
|
$states = $mailbox->getStates();
|
|
if (count($states) === 0) {
|
|
return false;
|
|
}
|
|
$last = $states[count($states) - 1];
|
|
return abs((int) $last->getDateTime()->diff(new \DateTimeImmutable())->format('%r%a')) < $this->max_update_days;
|
|
}
|
|
}
|