src/EventListener/AuthAnnotationListener.php line 27

Open in your IDE?
  1. <?php declare(strict_types=1);
  2.      
  3. namespace App\EventListener;
  4.  
  5. use App\Annotation\Auth as AuthAnnotation;
  6. use App\ArgumentResolver\AuthValueResolver;
  7. use Doctrine\Common\Annotations\Reader;
  8. use ReflectionClass;
  9. use ReflectionException;
  10. use RuntimeException;
  11. use App\Tartarus;
  12. use Symfony\Component\HttpKernel\Event\{
  13.     ControllerEvent
  14.     ControllerArgumentsEvent
  15. };
  16. use Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadata;
  17.  
  18. class AuthAnnotationListener {
  19.     
  20.     private $annotationReader;
  21.     protected $annotation null;
  22.  
  23.     public function __construct(Reader $annotationReader) {
  24.         $this->annotationReader $annotationReader;
  25.     }
  26.  
  27.     public function onKernelController(ControllerEvent $event): void {
  28.         if (!$event->isMainRequest()) {
  29.             return;
  30.         }
  31.         $controllers $event->getController();
  32.         if (!is_array($controllers)) {
  33.             return;
  34.         }
  35.         $this->handleAnnotation($controllers);
  36.     }
  37.     
  38.     public function onKernelControllerArguments(ControllerArgumentsEvent $event): void {
  39.         if ((!$this->annotation) || (!$event->isMainRequest())) {
  40.             return;
  41.         }
  42.         $auth null;
  43.         foreach ($event->getArguments() as $argument) {
  44.             if ($argument instanceof Tartarus\AuthInterface) {
  45.                 $auth $argument;
  46.             }
  47.         }
  48.         if (!$auth) {
  49.             $argumentResolver = new AuthValueResolver();
  50.             $auth $argumentResolver->resolve(
  51.                 $event->getRequest(), 
  52.                 new ArgumentMetadata('auth'Tartarus\AuthInterface::class, falsefalsenull)
  53.             )->current();
  54.         }
  55.         if (!$auth) {
  56.             throw new RuntimeException('Auth is required, but missing value to resolve');
  57.         }
  58.         if ($this->annotation->required) {
  59.             $auth->check();
  60.         }
  61.         return;
  62.     }
  63.      
  64.     private function handleAnnotation(iterable $controllers): void {
  65.         list($controller$methodName) = $controllers;
  66.         try {
  67.             $controllerReflection = new ReflectionClass($controller);
  68.         } catch (ReflectionException $e) {
  69.             throw new RuntimeException('Failed to read annotation');
  70.         }
  71.         $this->handleMethodAnnotation($controllerReflection$methodName);
  72.     }
  73.  
  74.     private function handleMethodAnnotation(ReflectionClass $controllerstring $methodName): void {
  75.         $method $controller->getMethod($methodName);
  76.         /* Get activity annotation */
  77.         $annotation $this->annotationReader->getMethodAnnotation($methodAuthAnnotation::class);
  78.         if ($annotation instanceof AuthAnnotation) {
  79.             $this->annotation $annotation;
  80.         } 
  81.     }
  82. }