src/EventSubscriber/MRVVeto/AppointmentMailSubscriber.php line 35

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber\MRVVeto;
  3. use ApiPlatform\Core\EventListener\EventPriorities;
  4. use App\Entity\Asclepios\Appointment;
  5. use Doctrine\ORM\EntityManagerInterface;
  6. use Symfony\Bridge\Twig\Mime\TemplatedEmail;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. use Symfony\Component\HttpFoundation\Request;
  9. use Symfony\Component\HttpKernel\Event\ViewEvent;
  10. use Symfony\Component\HttpKernel\KernelEvents;
  11. use Symfony\Component\Mime\Email;
  12. use Symfony\Component\Mailer\MailerInterface;
  13. use Symfony\Component\Mailer\Bridge\Google\Smtp\GmailTransport;
  14. final class AppointmentMailSubscriber implements EventSubscriberInterface
  15. {
  16.     private MailerInterface $mailer;
  17.     private EntityManagerInterface $entityManager;
  18.     public function __construct(EntityManagerInterface $entityManagerMailerInterface $mailer)
  19.     {
  20.         $this->entityManager $entityManager;
  21.         $this->mailer $mailer;
  22.     }
  23.     public static function getSubscribedEvents()
  24.     {
  25.         return [
  26.             KernelEvents::VIEW => ['sendMail'EventPriorities::POST_WRITE],
  27.         ];
  28.     }
  29.     public function sendMail(ViewEvent $event): void
  30.     {
  31.         $appointment $event->getControllerResult();
  32.         $request $event->getRequest();
  33.         $method $request->getMethod();
  34.         if (!$appointment instanceof Appointment || Request::METHOD_POST !== $method) {
  35.             return;
  36.         }
  37.         $baseurl $request->getScheme() . '://' $request->getHttpHost() . $request->getBasePath();
  38.         $codeArgos = ($this->entityManager->getRepository('App:Asclepios\Customer')->findOneByEmail($appointment->getEmail()))?->getClinic()?->getArgosCode();
  39.         $message = (new TemplatedEmail())
  40.             ->from('nepasrepondre@clubvet.fr')
  41.             ->to($appointment->getEmail())
  42.             ->priority(Email::PRIORITY_HIGH)
  43.             ->subject('Confirmation de votre rendez-vous vétérinaire')
  44.             ->htmlTemplate('Apollon/Email/confirmationRDV.html.twig')
  45.             ->context([
  46.                 'base_url' => $baseurl,
  47.                 'name' => $appointment->getFirstname() . ' ' $appointment->getLastname(),
  48.                 'date_rdv' => $appointment->getStartAt(),
  49.                 'clinic' => $appointment->getClinic(),
  50.                 'veto' => $appointment->getVeterinary(),
  51.                 'espece' => $appointment->getAnimalBreed(),
  52.                 'pet_name' => $appointment->getPetName(),
  53.                 'type_rdv' => $appointment->getName(),
  54.                 'info_rdv' => (empty($appointment->getMoreInfo()) || $appointment->getMoreInfo() == "nul") ? "aucune" $appointment->getMoreInfo(),
  55.                 'isArgos' => (bool)$codeArgos,
  56.             ]);
  57.         $this->mailer->send($message);
  58.     }
  59. }