src/EventSubscriber/TreatmentSubscriber.php line 65
<?phpnamespace App\EventSubscriber;use App\Entity\App\Treatment;use App\Event\TreatmentAbandonedEvent;use App\Event\TreatmentValidatedEvent;use App\Event\TreatmentRealisedEvent;use App\Event\TreatmentRequestModificationEvent;use App\Repository\App\TreatmentRepository;use App\Repository\Pmi\UserRepository;use Symfony\Bridge\Twig\Mime\NotificationEmail;use Symfony\Component\EventDispatcher\EventSubscriberInterface;use Symfony\Component\Mailer\MailerInterface;use Symfony\Component\Mime\Address;use Symfony\Component\Routing\Generator\UrlGeneratorInterface;use Symfony\Component\Routing\RouterInterface;class TreatmentSubscriber implements EventSubscriberInterface{public function __construct(private MailerInterface $mailer,private RouterInterface $router,private UserRepository $userRepository,private TreatmentRepository $treatmentRepository,){}/*** @return string[]*/public static function getSubscribedEvents(): array{return [TreatmentAbandonedEvent::NAME => 'onTreatmendAbandoned',TreatmentRequestModificationEvent::NAME => 'onTreatmentModification',TreatmentRealisedEvent::NAME => 'onTreatmentRealised',TreatmentValidatedEvent::NAME => 'onTreatmentValidated',];}public function onTreatmendAbandoned(TreatmentAbandonedEvent $event): void{$treatment = $event->getTreatment();if (!$treatment instanceof Treatment) {return;}if (!empty($treatment->getNonConformity()->getEmmeteurEmail())){$notification = (new NotificationEmail())->to(new Address($treatment->getNonConformity()->getEmmeteurEmail()))->subject('Le traitement '.$treatment->findNumber(). ' de la non-conformité '. $treatment->getNonConformity()->getId(). ' a été abandonné' )->content('<p>Bonjour,</p><p>Le traitement vient d\'être refusé par '.$treatment->getRefusedBy().' pour le motif suivant :</p><p>'.$treatment->getRefusalReason().'</p>')->markAsPublic();$this->mailer->send($notification);}}public function onTreatmentModification(TreatmentRequestModificationEvent $event): void{$treatment = $event->getTreatment();if (!$treatment instanceof Treatment) {return;}if (!empty($treatment->getNonConformity()->getEmmeteurEmail())){$notification = (new NotificationEmail())->to(new Address($treatment->getNonConformity()->getEmmeteurEmail()))->subject('Demande de modification du traitement ' . $treatment->findNumber() . ' de la non-conformité ' . $treatment->getNonConformity()->getId())->content('<p>Bonjour,</p><p>' . $treatment->getBackModificationBy() . ' vous demande de mettre à jour votre demande de traitement.</p><p>Vous pouvez accéder à la non conformité en cliquant sur le bouton suivant :</p><p></p>')->action('Voir la non-conformité', $this->router->generate('non_conformity_view', ['id' => $treatment->getNonConformity()->getId()], UrlGeneratorInterface::ABSOLUTE_URL))->markAsPublic();$this->mailer->send($notification);}}public function onTreatmentRealised(TreatmentRealisedEvent $event): void{$treatment = $event->getTreatment();$nc = $treatment->getNonConformity();if (!$treatment instanceof Treatment) {return;}foreach ($treatment->getPeopleConcerned() as $user) {$user = $this->userRepository->findOneBy(['name' => $user]);if (!empty($user->getEmail())) {$notification = (new NotificationEmail())->to(new Address($user->getEmail()))->subject('Le traitement de la non-conformité n°'. $treatment->getNonConformity()->getId(). ' a été réalisé' )->content('<p>Bonjour,</p><p>Le traitement vient d\'être réalisé</p>')->action('Voir la non-conformité', $this->router->generate('non_conformity_view', ['id' => $nc->getId()], UrlGeneratorInterface::ABSOLUTE_URL))->markAsPublic();$this->mailer->send($notification);}}$notification = (new NotificationEmail())->to('laurence-floux@itoh-denki.com','qualite@itoh-denki.com')->subject('Le traitement de la non-conformité n°'. $treatment->getNonConformity()->getId(). ' a été réalisé' )->content('<p>Bonjour,</p><p>Le traitement vient d\'être réalisé</p>')->action('Voir la non-conformité', $this->router->generate('non_conformity_view', ['id' => $nc->getId()], UrlGeneratorInterface::ABSOLUTE_URL))->markAsPublic();$this->mailer->send($notification);}public function onTreatmentValidated(TreatmentValidatedEvent $event): void{$treatmentId = $event->getTreatmentId();$treatment = $this->treatmentRepository->find($treatmentId);if (!$treatment instanceof Treatment) {return;}$nc = $treatment->getNonConformity();// Envoyer un email à chaque personne concernéeforeach ($treatment->getPeopleConcerned() as $userName) {$user = $this->userRepository->findOneBy(['name' => $userName]);if ($user && !empty($user->getEmail())) {$notification = (new NotificationEmail())->to(new Address($user->getEmail()))->subject('Nouveau traitement pour la non-conformité n°' . $nc->getId())->content('<p>Bonjour,</p><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><p>Vous pouvez consulter les détails en cliquant sur le bouton ci-dessous :</p>')->action('Voir la non-conformité', $this->router->generate('non_conformity_view', ['id' => $nc->getId()], UrlGeneratorInterface::ABSOLUTE_URL))->markAsPublic();$this->mailer->send($notification);}}}}