src/EventSubscriber/UserMailSubscriber.php line 29

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use ApiPlatform\Core\EventListener\EventPriorities;
  4. use App\Entity\User;
  5. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  6. use Symfony\Component\HttpFoundation\Request;
  7. use Symfony\Component\HttpKernel\Event\ViewEvent;
  8. use Symfony\Component\HttpKernel\KernelEvents;
  9. use App\Service\BridgeSimpleMailer;
  10. final class UserMailSubscriber implements EventSubscriberInterface
  11. {
  12.     private $mailer;
  13.     public function __construct(BridgeSimpleMailer $mailer)
  14.     {
  15.         $this->mailer $mailer;
  16.     }
  17.     public static function getSubscribedEvents()
  18.     {
  19.         return [
  20.             KernelEvents::VIEW => ['sendMail'EventPriorities::POST_WRITE],
  21.         ];
  22.     }
  23.     public function sendMail(ViewEvent $event)
  24.     {
  25.         $user $event->getControllerResult();
  26.         $method $event->getRequest()->getMethod();
  27.         if (!$user instanceof User || Request::METHOD_POST !== $method) {
  28.             return;
  29.         }
  30.         $this->mailer->sendToDN('A new user has been added'sprintf('The user #%d has been added.'$user->getId()));
  31.     }
  32. }