Full implemantation
This commit is contained in:
@ -1,37 +1,177 @@
|
||||
<?php
|
||||
namespace ProVM\Common\Service;
|
||||
|
||||
use ProVM\Common\Factory\Model;
|
||||
use Ddeboer\Imap\Exception\MessageDoesNotExistException;
|
||||
use Ddeboer\Imap\MailboxInterface;
|
||||
use PDOException;
|
||||
use ProVM\Common\Exception\Mailbox\Stateless;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Ddeboer\Imap\MessageInterface;
|
||||
use Ddeboer\Imap\Message\AttachmentInterface;
|
||||
use ProVM\Emails\Model\Mailbox;
|
||||
use ProVM\Emails\Repository\Message;
|
||||
use Safe\DateTimeImmutable;
|
||||
|
||||
class Messages
|
||||
class Messages extends Base
|
||||
{
|
||||
public function __construct(Model $factory, LoggerInterface $logger)
|
||||
public function __construct(Mailboxes $mailboxes, Message $repository, Remote\Messages $remoteService, LoggerInterface $logger)
|
||||
{
|
||||
$this->setFactory($factory)
|
||||
$this->setMailboxes($mailboxes)
|
||||
->setRepository($repository)
|
||||
->setRemoteService($remoteService)
|
||||
->setLogger($logger);
|
||||
}
|
||||
|
||||
protected Model $factory;
|
||||
protected LoggerInterface $logger;
|
||||
protected Mailboxes $mailboxes;
|
||||
protected Message $repository;
|
||||
protected Remote\Messages $remoteService;
|
||||
|
||||
public function getFactory(): Model
|
||||
public function getMailboxes(): Mailboxes
|
||||
{
|
||||
return $this->factory;
|
||||
return $this->mailboxes;
|
||||
}
|
||||
public function getLogger(): LoggerInterface
|
||||
public function getRepository(): Message
|
||||
{
|
||||
return $this->logger;
|
||||
return $this->repository;
|
||||
}
|
||||
public function getRemoteService(): Remote\Messages
|
||||
{
|
||||
return $this->remoteService;
|
||||
}
|
||||
|
||||
public function setFactory(Model $factory): Messages
|
||||
public function setMailboxes(Mailboxes $mailboxes): Messages
|
||||
{
|
||||
$this->factory = $factory;
|
||||
$this->mailboxes = $mailboxes;
|
||||
return $this;
|
||||
}
|
||||
public function setLogger(LoggerInterface $logger): Messages
|
||||
public function setRepository(Message $repository): Messages
|
||||
{
|
||||
$this->logger = $logger;
|
||||
$this->repository = $repository;
|
||||
return $this;
|
||||
}
|
||||
public function setRemoteService(Remote\Messages $service): Messages
|
||||
{
|
||||
$this->remoteService = $service;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getLocalMessage(string $message_uid): \ProVM\Emails\Model\Message
|
||||
{
|
||||
return $this->getRepository()->fetchByUID($message_uid);
|
||||
}
|
||||
public function getRemoteMessage(string $message_uid): MessageInterface
|
||||
{
|
||||
$message = $this->getLocalMessage($message_uid);
|
||||
$remote_mailbox = $this->getMailboxes()->getRemoteMailbox($message->getMailbox()->getName());
|
||||
return $this->getRemoteService()->get($remote_mailbox, $message->getSubject(), $message->getFrom(), $message->getDateTime());
|
||||
}
|
||||
|
||||
public function getAll(string $mailbox_name): array
|
||||
{
|
||||
$mailbox = $this->getMailboxes()->getLocalMailbox($mailbox_name);
|
||||
return $this->getRepository()->fetchByMailbox($mailbox->getId());
|
||||
}
|
||||
public function getValid(string $mailbox_name): array
|
||||
{
|
||||
$mailbox = $this->getMailboxes()->getLocalMailbox($mailbox_name);
|
||||
return $this->getRepository()->fetchValidByMailbox($mailbox->getId());
|
||||
}
|
||||
public function grab(string $mailbox_name): array
|
||||
{
|
||||
$mailbox = $this->getMailboxes()->getLocalMailbox($mailbox_name);
|
||||
if (!$this->getMailboxes()->validate($mailbox_name)) {
|
||||
$this->restoreUIDs($mailbox_name);
|
||||
}
|
||||
try {
|
||||
$start = $mailbox->lastPosition() + 1;
|
||||
} catch (Stateless $e) {
|
||||
$start = 0;
|
||||
}
|
||||
$remote_mailbox = $this->getMailboxes()->getRemoteMailbox($mailbox_name);
|
||||
$total = $remote_mailbox->count();
|
||||
$total_amount = $total - $start;
|
||||
$amount = min(100, $total_amount);
|
||||
$messages = [];
|
||||
for ($i = 0; $i < $total_amount; $i += $amount) {
|
||||
if ($amount + $i > $total_amount) {
|
||||
$amount = $total_amount - $i;
|
||||
}
|
||||
$remote_messages = $this->getRemoteService()->getAll($remote_mailbox, $start + $i, $amount);
|
||||
foreach ($remote_messages as $p => $m) {
|
||||
if ($this->save($mailbox, $m, $p)) {
|
||||
$messages []= $m->getId();
|
||||
}
|
||||
}
|
||||
}
|
||||
$this->getMailboxes()->updateState($mailbox, $messages, new DateTimeImmutable());
|
||||
return $messages;
|
||||
}
|
||||
public function restoreUIDs(string $mailbox_name): void
|
||||
{
|
||||
$mailbox = $this->getMailboxes()->getLocalMailbox($mailbox_name);
|
||||
$remote_mailbox = $this->getMailboxes()->getRemoteMailbox($mailbox_name);
|
||||
$total_amount = $mailbox->lastPosition();
|
||||
$amount = min(100, $total_amount);
|
||||
for ($i = 0; $i < $total_amount; $i += $amount) {
|
||||
if ($amount + $i > $total_amount) {
|
||||
$amount = $total_amount - $i;
|
||||
}
|
||||
$remote_messages = $this->getRemoteService()->getAll($remote_mailbox, $i, $amount);
|
||||
foreach ($remote_messages as $m) {
|
||||
$this->update($mailbox, $m, $i);
|
||||
}
|
||||
}
|
||||
}
|
||||
public function save(Mailbox $mailbox, MessageInterface $remote_message, int $position): bool
|
||||
{
|
||||
$data = [
|
||||
'mailbox_id' => $mailbox->getId(),
|
||||
'position' => $position,
|
||||
'uid' => $remote_message->getId(),
|
||||
'subject' => $remote_message->getSubject() ?? '',
|
||||
'from' => $remote_message->getFrom()->getAddress(),
|
||||
'date_time' => $remote_message->getDate()->format('Y-m-d H:i:s')
|
||||
];
|
||||
try {
|
||||
$message = $this->getRepository()->create($data);
|
||||
if ($message->getId() === 0) {
|
||||
if ($remote_message->hasAttachments()) {
|
||||
$message->doesHaveAttachments();
|
||||
}
|
||||
if ($this->validAttachments($remote_message)) {
|
||||
$message->doesHaveValidAttachments();
|
||||
}
|
||||
}
|
||||
$this->getRepository()->save($message);
|
||||
return true;
|
||||
} catch (PDOException $e) {
|
||||
$this->getLogger()->error($e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
public function update(Mailbox $mailbox, MessageInterface $remote_message, int $position): bool
|
||||
{
|
||||
try {
|
||||
$message = $this->getRepository()->fetchByMailboxSubjectFromAndDate($mailbox->getId(), $remote_message->getSubject(), $remote_message->getFrom()->getAddress(), $remote_message->getDate());
|
||||
$message->setUID($remote_message->getId());
|
||||
$message->setPosition($position);
|
||||
$this->getRepository()->save($message);
|
||||
return true;
|
||||
} catch (PDOException $e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
public function validateAttachment(AttachmentInterface $attachment): bool
|
||||
{
|
||||
return str_contains($attachment->getFilename(), '.pdf');
|
||||
}
|
||||
public function validAttachments(MessageInterface $remote_message): bool
|
||||
{
|
||||
foreach ($remote_message->getAttachments() as $attachment) {
|
||||
if ($this->validateAttachment($attachment)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user