Full implemantation

This commit is contained in:
2022-11-28 22:56:21 -03:00
parent 30ef4c6a35
commit c53eb4c7a6
55 changed files with 1505 additions and 1011 deletions

View File

@ -1,151 +1,143 @@
<?php
namespace ProVM\Common\Service;
use Ddeboer\Imap\Message\AttachmentInterface;
use Iterator;
use Generator;
use Ddeboer\Imap\ConnectionInterface;
use Ddeboer\Imap\MailboxInterface;
use Ddeboer\Imap\MessageInterface;
use ProVM\Common\Exception\EmptyMailbox;
use ProVM\Common\Exception\Mailbox\Invalid;
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(ConnectionInterface $connection, Attachments $attachments, string $username)
public function __construct(Mailbox $repository, Remote\Mailboxes $remoteService, State\Mailbox $states, LoggerInterface $logger)
{
$this->setConnection($connection)
->setAttachments($attachments)
->setUsername($username);
$this->setRepository($repository)
->setRemoteService($remoteService)
->setStatesRepository($states)
->setLogger($logger);
}
protected ConnectionInterface $connection;
protected Attachments $attachments;
protected string $username;
protected Mailbox $repository;
protected Remote\Mailboxes $remoteService;
protected State\Mailbox $statesRepository;
public function getConnection(): ConnectionInterface
public function getRepository(): Mailbox
{
return $this->connection;
return $this->repository;
}
public function getAttachments(): Attachments
public function getRemoteService(): Remote\Mailboxes
{
return $this->attachments;
return $this->remoteService;
}
public function getUsername(): string
public function getStatesRepository(): State\Mailbox
{
return $this->username;
return $this->statesRepository;
}
public function setConnection(ConnectionInterface $connection): Mailboxes
public function setRepository(Mailbox $repository): Mailboxes
{
$this->connection = $connection;
$this->repository = $repository;
return $this;
}
public function setAttachments(Attachments $attachments): Mailboxes
public function setRemoteService(Remote\Mailboxes $service): Mailboxes
{
$this->attachments = $attachments;
$this->remoteService = $service;
return $this;
}
public function setUsername(string $username): Mailboxes
public function setStatesRepository(State\Mailbox $repository): Mailboxes
{
$this->username = $username;
$this->statesRepository = $repository;
return $this;
}
protected array $mailboxes;
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
{
if (!isset($this->mailboxes)) {
$this->mailboxes = $this->getConnection()->getMailboxes();
}
return $this->mailboxes;
return $this->getRemoteService()->getAll();
}
public function get(string $mailbox): MailboxInterface
public function getRegistered(): array
{
if (!$this->getConnection()->hasMailbox($mailbox)) {
throw new Invalid();
}
return $this->getConnection()->getMailbox($mailbox);
return $this->getRepository()->fetchAll();
}
public function get(int $mailbox_id): \ProVM\Emails\Model\Mailbox
{
return $this->getRepository()->fetchById($mailbox_id);
}
// Messages
protected function advanceIterator(Iterator $iterator, int $up_to): Iterator
public function isRegistered(string $mailbox_name): bool
{
for ($i = 0; $i < $up_to; $i ++) {
$iterator->next();
try {
$mailbox = $this->getRepository()->fetchByName($mailbox_name);
return true;
} catch (BlankResult $e) {
return false;
}
return $iterator;
}
public function getMessages(MailboxInterface $mailbox, int $start = 0, ?int $amount = null): Generator
public function register(string $mailbox_name): bool
{
if ($mailbox->count() === 0) {
$this->getLogger()->notice('No mails found.');
throw new EmptyMailbox();
}
$it = $mailbox->getIterator();
if ($amount === null) {
$amount = $mailbox->count() - $start;
}
$it = $this->advanceIterator($it, $start);
for ($i = $start; $i < min($start + $amount, $mailbox->count()); $i ++) {
yield $it->key() => $it->current();
$it->next();
$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 countValid(MailboxInterface $mailbox): int
public function updateState(\ProVM\Emails\Model\Mailbox $mailbox, array $messages, \DateTimeInterface $dateTime): bool
{
$cnt = 0;
foreach ($mailbox->getIterator() as $message) {
if ($this->hasAttachments($message) and $this->validAttachments($message)) {
$cnt ++;
}
$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;
}
return $cnt;
}
/**
* @param MailboxInterface $mailbox
* @param int $uid
* @return MessageInterface
*/
public function getMessage(MailboxInterface $mailbox, int $uid): MessageInterface
public function unregister(string $mailbox_name): bool
{
return $mailbox->getMessage($uid);
try {
$mailbox = $this->getRepository()->fetchByName($mailbox_name);
} catch (BlankResult $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);
protected function validateAddress(array $to): bool
{
foreach ($to as $address) {
if (strtolower($address->getAddress()) === strtolower($this->getUsername())) {
return true;
}
}
return false;
}
public function hasAttachments(MessageInterface $message): bool
{
return ($message->hasAttachments() and $this->validateAddress($message->getTo()));
}
protected function validateAttachment(AttachmentInterface $attachment): bool
{
return str_contains($attachment->getFilename(), '.pdf');
}
public function validAttachments(MessageInterface $message): bool
{
foreach ($message->getAttachments() as $attachment) {
if ($this->validateAttachment($attachment)) {
return true;
}
}
return false;
}
public function downloadAttachments(MessageInterface $message)
{
foreach ($message->getAttachments() as $attachment) {
if ($this->validateAttachment($attachment)) {
$attachment->getContent();
}
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;
}
}