Spaces:
No application file
No application file
File size: 3,773 Bytes
d2897cd |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
<?php
namespace Mautic\EmailBundle\MonitoredEmail;
use Mautic\EmailBundle\EmailEvents;
use Mautic\EmailBundle\Event\ParseEmailEvent;
use Mautic\EmailBundle\MonitoredEmail\Accessor\ConfigAccessor;
use Mautic\EmailBundle\MonitoredEmail\Organizer\MailboxOrganizer;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
class Fetcher
{
private ?array $mailboxes = null;
/**
* @var array
*/
private $log = [];
private int $processedMessageCounter = 0;
public function __construct(
private Mailbox $imapHelper,
private EventDispatcherInterface $dispatcher,
private TranslatorInterface $translator
) {
}
/**
* @return $this
*/
public function setMailboxes(array $mailboxes)
{
$this->mailboxes = $mailboxes;
return $this;
}
/**
* @param int $limit
*/
public function fetch($limit = null): void
{
/** @var ParseEmailEvent $event */
$event = $this->dispatcher->dispatch(new ParseEmailEvent(), EmailEvents::EMAIL_PRE_FETCH);
// Get a list of criteria and group by it
$organizer = new MailboxOrganizer($event, $this->getConfigs());
$organizer->organize();
if (!$containers = $organizer->getContainers()) {
$this->log[] = $this->translator->trans('mautic.email.fetch.no_mailboxes_configured');
return;
}
foreach ($containers as $container) {
$path = $container->getPath();
$markAsSeen = $container->shouldMarkAsSeen();
foreach ($container->getCriteria() as $criteria => $mailboxes) {
try {
// Get mail and parse into Message objects
$this->imapHelper->switchMailbox($mailboxes[0]);
$mailIds = $this->imapHelper->searchMailBox($criteria);
$messages = $this->getMessages($mailIds, $limit, $markAsSeen);
$processed = count($messages);
if ($messages) {
$event->setMessages($messages)
->setKeys($mailboxes);
$this->dispatcher->dispatch($event, EmailEvents::EMAIL_PARSE);
}
$this->log[] = $this->translator->trans(
'mautic.email.fetch.processed',
['%count%' => $processed, '%imapPath%' => $path, '%criteria%' => $criteria]
);
if ($limit && $this->processedMessageCounter >= $limit) {
break;
}
} catch (\Exception $e) {
$this->log[] = $e->getMessage();
}
}
}
}
/**
* @return array
*/
public function getLog()
{
return $this->log;
}
/**
* @param int $limit
* @param bool $markAsSeen
*/
private function getMessages(array $mailIds, $limit, $markAsSeen): array
{
if (!count($mailIds)) {
return [];
}
$messages = [];
foreach ($mailIds as $id) {
$messages[] = $this->imapHelper->getMail($id, $markAsSeen);
++$this->processedMessageCounter;
if ($limit && $this->processedMessageCounter >= $limit) {
break;
}
}
return $messages;
}
private function getConfigs(): array
{
$mailboxes = [];
foreach ($this->mailboxes as $mailbox) {
$mailboxes[$mailbox] = new ConfigAccessor($this->imapHelper->getMailboxSettings($mailbox));
}
return $mailboxes;
}
}
|