src/Events/DeleteCommitteeEventSubscriber.php line 34

Open in your IDE?
  1. <?php
  2. namespace App\Events;
  3. use App\Entity\Committee;
  4. use App\Mybase\Services\Base\SBase;
  5. use Symfony\Component\Security\Core\Security;
  6. use Symfony\Component\HttpKernel\KernelEvents;
  7. use Symfony\Component\HttpKernel\Event\ViewEvent;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  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.      * @param ViewEvent $event
  26.      * @return void
  27.      */
  28.     public function onDelete(ViewEvent $event)
  29.     {
  30.         $object $event->getControllerResult();
  31.         $isDeleteCommittee $object instanceof Committee && $event->getRequest()->isMethod("DELETE");
  32.         if ($isDeleteCommittee && $object->getCreator() !== $this->security->getUser()) {
  33.             $event->setResponse($this->baseService->jsonResponseFailedCondition("L'utilisateur n'est pas le createur du comité"));
  34.         }
  35.     }
  36.     /**
  37.      * @return array<string, mixed>
  38.      */
  39.     public static function getSubscribedEvents(): array
  40.     {
  41.         return [
  42.             KernelEvents::VIEW => ['onDelete'64],
  43.         ];
  44.     }
  45. }