Files
emails/api/common/Service/Mailboxes.php

144 lines
4.4 KiB
PHP
Raw Normal View History

2022-11-25 20:52:52 -03:00
<?php
namespace ProVM\Common\Service;
use Ddeboer\Imap\MailboxInterface;
2022-11-28 22:56:21 -03:00
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;
2022-11-25 20:52:52 -03:00
class Mailboxes extends Base
{
2022-11-28 22:56:21 -03:00
public function __construct(Mailbox $repository, Remote\Mailboxes $remoteService, State\Mailbox $states, LoggerInterface $logger)
2022-11-25 20:52:52 -03:00
{
2022-11-28 22:56:21 -03:00
$this->setRepository($repository)
->setRemoteService($remoteService)
->setStatesRepository($states)
->setLogger($logger);
2022-11-25 20:52:52 -03:00
}
2022-11-28 22:56:21 -03:00
protected Mailbox $repository;
protected Remote\Mailboxes $remoteService;
protected State\Mailbox $statesRepository;
2022-11-25 20:52:52 -03:00
2022-11-28 22:56:21 -03:00
public function getRepository(): Mailbox
2022-11-25 20:52:52 -03:00
{
2022-11-28 22:56:21 -03:00
return $this->repository;
2022-11-25 20:52:52 -03:00
}
2022-11-28 22:56:21 -03:00
public function getRemoteService(): Remote\Mailboxes
2022-11-25 20:52:52 -03:00
{
2022-11-28 22:56:21 -03:00
return $this->remoteService;
2022-11-25 20:52:52 -03:00
}
2022-11-28 22:56:21 -03:00
public function getStatesRepository(): State\Mailbox
2022-11-25 20:52:52 -03:00
{
2022-11-28 22:56:21 -03:00
return $this->statesRepository;
2022-11-25 20:52:52 -03:00
}
2022-11-28 22:56:21 -03:00
public function setRepository(Mailbox $repository): Mailboxes
2022-11-25 20:52:52 -03:00
{
2022-11-28 22:56:21 -03:00
$this->repository = $repository;
2022-11-25 20:52:52 -03:00
return $this;
}
2022-11-28 22:56:21 -03:00
public function setRemoteService(Remote\Mailboxes $service): Mailboxes
2022-11-25 20:52:52 -03:00
{
2022-11-28 22:56:21 -03:00
$this->remoteService = $service;
2022-11-25 20:52:52 -03:00
return $this;
}
2022-11-28 22:56:21 -03:00
public function setStatesRepository(State\Mailbox $repository): Mailboxes
2022-11-25 20:52:52 -03:00
{
2022-11-28 22:56:21 -03:00
$this->statesRepository = $repository;
2022-11-25 20:52:52 -03:00
return $this;
}
2022-11-28 22:56:21 -03:00
public function getLocalMailbox(string $mailbox_name): \ProVM\Emails\Model\Mailbox
2022-11-25 20:52:52 -03:00
{
2022-11-28 22:56:21 -03:00
return $this->getRepository()->fetchByName($mailbox_name);
2022-11-25 20:52:52 -03:00
}
2022-11-28 22:56:21 -03:00
public function getRemoteMailbox(string $mailbox_name): MailboxInterface
2022-11-25 20:52:52 -03:00
{
2022-11-28 22:56:21 -03:00
return $this->getRemoteService()->get($mailbox_name);
2022-11-25 20:52:52 -03:00
}
2022-11-28 22:56:21 -03:00
public function getAll(): array
2022-11-25 20:52:52 -03:00
{
2022-11-28 22:56:21 -03:00
return $this->getRemoteService()->getAll();
2022-11-25 20:52:52 -03:00
}
2022-11-28 22:56:21 -03:00
public function getRegistered(): array
2022-11-25 20:52:52 -03:00
{
2022-11-28 22:56:21 -03:00
return $this->getRepository()->fetchAll();
2022-11-25 20:52:52 -03:00
}
2022-11-28 22:56:21 -03:00
public function get(int $mailbox_id): \ProVM\Emails\Model\Mailbox
2022-11-25 20:52:52 -03:00
{
2022-11-28 22:56:21 -03:00
return $this->getRepository()->fetchById($mailbox_id);
2022-11-25 20:52:52 -03:00
}
2022-11-28 22:56:21 -03:00
public function isRegistered(string $mailbox_name): bool
2022-11-25 20:52:52 -03:00
{
2022-11-28 22:56:21 -03:00
try {
$mailbox = $this->getRepository()->fetchByName($mailbox_name);
return true;
} catch (BlankResult $e) {
return false;
2022-11-25 20:52:52 -03:00
}
}
2022-11-28 22:56:21 -03:00
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;
}
2022-11-25 20:52:52 -03:00
}
2022-11-28 22:56:21 -03:00
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) {
return false;
}
2022-11-25 20:52:52 -03:00
}
2022-11-28 22:56:21 -03:00
public function unregister(string $mailbox_name): bool
2022-11-25 20:52:52 -03:00
{
2022-11-28 22:56:21 -03:00
try {
$mailbox = $this->getRepository()->fetchByName($mailbox_name);
} catch (BlankResult $e) {
2022-11-30 10:40:36 -03:00
$this->getLogger()->error($e);
2022-11-28 22:56:21 -03:00
// It's already unregistered
return true;
}
try {
$this->getRepository()->delete($mailbox);
return true;
} catch (PDOException $e) {
return false;
2022-11-25 20:52:52 -03:00
}
}
2022-11-28 22:56:21 -03:00
public function validate(string $mailbox_name): bool
2022-11-25 20:52:52 -03:00
{
2022-11-28 22:56:21 -03:00
$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;
2022-11-25 20:52:52 -03:00
}
2022-11-28 22:56:21 -03:00
return true;
2022-11-25 20:52:52 -03:00
}
}