src/EventListener/NotifySubscriber.php line 42

Open in your IDE?
  1. <?php
  2. namespace App\EventListener;
  3.  
  4. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  5. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  6. use Doctrine\ORM\EntityManagerInterface;
  7. use App\Utils\MailerService;
  8. use App\Entity\Notification;
  9. use App\Entity\NotificationTemplate;
  10. use App\Event\NotifyEvent;
  11. class NotifySubscriber implements EventSubscriberInterface
  12. {
  13.   private $mailer;
  14.   private $parameters;
  15.   private $em;
  16.   
  17.   public function __construct(
  18.     EntityManagerInterface  $em
  19.     ParameterBagInterface $parameters
  20.     MailerService $mailer
  21.     \Twig\Environment $templating
  22.   ) {
  23.       $this->mailer $mailer;
  24.       $this->parameters $parameters;
  25.       $this->em $em;
  26.       
  27.       $this->templating $templating;
  28.   }
  29.   public static function getSubscribedEvents(): array
  30.   {
  31.       return [
  32.         NotifyEvent::NAME => 'notify',
  33.       ];
  34.   }
  35.   public function notify($event) {
  36.     $recipients = [];
  37.         
  38.     $object $event->getObject();
  39.     $event $object['event'];
  40.     $vars $object['vars'];
  41.     $dossier $object['dossier'];
  42.     $notification_copy $this->parameters->get('notification_copy');
  43.     $entreprise $dossier->getEntreprise();
  44.     $raison_sociale $entreprise->getRaisonSociale();
  45.     $templates $this->em->getRepository(NotificationTemplate::class)->findByEvent($event);
  46.     foreach($templates as $template) {
  47.       $messageHtml $this->templating->render("emails\\" $template->getModele() . ".html.twig"$vars);
  48.       $messageTxt $this->templating->render("emails\\" $template->getModele() . ".txt.twig"$vars);
  49.       $messageSujet $this->templating->createTemplate($template->getSujet());
  50.       $messageSujet "[Mise en gestion][" .$raison_sociale "] " $messageSujet->render($vars);
  51.       $notification = new Notification();
  52.       $notification->setDossier($dossier);
  53.       $notification->setDestinataire($template->getDestinataire());
  54.       $notification->setSujet($messageSujet);
  55.       $notification->setCorps($messageHtml);
  56.       $notification->setEvent($event);
  57.       switch ($template->getDestinataire()) {
  58.         case Notification::DESTINATAIRE_ENTREPRISE:
  59.           $recipients[] = [
  60.             'entreprise' => $dossier->getEntreprise(),
  61.             'email' => $dossier->getEntreprise()->getEmail(),
  62.             'name' => $dossier->getEntreprise()->getContactName()
  63.           ];
  64.         break;
  65.         case Notification::DESTINATAIRE_COMMERCIAL:
  66.           if(!is_null($dossier->getEntreprise()->getCommercialEntreprise())) {
  67.             $commercial $dossier->getEntreprise()->getCommercialEntreprise();
  68.             $recipients[] = [
  69.               'entreprise' => $dossier->getEntreprise(),
  70.               'email' => $commercial->getEmail(),
  71.               'name' => $commercial->getPrenom() . " " $commercial->getNom()
  72.             ];
  73.           } else {
  74.             $recipients[] = null;
  75.           }
  76.           
  77.         break;
  78.       }
  79.       
  80.       $from = [
  81.         'email' => $this->parameters->get('recipient.platform.email'), 
  82.         'name' => $this->parameters->get('recipient.platform.name')
  83.       ];
  84.       
  85.       $copy = [
  86.         'email' => $this->parameters->get('recipient.copy.email'), 
  87.         'name' => $this->parameters->get('recipient.copy.name')
  88.       ];
  89.       
  90.       try {
  91.           $this->mailer->send($from$recipients$messageSujet " - Original"$messageHtml$messageTxt);
  92.       } catch (TransportExceptionInterface $e) {
  93.         $notification->setStatut(NOTIFICATION::STATUT_NOTSENT);
  94.         $this->em->persist($notification);
  95.         $this->em->flush();
  96.       }
  97.       if($notification_copy) {
  98.         $recipient_copy[] = [
  99.           'entreprise' => $entreprise,
  100.           'email' => $copy["email"],
  101.           'name' => $copy["name"]
  102.         ];
  103.         try {
  104.           $this->mailer->send($from$recipient_copy$messageSujet " - Copy collective"$messageHtml$messageTxt);
  105.         } catch (TransportExceptionInterface $e) {
  106.         }
  107.       }
  108.       $notification->setStatut(NOTIFICATION::STATUT_SENT);
  109.       $this->em->persist($notification);
  110.       $this->em->flush();
  111.     }
  112.   }
  113. }