src/EventSubscriber/DeleteCommitteeEventSubscriber.php line 33

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Entity\Committee;
  4. use App\Mybase\Services\Base\SBase;
  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 DeleteCommitteeEventSubscriber implements EventSubscriberInterface
  10. {
  11.     /**
  12.      * @var Security
  13.      */
  14.     private $security;
  15.     /**
  16.      * @var SBase
  17.      */
  18.     private $baseService;
  19.     public function __construct(Security $securitySBase $baseService)
  20.     {
  21.         $this->security $security;
  22.         $this->baseService $baseService;
  23.     }
  24.     /**
  25.      * @return void
  26.      */
  27.     public function onDelete(ViewEvent $event)
  28.     {
  29.         $object $event->getControllerResult();
  30.         $isDeleteCommittee $object instanceof Committee && $event->getRequest()->isMethod('DELETE');
  31.         if ($isDeleteCommittee && $object->getCreator() !== $this->security->getUser()) {
  32.             $event->setResponse($this->baseService->jsonResponseFailedCondition("L'utilisateur n'est pas le createur du comité"));
  33.         }
  34.     }
  35.     /**
  36.      * @return array<string, mixed>
  37.      */
  38.     public static function getSubscribedEvents(): array
  39.     {
  40.         return [
  41.             KernelEvents::VIEW => ['onDelete'64],
  42.         ];
  43.     }
  44. }