<?php
namespace App\Events;
use ApiPlatform\Core\EventListener\EventPriorities;
use App\Entity\PersCulture;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\ViewEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\Security\Core\Security;
class AddPersCultureSubscriber implements EventSubscriberInterface
{
private $security;
public function __construct(Security $security)
{
$this->security = $security;
}
public static function getSubscribedEvents()
{
return [
KernelEvents::VIEW => ['setEntrepriseForPersCulture',EventPriorities::PRE_VALIDATE]
];
}
public function setEntrepriseForPersCulture(ViewEvent $event){
$result = $event->getControllerResult();
$method = $event->getRequest()->getMethod();
if($result instanceof PersCulture && $method == 'POST'){
$entreprise = $this->security->getUser()->getEntreprise();
$result->setEntreprise($entreprise);
}
}
}