src/Events/AddPersCommuneSubscriber.php line 29

Open in your IDE?
  1. <?php
  2. namespace App\Events;
  3. use ApiPlatform\Core\EventListener\EventPriorities;
  4. use App\Entity\PersCommune;
  5. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  6. use Symfony\Component\HttpKernel\Event\ViewEvent;
  7. use Symfony\Component\HttpKernel\KernelEvents;
  8. use Symfony\Component\Security\Core\Security;
  9. class AddPersCommuneSubscriber implements EventSubscriberInterface
  10. {
  11.     private $security;
  12.     public function __construct(Security $security)
  13.     {
  14.         $this->security $security;
  15.     }
  16.     public static function getSubscribedEvents()
  17.     {
  18.         return [
  19.             KernelEvents::VIEW => ['setEntrepriseForPersCommune',EventPriorities::PRE_VALIDATE]
  20.         ];
  21.     }
  22.     public function setEntrepriseForPersCommune(ViewEvent $event){
  23.         $result $event->getControllerResult();
  24.         $method $event->getRequest()->getMethod();
  25.         if($result instanceof PersCommune && $method == 'POST'){
  26.             $entreprise $this->security->getUser()->getEntreprise();
  27.             $result->setEntreprise($entreprise);
  28.         }
  29.     }
  30. }