src/EventSubscriber/TreatmentSubscriber.php line 86

  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Entity\App\Treatment;
  4. use App\Event\TreatmentAbandonedEvent;
  5. use App\Event\TreatmentRealisedEvent;
  6. use App\Event\TreatmentRequestModificationEvent;
  7. use App\Repository\Pmi\UserRepository;
  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 TreatmentSubscriber implements EventSubscriberInterface
  15. {
  16.     public function __construct(
  17.         private MailerInterface $mailer,
  18.         private RouterInterface $router,
  19.         private UserRepository $userRepository,
  20.     ){}
  21.     /**
  22.      * @return string[]
  23.      */
  24.     public static function getSubscribedEvents(): array
  25.     {
  26.         return [
  27.             TreatmentAbandonedEvent::NAME => 'onTreatmendAbandoned',
  28.             TreatmentRequestModificationEvent::NAME => 'onTreatmentModification',
  29.             TreatmentRealisedEvent::NAME => 'onTreatmentRealised',
  30.         ];
  31.     }
  32.     public function onTreatmendAbandoned(TreatmentAbandonedEvent $event): void
  33.     {
  34.         $treatment $event->getTreatment();
  35.         if (!$treatment instanceof Treatment) {
  36.             return;
  37.         }
  38.             if (!empty($treatment->getNonConformity()->getEmmeteurEmail())){
  39.                 $notification = (new NotificationEmail())
  40.                     ->to(new Address($treatment->getNonConformity()->getEmmeteurEmail()))
  41.                     ->subject('Le traitement '.$treatment->findNumber(). ' de la non-conformité '$treatment->getNonConformity()->getId(). ' a été abandonné' )
  42.                     ->content('
  43.                 <p>Bonjour,</p>
  44.                 <p>Le traitement vient d\'être refusé par '.$treatment->getRefusedBy().'  pour le motif suivant :</p>
  45.                 <p>'.$treatment->getRefusalReason().'</p>
  46.             ')
  47.                     ->markAsPublic()
  48.                 ;
  49.                 $this->mailer->send($notification);
  50.             }
  51.     }
  52.     public function onTreatmentModification(TreatmentRequestModificationEvent $event): void
  53.     {
  54.         $treatment $event->getTreatment();
  55.         if (!$treatment instanceof Treatment) {
  56.             return;
  57.         }
  58.            if (!empty($treatment->getNonConformity()->getEmmeteurEmail())){
  59.                $notification = (new NotificationEmail())
  60.                    ->to(new Address($treatment->getNonConformity()->getEmmeteurEmail()))
  61.                    ->subject('Demande de modification du traitement ' $treatment->findNumber() . ' de la non-conformité ' $treatment->getNonConformity()->getId())
  62.                    ->content('
  63.                 <p>Bonjour,</p>
  64.                 <p>' $treatment->getBackModificationBy() . ' vous demande de mettre à jour votre demande de traitement.</p>
  65.                 <p>Vous pouvez accéder à la non conformité en cliquant sur le bouton suivant :</p>
  66.                 <p></p>
  67.             ')
  68.                    ->action('Voir la non-conformité'$this->router->generate('non_conformity_view', ['id' => $treatment->getNonConformity()->getId()], UrlGeneratorInterface::ABSOLUTE_URL))
  69.                    ->markAsPublic();
  70.                $this->mailer->send($notification);
  71.            }
  72.     }
  73.     public function onTreatmentRealised(TreatmentRealisedEvent $event): void
  74.     {
  75.         $treatment $event->getTreatment();
  76.         $nc $treatment->getNonConformity();
  77.         if (!$treatment instanceof Treatment) {
  78.             return;
  79.         }
  80.         foreach ($treatment->getPeopleConcerned() as $user) {
  81.             $user $this->userRepository->findOneBy(['name' => $user]);
  82.             if (!empty($user->getEmail())) {
  83.                 $notification = (new NotificationEmail())
  84.                     ->to(new Address($user->getEmail()))
  85.                     ->subject('Le traitement de la non-conformité n°'$treatment->getNonConformity()->getId(). ' a été réalisé' )
  86.                     ->content('
  87.                 <p>Bonjour,</p>
  88.                 <p>Le traitement vient d\'être réalisé</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.         $notification = (new NotificationEmail())
  97.             ->to('laurence-floux@itoh-denki.com','qualite@itoh-denki.com')
  98.             ->subject('Le traitement  de la non-conformité n°'$treatment->getNonConformity()->getId(). ' a été réalisé' )
  99.             ->content('
  100.                 <p>Bonjour,</p>
  101.                 <p>Le traitement vient d\'être réalisé</p>
  102.             ')
  103.             ->action('Voir la non-conformité'$this->router->generate('non_conformity_view', ['id' => $nc->getId()], UrlGeneratorInterface::ABSOLUTE_URL))
  104.             ->markAsPublic()
  105.         ;
  106.         $this->mailer->send($notification);
  107.     }
  108. }