src/Events/AddPersCultureSubscriber.php line 29

Open in your IDE?
  1. <?php
  2. namespace App\Events;
  3. use ApiPlatform\Core\EventListener\EventPriorities;
  4. use App\Entity\PersCulture;
  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 AddPersCultureSubscriber 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 => ['setEntrepriseForPersCulture',EventPriorities::PRE_VALIDATE]
  20. ];
  21. }
  22. public function setEntrepriseForPersCulture(ViewEvent $event){
  23. $result = $event->getControllerResult();
  24. $method = $event->getRequest()->getMethod();
  25. if($result instanceof PersCulture && $method == 'POST'){
  26. $entreprise = $this->security->getUser()->getEntreprise();
  27. $result->setEntreprise($entreprise);
  28. }
  29. }
  30. }