vendor/symfony/monolog-bridge/Handler/SwiftMailerHandler.php line 39

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