src/EventSubscriber/ContactSubscriber.php line 57

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use ApiPlatform\Core\EventListener\EventPriorities;
  4. use App\Entity\Contact;
  5. use App\Entity\Gender;
  6. use App\Mailer\Contact\ContactMailerInterface;
  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. class ContactSubscriber implements EventSubscriberInterface
  12. {
  13.     private ContactMailerInterface $mailer;
  14.     /**
  15.      * @param ContactMailerInterface $mailer
  16.      */
  17.     public function __construct(ContactMailerInterface $mailer)
  18.     {
  19.         $this->mailer $mailer;
  20.     }
  21.     /**
  22.      * @return array[]
  23.      */
  24.     public static function getSubscribedEvents(): array
  25.     {
  26.         return [
  27.             KernelEvents::VIEW => [
  28.                 ['onPost'EventPriorities::POST_WRITE],
  29.                 ['prePersist'EventPriorities::PRE_WRITE]
  30.             ]
  31.         ];
  32.     }
  33.     /**
  34.      * @param ViewEvent $event
  35.      *
  36.      * @return void
  37.      */
  38.     public function onPost(ViewEvent $event): void
  39.     {
  40.         $contact $event->getControllerResult();
  41.         $method $event->getRequest()->getMethod();
  42.         if ($contact instanceof Contact && Request::METHOD_POST === $method && $contact->getRecommender() !== null) {
  43.                 $this->mailer->notifyRecommended($contact);
  44.         }
  45.     }
  46.     /**
  47.      * @param ViewEvent $event
  48.      * @return void
  49.      */
  50.     public function prePersist(ViewEvent $event): void
  51.     {
  52.         $contact $event->getControllerResult();
  53.         $isPutMethod $event->getRequest()->getMethod() === Request::METHOD_PUT;
  54.         $isPostMethod $event->getRequest()->getMethod() === Request::METHOD_POST;
  55.         if ($contact instanceof Contact && ($isPutMethod || $isPostMethod) && $contact->getIsPreoccupation()) {
  56.             if ($contact->isCompany()) {
  57.                 $contact->setIsPreoccupation(false);
  58.                 $contact->setProxyContact(null);
  59.             }
  60.         }
  61.     }
  62. }