src/EventSubscriber/TreatmentSubscriber.php line 61
<?php
namespace App\EventSubscriber;
use App\Entity\App\Treatment;
use App\Event\TreatmentAbandonedEvent;
use App\Event\TreatmentRealisedEvent;
use App\Event\TreatmentRequestModificationEvent;
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,
){}
/**
* @return string[]
*/
public static function getSubscribedEvents(): array
{
return [
TreatmentAbandonedEvent::NAME => 'onTreatmendAbandoned',
TreatmentRequestModificationEvent::NAME => 'onTreatmentModification',
TreatmentRealisedEvent::NAME => 'onTreatmentRealised',
];
}
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);
}
}