<?php
namespace App\EventListener;
use App\Entity\Information;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\EventDispatcher\GenericEvent;
use Twig\Environment;
class AdminSubscriber implements EventSubscriberInterface
{
private $mailer;
private $twig;
public function __construct(\Swift_Mailer $mailer, Environment $twig)
{
$this->mailer = $mailer;
$this->twig = $twig;
}
public static function getSubscribedEvents()
{
return array(
'easy_admin.post_persist' => array('sendInformationPdf'),
);
}
public function sendInformationPdf(GenericEvent $event)
{
$entity = $event->getSubject();
if (!($entity instanceof Information)) {
return;
}
if (!is_null($entity->getPdf())) {
$message = (new \Swift_Message('Hello Email'))
->setFrom('send@example.com')
->setTo('maxime@logomotion.fr')
->setBody(
$this->twig->render(
'emails/information.html.twig',
['information' => $entity]
),
'text/html'
);
$this->mailer->send($message);
}
$event['entity'] = $entity;
}
}