vendor/symfony/swiftmailer-bundle/EventListener/EmailSenderListener.php line 42

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <fabien@symfony.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Symfony\Bundle\SwiftmailerBundle\EventListener;
  11. use Psr\Log\LoggerInterface;
  12. use Symfony\Component\DependencyInjection\ContainerInterface;
  13. use Symfony\Component\HttpKernel\KernelEvents;
  14. use Symfony\Component\Console\ConsoleEvents;
  15. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  16. /**
  17.  * Sends emails for the memory spool.
  18.  *
  19.  * Emails are sent on the kernel.terminate event.
  20.  *
  21.  * @author Fabien Potencier <fabien@symfony.com>
  22.  */
  23. class EmailSenderListener implements EventSubscriberInterface {
  24.     private $container;
  25.     private $logger;
  26.     private $wasExceptionThrown false;
  27.     public function __construct(ContainerInterface $containerLoggerInterface $logger null) {
  28.         $this->container $container;
  29.         $this->logger $logger;
  30.     }
  31.     public function onException() {
  32.         $this->wasExceptionThrown true;
  33.     }
  34.     public function onTerminate() {
  35.         if (!$this->container->has('mailer') || $this->wasExceptionThrown) {
  36.             return;
  37.         }
  38.         $mailers array_keys($this->container->getParameter('swiftmailer.mailers'));
  39.         foreach ($mailers as $name) {
  40.             if (method_exists($this->container'initialized') ? $this->container->initialized(sprintf('swiftmailer.mailer.%s'$name)) : true) {
  41.                 if ($this->container->getParameter(sprintf('swiftmailer.mailer.%s.spool.enabled'$name))) {
  42.                     $mailer $this->container->get(sprintf('swiftmailer.mailer.%s'$name));
  43.                     $transport $mailer->getTransport();
  44.                     if ($transport instanceof \Swift_Transport_SpoolTransport) {
  45.                         $spool $transport->getSpool();
  46.                         if ($spool instanceof \Swift_MemorySpool) {
  47.                             try {
  48.                                 $spool->flushQueue($this->container->get(sprintf('swiftmailer.mailer.%s.transport.real'$name)));
  49.                             } catch (\Swift_TransportException $exception) {
  50.                                 if (null !== $this->logger) {
  51.                                     $this->logger->error(sprintf('Exception occurred while flushing email queue: %s'$exception->getMessage()));
  52.                                 }
  53.                             }
  54.                         }
  55.                     }
  56.                 }
  57.             }
  58.         }
  59.     }
  60.     public static function getSubscribedEvents() {
  61.         $listeners = [
  62.             KernelEvents::EXCEPTION => 'onException',
  63.             KernelEvents::TERMINATE => 'onTerminate',
  64.         ];
  65.         if (class_exists('Symfony\Component\Console\ConsoleEvents')) {
  66.             $listeners[class_exists('Symfony\Component\Console\Event\ConsoleErrorEvent') ? ConsoleEvents::ERROR ConsoleEvents::EXCEPTION] = 'onException';
  67.             $listeners[ConsoleEvents::TERMINATE] = 'onTerminate';
  68.         }
  69.         return $listeners;
  70.     }
  71.     public function reset() {
  72.         $this->wasExceptionThrown false;
  73.     }
  74. }