src/EventSubscriber/RemoveSiteSubscriber.php line 69

Open in your IDE?
  1. <?php
  2. /**
  3.  * Subscriber delete site
  4.  *
  5.  * @package RMCS
  6.  * @author Vlad Shashkov  <vlad.s@zimalab.com>
  7.  * @copyright 2014 - 2019 The Zimalab
  8.  */
  9. declare(strict_types=1);
  10. namespace App\EventSubscriber;
  11. use ApiPlatform\Core\EventListener\EventPriorities;
  12. use App\Entity\Main\{SiteTru};
  13. use Doctrine\Persistence\{ManagerRegistryObjectManager};
  14. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  15. use Symfony\Component\HttpFoundation\{RequestResponse};
  16. use Symfony\Component\HttpKernel\{Event\ViewEventException\HttpExceptionKernelEvents};
  17. class RemoveSiteSubscriber implements EventSubscriberInterface
  18. {
  19.     /**
  20.      * @var ObjectManager
  21.      */
  22.     private $em;
  23.     /**
  24.      * RemoveSiteSubscriber constructor.
  25.      *
  26.      * @param ManagerRegistry $registry
  27.      */
  28.     public function __construct(ManagerRegistry $registry)
  29.     {
  30.         $this->em $registry->getManager('default');
  31.     }
  32.     /**
  33.      * Returns an array of event names this subscriber wants to listen to.
  34.      *
  35.      * The array keys are event names and the value can be:
  36.      *
  37.      *  * The method name to call (priority defaults to 0)
  38.      *  * An array composed of the method name to call and the priority
  39.      *  * An array of arrays composed of the method names to call and respective
  40.      *    priorities, or 0 if unset
  41.      *
  42.      * For instance:
  43.      *
  44.      *  * ['eventName' => 'methodName']
  45.      *  * ['eventName' => ['methodName', $priority]]
  46.      *  * ['eventName' => [['methodName1', $priority], ['methodName2']]]
  47.      *
  48.      * @return array The event names to listen to
  49.      */
  50.     public static function getSubscribedEvents(): array
  51.     {
  52.         return [
  53.             KernelEvents::VIEW => ['onDelete'EventPriorities::PRE_VALIDATE],
  54.         ];
  55.     }
  56.     /**
  57.      * Event check is tied tru
  58.      *
  59.      * @param ViewEvent $event
  60.      */
  61.     public function onDelete(ViewEvent $event): void
  62.     {
  63.         $site $event->getControllerResult();
  64.         $request $event->getRequest();
  65.         if ($site instanceof Site && $event->getRequest()->getMethod() == Request::METHOD_DELETE) {
  66.             $id $request->get('id');
  67.             $tru $this->em
  68.                 ->getRepository(Tru::class)
  69.                 ->findOneBy(['site' => $id]);
  70.             if ($tru) {
  71.                 throw new HttpException(Response::HTTP_UNPROCESSABLE_ENTITY'This site is tied to Tru');
  72.             }
  73.         }
  74.     }
  75. }