src/EventListener/AdminSubscriber.php line 28

Open in your IDE?
  1. <?php
  2. namespace App\EventListener;
  3. use App\Entity\Information;
  4. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  5. use Symfony\Component\EventDispatcher\GenericEvent;
  6. use Twig\Environment;
  7. class AdminSubscriber implements EventSubscriberInterface
  8. {
  9.     private $mailer;
  10.     private $twig;
  11.     public function __construct(\Swift_Mailer $mailerEnvironment $twig)
  12.     {
  13.         $this->mailer $mailer;
  14.         $this->twig $twig;
  15.     }
  16.     public static function getSubscribedEvents()
  17.     {
  18.         return array(
  19.             'easy_admin.post_persist' => array('sendInformationPdf'),
  20.         );
  21.     }
  22.     public function sendInformationPdf(GenericEvent $event)
  23.     {
  24.         $entity $event->getSubject();
  25.         if (!($entity instanceof Information)) {
  26.             return;
  27.         }
  28.         if (!is_null($entity->getPdf())) {
  29.             $message = (new \Swift_Message('Hello Email'))
  30.                 ->setFrom('send@example.com')
  31.                 ->setTo('maxime@logomotion.fr')
  32.                 ->setBody(
  33.                     $this->twig->render(
  34.                         'emails/information.html.twig',
  35.                         ['information' => $entity]
  36.                     ),
  37.                     'text/html'
  38.                 );
  39.             $this->mailer->send($message);
  40.         }
  41.         $event['entity'] = $entity;
  42.     }
  43. }