Full implemantation
This commit is contained in:
31
api/common/Service/Remote/Attachments.php
Normal file
31
api/common/Service/Remote/Attachments.php
Normal file
@ -0,0 +1,31 @@
|
||||
<?php
|
||||
namespace ProVM\Common\Service\Remote;
|
||||
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Ddeboer\Imap\ConnectionInterface;
|
||||
use Ddeboer\Imap\MessageInterface;
|
||||
use Ddeboer\Imap\Message\AttachmentInterface;
|
||||
use ProVM\Common\Exception\Attachment\NotFound;
|
||||
|
||||
class Attachments extends Base
|
||||
{
|
||||
public function __construct(ConnectionInterface $connection, LoggerInterface $logger)
|
||||
{
|
||||
$this->setConnection($connection)
|
||||
->setLogger($logger);
|
||||
}
|
||||
|
||||
public function getAttachments(MessageInterface $message): array
|
||||
{
|
||||
return $message->getAttachments();
|
||||
}
|
||||
public function get(MessageInterface $message, string $filename): AttachmentInterface
|
||||
{
|
||||
foreach ($message->getAttachments() as $attachment) {
|
||||
if ($attachment->getFilename() === $filename) {
|
||||
return $attachment;
|
||||
}
|
||||
}
|
||||
throw new NotFound($message, $filename);
|
||||
}
|
||||
}
|
21
api/common/Service/Remote/Base.php
Normal file
21
api/common/Service/Remote/Base.php
Normal file
@ -0,0 +1,21 @@
|
||||
<?php
|
||||
namespace ProVM\Common\Service\Remote;
|
||||
|
||||
use Ddeboer\Imap\ConnectionInterface;
|
||||
use ProVM\Common\Service\Base as BaseService;
|
||||
|
||||
abstract class Base extends BaseService
|
||||
{
|
||||
protected ConnectionInterface $connection;
|
||||
|
||||
public function getConnection(): ConnectionInterface
|
||||
{
|
||||
return $this->connection;
|
||||
}
|
||||
|
||||
public function setConnection(ConnectionInterface $connection): Base
|
||||
{
|
||||
$this->connection = $connection;
|
||||
return $this;
|
||||
}
|
||||
}
|
45
api/common/Service/Remote/Mailboxes.php
Normal file
45
api/common/Service/Remote/Mailboxes.php
Normal file
@ -0,0 +1,45 @@
|
||||
<?php
|
||||
namespace ProVM\Common\Service\Remote;
|
||||
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Ddeboer\Imap\ConnectionInterface;
|
||||
use Ddeboer\Imap\MailboxInterface;
|
||||
use ProVM\Common\Exception\Mailbox\Invalid;
|
||||
|
||||
/**
|
||||
* Grab mailboxes from Email Provider
|
||||
*/
|
||||
class Mailboxes extends Base
|
||||
{
|
||||
public function __construct(ConnectionInterface $connection, LoggerInterface $logger)
|
||||
{
|
||||
$this->setConnection($connection)
|
||||
->setLogger($logger);
|
||||
}
|
||||
|
||||
protected array $mailboxes;
|
||||
public function getAll(): array
|
||||
{
|
||||
if (!isset($this->mailboxes)) {
|
||||
$this->mailboxes = $this->getConnection()->getMailboxes();
|
||||
}
|
||||
return $this->mailboxes;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws Invalid
|
||||
*/
|
||||
public function get(string $mailbox_name): MailboxInterface
|
||||
{
|
||||
if (!$this->getConnection()->hasMailbox($mailbox_name)) {
|
||||
throw new Invalid($mailbox_name);
|
||||
}
|
||||
return $this->getConnection()->getMailbox($mailbox_name);
|
||||
}
|
||||
|
||||
public function validate(string $mailbox_name, int $uidvalidity): bool
|
||||
{
|
||||
$mailbox = $this->get($mailbox_name);
|
||||
return ($mailbox->getStatus()->uidvalidity === $uidvalidity);
|
||||
}
|
||||
}
|
67
api/common/Service/Remote/Messages.php
Normal file
67
api/common/Service/Remote/Messages.php
Normal file
@ -0,0 +1,67 @@
|
||||
<?php
|
||||
namespace ProVM\Common\Service\Remote;
|
||||
|
||||
use DateTimeInterface;
|
||||
use Ddeboer\Imap\Exception\MessageDoesNotExistException;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Ddeboer\Imap\ConnectionInterface;
|
||||
use Ddeboer\Imap\MailboxInterface;
|
||||
use Ddeboer\Imap\MessageInterface;
|
||||
use Ddeboer\Imap\SearchExpression;
|
||||
use Ddeboer\Imap\Search\Date\On;
|
||||
use Ddeboer\Imap\Search\Email\From;
|
||||
use Ddeboer\Imap\Search\Text\Subject;
|
||||
use ProVM\Common\Exception\EmptyMailbox;
|
||||
|
||||
class Messages extends Base
|
||||
{
|
||||
public function __construct(ConnectionInterface $connection, LoggerInterface $logger)
|
||||
{
|
||||
$this->setConnection($connection)
|
||||
->setLogger($logger);
|
||||
}
|
||||
|
||||
public function getAll(MailboxInterface $mailbox, int $start = 0, ?int $amount = null): array
|
||||
{
|
||||
if ($mailbox->count() === 0) {
|
||||
throw new EmptyMailbox($mailbox);
|
||||
}
|
||||
|
||||
if ($amount === null) {
|
||||
$amount = $mailbox->count() - $start;
|
||||
}
|
||||
|
||||
$it = $mailbox->getIterator();
|
||||
for ($i = 0; $i < $start; $i ++) {
|
||||
$it->next();
|
||||
}
|
||||
|
||||
$messages = [];
|
||||
for ($i = $start; $i < $start + $amount; $i ++) {
|
||||
if (!$it->valid()) {
|
||||
break;
|
||||
}
|
||||
$messages[$i] = $it->current();
|
||||
$it->next();
|
||||
}
|
||||
return $messages;
|
||||
}
|
||||
public function get(MailboxInterface $mailbox, string $subject, string $from, DateTimeInterface $dateTime): MessageInterface
|
||||
{
|
||||
if ($mailbox->count() === 0) {
|
||||
$this->getLogger()->notice("Mailbox {$mailbox->getName()} is empty");
|
||||
throw new EmptyMailbox($mailbox);
|
||||
}
|
||||
|
||||
$query = new SearchExpression();
|
||||
$query->addCondition(new Subject($subject));
|
||||
$query->addCondition(new From($from));
|
||||
$query->addCondition(new On($dateTime));
|
||||
|
||||
$result = $mailbox->getMessages($query);
|
||||
if (count($result) === 0) {
|
||||
throw new MessageDoesNotExistException("{$mailbox->getName()}: {$subject} - {$from} [{$dateTime->format('Y-m-d H:i:s')}]");
|
||||
}
|
||||
return $result->current();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user