src/EventSubscriber/ItemUserDeleteEventSubscriber.php line 32

  1. <?php
  2. declare(strict_types=1);
  3. namespace App\EventSubscriber;
  4. use ApiPlatform\Core\EventListener\EventPriorities;
  5. use App\Entity\ItemUser;
  6. use App\Service\ItemUser\ItemUserService;
  7. use Doctrine\DBAL\Exception;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. use Symfony\Component\HttpFoundation\Request;
  10. use Symfony\Component\HttpKernel\Event\ViewEvent;
  11. use Symfony\Component\HttpKernel\KernelEvents;
  12. final class ItemUserDeleteEventSubscriber implements EventSubscriberInterface
  13. {
  14.     public function __construct(private readonly ItemUserService $itemUserService)
  15.     {
  16.     }
  17.     public static function getSubscribedEvents(): array
  18.     {
  19.         return [
  20.             KernelEvents::VIEW => ['removeFromChildren'EventPriorities::PRE_WRITE]
  21.         ];
  22.     }
  23.     /**
  24.      * @throws Exception
  25.      */
  26.     public function removeFromChildren(ViewEvent $event): void
  27.     {
  28.         $entity $event->getControllerResult();
  29.         if (
  30.             !$entity instanceof ItemUser
  31.             ||
  32.             $event->getRequest()->getMethod() !== Request::METHOD_DELETE
  33.         ) {
  34.             return;
  35.         }
  36.         $this->itemUserService->remove($entity);
  37.     }
  38. }