<?php
namespace App\EventSubscriber\MRVVeto;
use ApiPlatform\Core\EventListener\EventPriorities;
use App\Entity\Asclepios\Appointment;
use Symfony\Bridge\Twig\Mime\TemplatedEmail;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Event\ViewEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\Mime\Email;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Mailer\Bridge\Google\Smtp\GmailTransport;
final class AppointmentMailSubscriber implements EventSubscriberInterface
{
private MailerInterface $mailer;
public function __construct(MailerInterface $mailer)
{
$this->mailer = $mailer;
}
public static function getSubscribedEvents()
{
return [
KernelEvents::VIEW => ['sendMail', EventPriorities::POST_WRITE],
];
}
public function sendMail(ViewEvent $event): void
{
$appointment = $event->getControllerResult();
$request = $event->getRequest();
$method = $request->getMethod();
if (!$appointment instanceof Appointment || Request::METHOD_POST !== $method) {
return;
}
$baseurl = $request->getScheme() . '://' . $request->getHttpHost() . $request->getBasePath();
$message = (new TemplatedEmail())
->from('nepasrepondre@clubvet.fr')
->to($appointment->getEmail())
->priority(Email::PRIORITY_HIGH)
->subject('Confirmation de votre rendez-vous vétérinaire')
->htmlTemplate('Apollon/Email/confirmationRDV.html.twig')
->context([
'base_url' => $baseurl,
'name' => $appointment->getFirstname().' '.$appointment->getLastname(),
'date_rdv' => $appointment->getStartAt(),
'clinic' => $appointment->getClinic(),
'veto' => $appointment->getVeterinary(),
'espece' => $appointment->getAnimalBreed(),
'pet_name' => $appointment->getPetName(),
'type_rdv' => $appointment->getName(),
'info_rdv' => (empty($appointment->getMoreInfo()) || $appointment->getMoreInfo() == "nul") ? "aucune" : $appointment->getMoreInfo()
]);
$this->mailer->send($message);
}
}