src/EventSubscriber/NonConformitySubscriber.php line 84

  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Entity\App\NonConformity;
  4. use App\Event\NcApprovedEvent;
  5. use App\Event\NcClosedEvent;
  6. use App\Event\NcRefusedEvent;
  7. use App\Event\NcSubmittedEvent;
  8. use Symfony\Bridge\Twig\Mime\NotificationEmail;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. use Symfony\Component\Mailer\MailerInterface;
  11. use Symfony\Component\Mime\Address;
  12. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  13. use Symfony\Component\Routing\RouterInterface;
  14. class NonConformitySubscriber implements EventSubscriberInterface
  15. {
  16.     public function __construct(
  17.         private MailerInterface $mailer,
  18.         private RouterInterface $router,
  19.     ){}
  20.     /**
  21.      * @return string[]
  22.      */
  23.     public static function getSubscribedEvents(): array
  24.     {
  25.         return [
  26.             NcSubmittedEvent::NAME => 'onNcSubmitted',
  27.             NcApprovedEvent::NAME => 'onNcApproved',
  28.             NcRefusedEvent::NAME => 'onNcRefused',
  29.             NcClosedEvent::NAME => 'onNcClosed',
  30.         ];
  31.     }
  32.     /**
  33.      * Intercept the nc.submitted event
  34.      *
  35.      * Sends a notification to the SQSS department to let them know
  36.      * that a new NC has been created
  37.      *
  38.      * @param NcSubmittedEvent $event
  39.      *
  40.      * @return void
  41.      *
  42.      * @throws \Symfony\Component\Mailer\Exception\TransportExceptionInterface
  43.      */
  44.     public function onNcSubmitted(NcSubmittedEvent $event): void
  45.     {
  46.         $nc $event->getNonConformity();
  47.         if (!$nc instanceof NonConformity) {
  48.             return;
  49.         }
  50.         $notification = (new NotificationEmail())
  51.             ->to(new Address('qualite@itoh-denki.com'), new Address('laurence-floux@itoh-denki.com'))
  52.             ->subject('La non-conformité '.str_pad($nc->getId(), 60STR_PAD_LEFT).' a été soumise')
  53.             ->content('
  54.                 <p>Bonjour,</p>
  55.                 <p>Une nouvelle non-conformité vient d\'être ouverte par '.$nc->getEmetteur().'</p>
  56.                 <p>Vous pouvez la consulter en cliquant ici :</p>
  57.             ')
  58.             ->action('Voir la non-conformité'$this->router->generate('non_conformity_view', ['id' => $nc->getId()], UrlGeneratorInterface::ABSOLUTE_URL))
  59.             ->markAsPublic()
  60.         ;
  61.         $this->mailer->send($notification);
  62.     }
  63.     /**
  64.      * Intercept the nc.approved event
  65.      *
  66.      * Sends a notification to the NC sender to let him know
  67.      * that hist NC has been approved
  68.      *
  69.      * @param NcApprovedEvent $event
  70.      *
  71.      * @return void
  72.      *
  73.      * @throws \Symfony\Component\Mailer\Exception\TransportExceptionInterface
  74.      */
  75.     public function onNcApproved(NcApprovedEvent $event): void
  76.     {
  77.         $nc $event->getNonConformity();
  78.         if (!$nc instanceof NonConformity) {
  79.             return;
  80.         }
  81.         if (!empty($nc->getEmmeteurEmail())) {
  82.             $notification = (new NotificationEmail())
  83.                 ->to(new Address($nc->getEmmeteurEmail()))
  84.                 ->subject('Votre non-conformité '.str_pad($nc->getId(), 60STR_PAD_LEFT).' a été approuvée')
  85.                 ->content('
  86.                 <p>Bonjour,</p>
  87.                 <p>Votre non-conformité n°'.$nc->getId().' vient d\'être approuvée</p>
  88.                 <p>Vous pouvez la consulter en cliquant ici :</p>
  89.             ')
  90.                 ->action('Voir la non-conformité'$this->router->generate('non_conformity_view', ['id' => $nc->getId()], UrlGeneratorInterface::ABSOLUTE_URL))
  91.                 ->markAsPublic()
  92.             ;
  93.             $this->mailer->send($notification);
  94.         }
  95.     }
  96.     /**
  97.      * Intercept the nc.refused event
  98.      *
  99.      * Sends a notification to the NC sender to let him know
  100.      * that hist NC has been refused
  101.      *
  102.      * @param NcRefusedEvent $event
  103.      *
  104.      * @return void
  105.      *
  106.      * @throws \Symfony\Component\Mailer\Exception\TransportExceptionInterface
  107.      */
  108.     public function onNcRefused(NcRefusedEvent $event): void
  109.     {
  110.         $nc $event->getNonConformity();
  111.         if (!$nc instanceof NonConformity) {
  112.             return;
  113.         }
  114.         if (!empty($nc->getEmmeteurEmail())) {
  115.             $notification = (new NotificationEmail())
  116.                 ->to(new Address($nc->getEmmeteurEmail()))
  117.                 ->subject('Votre non-conformité '.str_pad($nc->getId(), 60STR_PAD_LEFT).' a été refusée')
  118.                 ->content('
  119.                 <p>Bonjour,</p>
  120.                 <p>Votre non-conformité n°'.$nc->getId().' vient d\'être refusée</p>
  121.                 <p>Voici la raison du refus :</p>
  122.                 <p>'.$nc->getRefuseNcMessage().'</p>
  123.             ')
  124.                 ->markAsPublic()
  125.             ;
  126.             $this->mailer->send($notification);
  127.         }
  128.     }
  129.     /**
  130.      * Intercept the nc.closed event
  131.      *
  132.      * Sends a notification to the NC sender to let him know
  133.      * that hist NC has been closed
  134.      *
  135.      * @param NcClosedEvent $event
  136.      *
  137.      * @return void
  138.      *
  139.      * @throws \Symfony\Component\Mailer\Exception\TransportExceptionInterface
  140.      */
  141.     public function onNcClosed(NcClosedEvent $event): void
  142.     {
  143.         $nc $event->getNonConformity();
  144.         if (!$nc instanceof NonConformity) {
  145.             return;
  146.         }
  147.         if (!empty($nc->getEmmeteurEmail())) {
  148.             $notification = (new NotificationEmail())
  149.                 ->to(new Address($nc->getEmmeteurEmail()))
  150.                 ->subject('Votre non-conformité '.str_pad($nc->getId(), 60STR_PAD_LEFT).' a été fermée')
  151.                 ->content('
  152.                 <p>Bonjour,</p>
  153.                 <p>Votre non-conformité n°'.$nc->getId().' vient d\'être fermée</p>
  154.             ')
  155.                 ->markAsPublic()
  156.             ;
  157.             $this->mailer->send($notification);
  158.         }
  159.     }
  160. }