src/EventSubscriber/UserCreatedSubscriber.php line 47

Open in your IDE?
  1. <?php
  2. /**
  3.  * This file is part of an Innatial Developers development or collaboration.
  4.  *
  5.  * @author Innatial Developers, S.L.
  6.  * @link https://innatial.com
  7.  * @copyright Copyright (c) 2020. Innatial Developers, S.L.
  8.  *
  9.  * All rights are reserved.
  10.  * Copyright (c) 2020. Innatial Developers, S.L.
  11.  */
  12. namespace App\EventSubscriber;
  13. use App\Event\UserCreatedEvent;
  14. use Doctrine\ORM\EntityManager;
  15. use Doctrine\ORM\EntityManagerInterface;
  16. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  17. use Symfony\Component\Templating\EngineInterface;
  18. use Symfony\Contracts\Translation\TranslatorInterface;
  19. class UserCreatedSubscriber implements EventSubscriberInterface{
  20.     protected $mailer;
  21.     protected $twig;
  22.     protected $translator;
  23.     protected $dm;
  24.     public function __construct(\Swift_Mailer $mailerEngineInterface $twigTranslatorInterface $translatorEntityManagerInterface $dm)
  25.     {
  26.         $this->mailer $mailer;
  27.         $this->twig $twig;
  28.         $this->translator $translator;
  29.         $this->dm $dm;
  30.     }
  31.     /**
  32.      * @return array
  33.      * @throws \Exception
  34.      */
  35.     public static function getSubscribedEvents()
  36.     {
  37.         return [
  38.             UserCreatedEvent::NAME => 'onUserCreatedEvent'
  39.         ];
  40.     }
  41.     public function onUserCreatedEvent(UserCreatedEvent $event){
  42.         $user $event->getUser();
  43.         $message = (new \Swift_Message($this->translator->trans('auth.mailer.templates.user_new.subject')))
  44.             ->setTo($user->getEmail())
  45.             ->setBody(
  46.                 $this->twig->render(
  47.                     'mailer/user_new.html.twig',
  48.                     ['user' => $user]
  49.                 ),
  50.                 'text/html'
  51.             );
  52.         $this->mailer->send($message);
  53.     }
  54. }