<?php declare(strict_types=0);
/*
* WellCommerce Foundation
*
* This file is part of the WellCommerce package.
*
* (c) Adam Piotrowski <adam@wellcommerce.org>, Adrian Potepa <adrian@wellcommerce.org>
*
* For the full copyright and license information,
* please view the LICENSE file that was distributed with this source code.
*/
namespace WellCommerce\Bundle\AppBundle\EventListener;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use WellCommerce\Bundle\AppBundle\Entity\Mailbox;
use WellCommerce\Bundle\AppBundle\Service\Imap\ImapClient;
use WellCommerce\Bundle\AppBundle\Service\Shop\Storage\ShopStorage;
use WellCommerce\Bundle\CoreBundle\Event\MailEvent;
/**
* Class CurrencySubscriber
*
* @author Adam Piotrowski <adam@wellcommerce.org>
*/
class ImapSubscriber implements EventSubscriberInterface
{
/**
* @var ShopStorage
*/
protected $shopStorage;
/**
* @var ImapClient
*/
protected $imapClient;
public function __construct(ShopStorage $shopStorage, ImapClient $imapClient)
{
$this->shopStorage = $shopStorage;
$this->imapClient = $imapClient;
}
public static function getSubscribedEvents()
{
return [
'mail.post_send' => ['onMailPostSend', 0],
];
}
public function onMailPostSend(MailEvent $event)
{
$message = $event->getMessage();
$mailbox = $this->shopStorage->getCurrentShop()->getMailbox();
if ($mailbox instanceof Mailbox && $mailbox->isEnabled()) {
foreach ($message->getFrom() as $from => $name) {
if ($mailbox->getUser() === $from) {
$result = $this->imapClient->saveMessage($message, $mailbox);
}
}
}
}
}