src/EventSubscriber/TreatmentSubscriber.php line 65

  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Entity\App\Treatment;
  4. use App\Event\TreatmentAbandonedEvent;
  5. use App\Event\TreatmentValidatedEvent;
  6. use App\Event\TreatmentRealisedEvent;
  7. use App\Event\TreatmentRequestModificationEvent;
  8. use App\Repository\App\TreatmentRepository;
  9. use App\Repository\Pmi\UserRepository;
  10. use Symfony\Bridge\Twig\Mime\NotificationEmail;
  11. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  12. use Symfony\Component\Mailer\MailerInterface;
  13. use Symfony\Component\Mime\Address;
  14. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  15. use Symfony\Component\Routing\RouterInterface;
  16. class TreatmentSubscriber implements EventSubscriberInterface
  17. {
  18.     public function __construct(
  19.         private MailerInterface $mailer,
  20.         private RouterInterface $router,
  21.         private UserRepository $userRepository,
  22.         private TreatmentRepository $treatmentRepository,
  23.     ){}
  24.     /**
  25.      * @return string[]
  26.      */
  27.     public static function getSubscribedEvents(): array
  28.     {
  29.         return [
  30.             TreatmentAbandonedEvent::NAME => 'onTreatmendAbandoned',
  31.             TreatmentRequestModificationEvent::NAME => 'onTreatmentModification',
  32.             TreatmentRealisedEvent::NAME => 'onTreatmentRealised',
  33.             TreatmentValidatedEvent::NAME => 'onTreatmentValidated',
  34.         ];
  35.     }
  36.     public function onTreatmendAbandoned(TreatmentAbandonedEvent $event): void
  37.     {
  38.         $treatment $event->getTreatment();
  39.         if (!$treatment instanceof Treatment) {
  40.             return;
  41.         }
  42.         if (!empty($treatment->getNonConformity()->getEmmeteurEmail())){
  43.             $notification = (new NotificationEmail())
  44.                 ->to(new Address($treatment->getNonConformity()->getEmmeteurEmail()))
  45.                 ->subject('Le traitement '.$treatment->findNumber(). ' de la non-conformité '$treatment->getNonConformity()->getId(). ' a été abandonné' )
  46.                 ->content('
  47.                 <p>Bonjour,</p>
  48.                 <p>Le traitement vient d\'être refusé par '.$treatment->getRefusedBy().'  pour le motif suivant :</p>
  49.                 <p>'.$treatment->getRefusalReason().'</p>
  50.             ')
  51.                 ->markAsPublic()
  52.             ;
  53.             $this->mailer->send($notification);
  54.         }
  55.     }
  56.     public function onTreatmentModification(TreatmentRequestModificationEvent $event): void
  57.     {
  58.         $treatment $event->getTreatment();
  59.         if (!$treatment instanceof Treatment) {
  60.             return;
  61.         }
  62.         if (!empty($treatment->getNonConformity()->getEmmeteurEmail())){
  63.             $notification = (new NotificationEmail())
  64.                 ->to(new Address($treatment->getNonConformity()->getEmmeteurEmail()))
  65.                 ->subject('Demande de modification du traitement ' $treatment->findNumber() . ' de la non-conformité ' $treatment->getNonConformity()->getId())
  66.                 ->content('
  67.                 <p>Bonjour,</p>
  68.                 <p>' $treatment->getBackModificationBy() . ' vous demande de mettre à jour votre demande de traitement.</p>
  69.                 <p>Vous pouvez accéder à la non conformité en cliquant sur le bouton suivant :</p>
  70.                 <p></p>
  71.             ')
  72.                 ->action('Voir la non-conformité'$this->router->generate('non_conformity_view', ['id' => $treatment->getNonConformity()->getId()], UrlGeneratorInterface::ABSOLUTE_URL))
  73.                 ->markAsPublic();
  74.             $this->mailer->send($notification);
  75.         }
  76.     }
  77.     public function onTreatmentRealised(TreatmentRealisedEvent $event): void
  78.     {
  79.         $treatment $event->getTreatment();
  80.         $nc $treatment->getNonConformity();
  81.         if (!$treatment instanceof Treatment) {
  82.             return;
  83.         }
  84.         foreach ($treatment->getPeopleConcerned() as $user) {
  85.             $user $this->userRepository->findOneBy(['name' => $user]);
  86.             if (!empty($user->getEmail())) {
  87.                 $notification = (new NotificationEmail())
  88.                     ->to(new Address($user->getEmail()))
  89.                     ->subject('Le traitement de la non-conformité n°'$treatment->getNonConformity()->getId(). ' a été réalisé' )
  90.                     ->content('
  91.                 <p>Bonjour,</p>
  92.                 <p>Le traitement vient d\'être réalisé</p>
  93.             ')
  94.                     ->action('Voir la non-conformité'$this->router->generate('non_conformity_view', ['id' => $nc->getId()], UrlGeneratorInterface::ABSOLUTE_URL))
  95.                     ->markAsPublic()
  96.                 ;
  97.                 $this->mailer->send($notification);
  98.             }
  99.         }
  100.         $notification = (new NotificationEmail())
  101.             ->to('laurence-floux@itoh-denki.com','qualite@itoh-denki.com')
  102.             ->subject('Le traitement  de la non-conformité n°'$treatment->getNonConformity()->getId(). ' a été réalisé' )
  103.             ->content('
  104.                 <p>Bonjour,</p>
  105.                 <p>Le traitement vient d\'être réalisé</p>
  106.             ')
  107.             ->action('Voir la non-conformité'$this->router->generate('non_conformity_view', ['id' => $nc->getId()], UrlGeneratorInterface::ABSOLUTE_URL))
  108.             ->markAsPublic()
  109.         ;
  110.         $this->mailer->send($notification);
  111.     }
  112.     public function onTreatmentValidated(TreatmentValidatedEvent $event): void
  113.     {
  114.         $treatmentId $event->getTreatmentId();
  115.         $treatment $this->treatmentRepository->find($treatmentId);
  116.         if (!$treatment instanceof Treatment) {
  117.             return;
  118.         }
  119.         $nc $treatment->getNonConformity();
  120.         // Envoyer un email à chaque personne concernée
  121.         foreach ($treatment->getPeopleConcerned() as $userName) {
  122.             $user $this->userRepository->findOneBy(['name' => $userName]);
  123.             if ($user && !empty($user->getEmail())) {
  124.                 $notification = (new NotificationEmail())
  125.                     ->to(new Address($user->getEmail()))
  126.                     ->subject('Nouveau traitement pour la non-conformité n°' $nc->getId())
  127.                     ->content('
  128.                 <p>Bonjour,</p>
  129.                 <p>Un nouveau traitement a été validé pour la non-conformité n°' $nc->getId() . ' et vous êtes désigné(e) comme personne concernée.</p>
  130.                 <p>Vous pouvez consulter les détails en cliquant sur le bouton ci-dessous :</p>
  131.             ')
  132.                     ->action('Voir la non-conformité'$this->router->generate('non_conformity_view', ['id' => $nc->getId()], UrlGeneratorInterface::ABSOLUTE_URL))
  133.                     ->markAsPublic();
  134.                 $this->mailer->send($notification);
  135.             }
  136.         }
  137.     }
  138. }