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