vendor/symfony/symfony/src/Symfony/Bridge/Monolog/Handler/SwiftMailerHandler.php line 45

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\Bridge\Monolog\Handler;
  11. use Monolog\Handler\SwiftMailerHandler as BaseSwiftMailerHandler;
  12. use Symfony\Component\Console\Event\ConsoleTerminateEvent;
  13. use Symfony\Component\HttpKernel\Event\PostResponseEvent;
  14. /**
  15.  * Extended SwiftMailerHandler that flushes mail queue if necessary.
  16.  *
  17.  * @author Philipp Kräutli <pkraeutli@astina.ch>
  18.  */
  19. class SwiftMailerHandler extends BaseSwiftMailerHandler
  20. {
  21.     protected $transport;
  22.     protected $instantFlush false;
  23.     public function setTransport(\Swift_Transport $transport)
  24.     {
  25.         $this->transport $transport;
  26.     }
  27.     /**
  28.      * After the kernel has been terminated we will always flush messages.
  29.      */
  30.     public function onKernelTerminate(PostResponseEvent $event)
  31.     {
  32.         $this->instantFlush true;
  33.     }
  34.     /**
  35.      * After the CLI application has been terminated we will always flush messages.
  36.      */
  37.     public function onCliTerminate(ConsoleTerminateEvent $event)
  38.     {
  39.         $this->instantFlush true;
  40.     }
  41.     /**
  42.      * {@inheritdoc}
  43.      */
  44.     protected function send($content, array $records)
  45.     {
  46.         parent::send($content$records);
  47.         if ($this->instantFlush) {
  48.             $this->flushMemorySpool();
  49.         }
  50.     }
  51.     /**
  52.      * {@inheritdoc}
  53.      */
  54.     public function reset()
  55.     {
  56.         $this->flushMemorySpool();
  57.     }
  58.     /**
  59.      * Flushes the mail queue if a memory spool is used.
  60.      */
  61.     private function flushMemorySpool()
  62.     {
  63.         $mailerTransport $this->mailer->getTransport();
  64.         if (!$mailerTransport instanceof \Swift_Transport_SpoolTransport) {
  65.             return;
  66.         }
  67.         $spool $mailerTransport->getSpool();
  68.         if (!$spool instanceof \Swift_MemorySpool) {
  69.             return;
  70.         }
  71.         if (null === $this->transport) {
  72.             throw new \Exception('No transport available to flush mail queue');
  73.         }
  74.         $spool->flushQueue($this->transport);
  75.     }
  76. }