src/EventSubscriber/RdvCreatedSubscriber.php line 34

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use ApiPlatform\Core\EventListener\EventPriorities;
  4. use App\Entity\Rdv;
  5. use App\Repository\UserRepository;
  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. final  class RdvCreatedSubscriber implements EventSubscriberInterface
  11. {
  12.     private $mailer;
  13.     private $userRepository;
  14.     public function __construct(\Swift_Mailer $mailer,UserRepository $userRepository)
  15.     {
  16.         $this->mailer $mailer;
  17.         $this->userRepository $userRepository;
  18.     }
  19.     public static function getSubscribedEvents()
  20.     {
  21.         return [
  22.             KernelEvents::VIEW => ['sendMail'EventPriorities::POST_WRITE],
  23.         ];
  24.     }
  25.     public function sendMail(ViewEvent $event): void
  26.     {
  27.         $rdv $event->getControllerResult();
  28.         $method $event->getRequest()->getMethod();
  29.         if (!$rdv instanceof Rdv || Request::METHOD_POST !== $method) {
  30.             return;
  31.         }
  32.         $admins $this->userRepository->findBy(['roles'=> ["ROLE_ADMIN"]]);
  33.         $emails = [];
  34.         foreach ($admins as $admin){
  35.             $emails[] = $admin->getEmail();
  36.         }
  37.         $message = (new \Swift_Message("Un nouveau Rendez-vous venant de l'application mobile E-lavandiere"))
  38.             ->setTo($emails)
  39.             ->setBody(sprintf('le client %s %s  souhaite prendre un rendez-vous le %s.'$rdv->getUser()->getFirstname(),$rdv->getUser()->getLastname(),$rdv->getCreatedAt()->format('d/m/Y')));
  40.         $this->mailer->send($message);
  41.     }
  42. }