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

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