<?php
namespace App\Events;
use App\Entity\Committee;
use App\Mybase\Services\Base\SBase;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\HttpKernel\Event\ViewEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class DeleteCommitteeEventSubscriber implements EventSubscriberInterface
{
/**
* @var Security
*/
private $security;
/**
* @var SBase
*/
private $baseService;
public function __construct(Security $security, SBase $baseService)
{
$this->security = $security;
$this->baseService = $baseService;
}
/**
* @param ViewEvent $event
* @return void
*/
public function onDelete(ViewEvent $event)
{
$object = $event->getControllerResult();
$isDeleteCommittee = $object instanceof Committee && $event->getRequest()->isMethod("DELETE");
if ($isDeleteCommittee && $object->getCreator() !== $this->security->getUser()) {
$event->setResponse($this->baseService->jsonResponseFailedCondition("L'utilisateur n'est pas le createur du comité"));
}
}
/**
* @return array<string, mixed>
*/
public static function getSubscribedEvents(): array
{
return [
KernelEvents::VIEW => ['onDelete', 64],
];
}
}