src/EventSubscriber/DeleteMediaObjectSubscriber.php line 33

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use ApiPlatform\Core\EventListener\EventPriorities;
  4. use App\Entity\Media;
  5. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  6. use Symfony\Component\HttpFoundation\Request;
  7. use Symfony\Component\HttpKernel\Event\ViewEvent;
  8. use Symfony\Component\HttpKernel\KernelEvents;
  9. use Doctrine\ORM\EntityManagerInterface;
  10. final class DeleteMediaObjectSubscriber implements EventSubscriberInterface
  11. {
  12.     /**
  13.      * @var EntityManagerInterface
  14.      */
  15.     protected $em;
  16.     public function __construct(EntityManagerInterface $em)
  17.     {
  18.         $this->em $em;
  19.     }
  20.     public static function getSubscribedEvents()
  21.     {
  22.         return [
  23.             KernelEvents::VIEW => ['onDeleteAction'EventPriorities::PRE_WRITE]
  24.         ];
  25.     }
  26.     public function onDeleteAction(ViewEvent $event)
  27.     {
  28.         $object $event->getControllerResult();
  29.         $request $event->getRequest();
  30.         $method $request->getMethod();
  31.         if (!$object instanceof Media || Request::METHOD_DELETE !== $method) {
  32.             return;
  33.         }
  34.         $this->em->remove($object);
  35.     }
  36. }