<?php
/**
* Subscriber select tru
*
* @package RMCS
* @author Vlad Shashkov <vlad.s@zimalab.com>
* @copyright 2014 - 2019 The Zimalab
*/
declare(strict_types=1);
namespace App\EventSubscriber;
use ApiPlatform\Core\EventListener\EventPriorities;
use App\Service\ManagerTru;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\KernelEvents;
class PreReadSubscriber implements EventSubscriberInterface
{
/**
* @var ManagerTru
*/
private $managerTru;
/**
* PreReadSubscriber constructor
*
* @param ManagerTru $managerTru
*/
public function __construct(ManagerTru $managerTru)
{
$this->managerTru = $managerTru;
}
/**
* Returns an array of event names this subscriber wants to listen to.
*
* The array keys are event names and the value can be:
*
* * The method name to call (priority defaults to 0)
* * An array composed of the method name to call and the priority
* * An array of arrays composed of the method names to call and respective
* priorities, or 0 if unset
*
* For instance:
*
* * ['eventName' => 'methodName']
* * ['eventName' => ['methodName', $priority]]
* * ['eventName' => [['methodName1', $priority], ['methodName2']]]
*
* @return array The event names to listen to
*/
public static function getSubscribedEvents():array
{
return [
KernelEvents::REQUEST => ['onRead', EventPriorities::PRE_READ],
];
}
/**
* Event request select tru
*
* @param RequestEvent $event
*/
public function onRead(RequestEvent $event):void
{
$id = $event->getRequest()->get('id_tru');
if (isset($id)) {
$id = intval($id);
$tru = $this->managerTru->findTru($id);
if (in_array($event->getRequest()->attributes->get('_route'), [
'api_ref_groups_post_collection',
'api_ref_definitions_post_collection',
'api_cm_definitions_post_collection',
'api_ref_definitions_delete_item',
'api_ref_definitions_delete_item',
'api_ref_groups_delete_item',
])) {
$this->managerTru->validTru($tru);
} else {
$this->managerTru->exitsTru($tru);
}
$this->managerTru->select($tru->getDataSource());
}
}
}