<?php
namespace App\EventListener;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Doctrine\ORM\EntityManagerInterface;
use App\Utils\MailerService;
use App\Entity\Notification;
use App\Entity\NotificationTemplate;
use App\Event\NotifyEvent;
class NotifySubscriber implements EventSubscriberInterface
{
private $mailer;
private $parameters;
private $em;
public function __construct(
EntityManagerInterface $em,
ParameterBagInterface $parameters,
MailerService $mailer,
\Twig\Environment $templating
) {
$this->mailer = $mailer;
$this->parameters = $parameters;
$this->em = $em;
$this->templating = $templating;
}
public static function getSubscribedEvents(): array
{
return [
NotifyEvent::NAME => 'notify',
];
}
public function notify($event) {
$recipients = [];
$object = $event->getObject();
$event = $object['event'];
$vars = $object['vars'];
$dossier = $object['dossier'];
$notification_copy = $this->parameters->get('notification_copy');
$entreprise = $dossier->getEntreprise();
$raison_sociale = $entreprise->getRaisonSociale();
$templates = $this->em->getRepository(NotificationTemplate::class)->findByEvent($event);
foreach($templates as $template) {
$messageHtml = $this->templating->render("emails\\" . $template->getModele() . ".html.twig", $vars);
$messageTxt = $this->templating->render("emails\\" . $template->getModele() . ".txt.twig", $vars);
$messageSujet = $this->templating->createTemplate($template->getSujet());
$messageSujet = "[Mise en gestion][" .$raison_sociale . "] " . $messageSujet->render($vars);
$notification = new Notification();
$notification->setDossier($dossier);
$notification->setDestinataire($template->getDestinataire());
$notification->setSujet($messageSujet);
$notification->setCorps($messageHtml);
$notification->setEvent($event);
switch ($template->getDestinataire()) {
case Notification::DESTINATAIRE_ENTREPRISE:
$recipients[] = [
'entreprise' => $dossier->getEntreprise(),
'email' => $dossier->getEntreprise()->getEmail(),
'name' => $dossier->getEntreprise()->getContactName()
];
break;
case Notification::DESTINATAIRE_COMMERCIAL:
if(!is_null($dossier->getEntreprise()->getCommercialEntreprise())) {
$commercial = $dossier->getEntreprise()->getCommercialEntreprise();
$recipients[] = [
'entreprise' => $dossier->getEntreprise(),
'email' => $commercial->getEmail(),
'name' => $commercial->getPrenom() . " " . $commercial->getNom()
];
} else {
$recipients[] = null;
}
break;
}
$from = [
'email' => $this->parameters->get('recipient.platform.email'),
'name' => $this->parameters->get('recipient.platform.name')
];
$copy = [
'email' => $this->parameters->get('recipient.copy.email'),
'name' => $this->parameters->get('recipient.copy.name')
];
try {
$this->mailer->send($from, $recipients, $messageSujet . " - Original", $messageHtml, $messageTxt);
} catch (TransportExceptionInterface $e) {
$notification->setStatut(NOTIFICATION::STATUT_NOTSENT);
$this->em->persist($notification);
$this->em->flush();
}
if($notification_copy) {
$recipient_copy[] = [
'entreprise' => $entreprise,
'email' => $copy["email"],
'name' => $copy["name"]
];
try {
$this->mailer->send($from, $recipient_copy, $messageSujet . " - Copy collective", $messageHtml, $messageTxt);
} catch (TransportExceptionInterface $e) {
}
}
$notification->setStatut(NOTIFICATION::STATUT_SENT);
$this->em->persist($notification);
$this->em->flush();
}
}
}