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

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\Console\ConsoleEvents;
  13. use Symfony\Component\DependencyInjection\ContainerInterface;
  14. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  15. use Symfony\Component\HttpKernel\KernelEvents;
  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. {
  25.     private $container;
  26.     private $logger;
  27.     private $wasExceptionThrown false;
  28.     public function __construct(ContainerInterface $containerLoggerInterface $logger null)
  29.     {
  30.         $this->container $container;
  31.         $this->logger $logger;
  32.     }
  33.     public function onException()
  34.     {
  35.         $this->wasExceptionThrown true;
  36.     }
  37.     public function onTerminate()
  38.     {
  39.         if ((!$this->container->has('mailer') && !$this->container->has('swiftmailer.mailer.default')) || $this->wasExceptionThrown) {
  40.             return;
  41.         }
  42.         $mailers array_keys($this->container->getParameter('swiftmailer.mailers'));
  43.         foreach ($mailers as $name) {
  44.             if (method_exists($this->container'initialized') ? $this->container->initialized(sprintf('swiftmailer.mailer.%s'$name)) : true) {
  45.                 if ($this->container->getParameter(sprintf('swiftmailer.mailer.%s.spool.enabled'$name))) {
  46.                     $mailer $this->container->get(sprintf('swiftmailer.mailer.%s'$name));
  47.                     $transport $mailer->getTransport();
  48.                     if ($transport instanceof \Swift_Transport_SpoolTransport) {
  49.                         $spool $transport->getSpool();
  50.                         if ($spool instanceof \Swift_MemorySpool) {
  51.                             try {
  52.                                 $spool->flushQueue($this->container->get(sprintf('swiftmailer.mailer.%s.transport.real'$name)));
  53.                             } catch (\Swift_TransportException $exception) {
  54.                                 if (null !== $this->logger) {
  55.                                     $this->logger->error(sprintf('Exception occurred while flushing email queue: %s'$exception->getMessage()));
  56.                                 }
  57.                             }
  58.                         }
  59.                     }
  60.                 }
  61.             }
  62.         }
  63.     }
  64.     public static function getSubscribedEvents()
  65.     {
  66.         $listeners = [
  67.             KernelEvents::EXCEPTION => 'onException',
  68.             KernelEvents::TERMINATE => 'onTerminate',
  69.         ];
  70.         if (class_exists('Symfony\Component\Console\ConsoleEvents')) {
  71.             $listeners[class_exists('Symfony\Component\Console\Event\ConsoleErrorEvent') ? ConsoleEvents::ERROR ConsoleEvents::EXCEPTION] = 'onException';
  72.             $listeners[ConsoleEvents::TERMINATE] = 'onTerminate';
  73.         }
  74.         return $listeners;
  75.     }
  76.     public function reset()
  77.     {
  78.         $this->wasExceptionThrown false;
  79.     }
  80. }