src/EventSubscriber/MRVVeto/AppointmentMailSubscriber.php line 32

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