src/EventSubscriber/PreReadSubscriber.php line 68

Open in your IDE?
  1. <?php
  2. /**
  3.  * Subscriber select tru
  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\Service\ManagerTru;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. use Symfony\Component\HttpKernel\Event\RequestEvent;
  15. use Symfony\Component\HttpKernel\KernelEvents;
  16. class PreReadSubscriber implements EventSubscriberInterface
  17. {
  18.     /**
  19.      * @var ManagerTru
  20.      */
  21.     private $managerTru;
  22.     /**
  23.      * PreReadSubscriber constructor
  24.      *
  25.      * @param ManagerTru $managerTru
  26.      */
  27.     public function __construct(ManagerTru $managerTru)
  28.     {
  29.         $this->managerTru $managerTru;
  30.     }
  31.     /**
  32.      * Returns an array of event names this subscriber wants to listen to.
  33.      *
  34.      * The array keys are event names and the value can be:
  35.      *
  36.      *  * The method name to call (priority defaults to 0)
  37.      *  * An array composed of the method name to call and the priority
  38.      *  * An array of arrays composed of the method names to call and respective
  39.      *    priorities, or 0 if unset
  40.      *
  41.      * For instance:
  42.      *
  43.      *  * ['eventName' => 'methodName']
  44.      *  * ['eventName' => ['methodName', $priority]]
  45.      *  * ['eventName' => [['methodName1', $priority], ['methodName2']]]
  46.      *
  47.      * @return array The event names to listen to
  48.      */
  49.     public static function getSubscribedEvents():array
  50.     {
  51.         return [
  52.             KernelEvents::REQUEST => ['onRead'EventPriorities::PRE_READ],
  53.         ];
  54.     }
  55.     /**
  56.      * Event request select tru
  57.      *
  58.      * @param RequestEvent $event
  59.      */
  60.     public function onRead(RequestEvent $event):void
  61.     {
  62.         $id $event->getRequest()->get('id_tru');
  63.         if (isset($id)) {
  64.             $id intval($id);
  65.             $tru $this->managerTru->findTru($id);
  66.             if (in_array($event->getRequest()->attributes->get('_route'), [
  67.                 'api_ref_groups_post_collection',
  68.                 'api_ref_definitions_post_collection',
  69.                 'api_cm_definitions_post_collection',
  70.                 'api_ref_definitions_delete_item',
  71.                 'api_ref_definitions_delete_item',
  72.                 'api_ref_groups_delete_item',
  73.             ])) {
  74.                 $this->managerTru->validTru($tru);
  75.             } else {
  76.                 $this->managerTru->exitsTru($tru);
  77.             }
  78.             $this->managerTru->select($tru->getDataSource());
  79.         }
  80.     }
  81. }