vendor/sulu/sulu/src/Sulu/Bundle/MarkupBundle/Listener/MailerListener.php line 51

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of Sulu.
  4.  *
  5.  * (c) Sulu GmbH
  6.  *
  7.  * This source file is subject to the MIT license that is bundled
  8.  * with this source code in the file LICENSE.
  9.  */
  10. namespace Sulu\Bundle\MarkupBundle\Listener;
  11. use Sulu\Bundle\MarkupBundle\Markup\MarkupParserInterface;
  12. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  13. use Symfony\Component\HttpFoundation\RequestStack;
  14. use Symfony\Component\Mailer\Event\MessageEvent;
  15. use Symfony\Component\Mime\Email;
  16. class MailerListener implements EventSubscriberInterface
  17. {
  18.     /**
  19.      * @var MarkupParserInterface
  20.      */
  21.     private $markupParser;
  22.     /**
  23.      * @var RequestStack
  24.      */
  25.     private $requestStack;
  26.     /**
  27.      * @var string
  28.      */
  29.     private $defaultLocale;
  30.     public function __construct(MarkupParserInterface $markupParserRequestStack $requestStackstring $defaultLocale)
  31.     {
  32.         $this->markupParser $markupParser;
  33.         $this->requestStack $requestStack;
  34.         $this->defaultLocale $defaultLocale;
  35.     }
  36.     public static function getSubscribedEvents()
  37.     {
  38.         return [
  39.             MessageEvent::class => 'onMessage',
  40.         ];
  41.     }
  42.     public function onMessage(MessageEvent $event): void
  43.     {
  44.         $message $event->getMessage();
  45.         if (!$message instanceof Email) {
  46.             return;
  47.         }
  48.         $html $message->getHtmlBody();
  49.         if (!$html || !\is_string($html)) {
  50.             return;
  51.         }
  52.         $currentRequest $this->requestStack->getCurrentRequest();
  53.         if (null !== $currentRequest) {
  54.             $locale $currentRequest->getLocale();
  55.         } else {
  56.             $locale $this->defaultLocale;
  57.         }
  58.         $message->html($this->markupParser->parse($html$locale));
  59.     }
  60. }