<?php
declare(strict_types=1);
namespace App\Listener\Pos;
use App\Annotation\GetPosIdWithoutCinemaContext;
use App\Exception\RegionReflectionException;
use App\Subscriber\Pos\PosNumberSubscriber;
use Doctrine\Common\Annotations\Reader;
use Symfony\Component\HttpKernel\Event\ControllerEvent;
use ReflectionClass;
use ReflectionException;
class GetPosIdWithoutCinemaContextListener
{
private PosNumberSubscriber $posNumberSubscriber;
private Reader $annotationReader;
public function __construct(PosNumberSubscriber $posNumberSubscriber, Reader $annotationReader)
{
$this->posNumberSubscriber = $posNumberSubscriber;
$this->annotationReader = $annotationReader;
}
public function onKernelController(ControllerEvent $event): void
{
$controllers = $event->getController();
if (!is_array($controllers)) {
return;
}
$this->handleAnnotation($controllers);
}
private function handleAnnotation(iterable $controllers)
{
[$controller, $method] = $controllers;
try {
$controller = new ReflectionClass($controller);
$method = $controller->getMethod($method);
} catch (ReflectionException) {
throw new RegionReflectionException();
}
$annotation = $this->annotationReader->getMethodAnnotation($method, GetPosIdWithoutCinemaContext::class);
if ($annotation instanceof GetPosIdWithoutCinemaContext) {
$this->posNumberSubscriber->setSkip(false);
}
}
}