src/Adapter/Api/EventListener/ApiAuthorizationListener.php line 51

Open in your IDE?
  1. <?php
  2. /**
  3.  * Created by PhpStorm.
  4.  * User: matijajanc
  5.  * Date: 22/05/2018
  6.  * Time: 15:18
  7.  */
  8. namespace App\Adapter\Api\EventListener;
  9. use App\Adapter\Api\Annotations\ApiAuthorization;
  10. use App\Adapter\Api\Exception\ApiException;
  11. use App\Adapter\Api\Service\Validators\ApiValidator;
  12. use Doctrine\Common\Annotations\Reader;
  13. use Doctrine\Common\Util\ClassUtils;
  14. use Nelmio\ApiDocBundle\Controller\SwaggerUiController;
  15. use ReflectionClass;
  16. use ReflectionObject;
  17. use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  18. use Symfony\Component\HttpFoundation\Response;
  19. use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
  20. class ApiAuthorizationListener
  21. {
  22.     /**
  23.      * @var Reader
  24.      */
  25.     private $reader;
  26.     /**
  27.      * @var ApiValidator
  28.      */
  29.     private $validator;
  30.     /**
  31.      * ApiAuthorizationListener constructor.
  32.      * @param Reader $reader
  33.      * @param ApiValidator $validator
  34.      */
  35.     public function __construct(Reader $readerApiValidator $validator)
  36.     {
  37.         $this->reader $reader;
  38.         $this->validator $validator;
  39.     }
  40.     /**
  41.      * @param FilterControllerEvent $event
  42.      * @throws ApiException
  43.      * @throws \ReflectionException
  44.      */
  45.     public function onKernelController(FilterControllerEvent $event)
  46.     {
  47.         $controller $event->getController();
  48.         $request $event->getRequest();
  49.         if (!$controller instanceof SwaggerUiController) {
  50.             /** @var Controller $controllerObject */
  51.             list($controllerObject$methodName) = $controller;
  52.             if ($controllerObject instanceof Controller
  53.                 && $this->hasApiAuthorizationAnnotation($controllerObject$methodName)
  54.             ) {
  55.                 if (!$this->validator->validateUserAgent($request)) {
  56.                     throw new ApiException(['ERR-301''User agent is required'], Response::HTTP_UNAUTHORIZED);
  57.                 }
  58.             }
  59.         }
  60.     }
  61.     /**
  62.      * @param Controller $controllerObject
  63.      * @param $methodName
  64.      * @return bool
  65.      * @throws \ReflectionException
  66.      */
  67.     private function hasApiAuthorizationAnnotation(Controller $controllerObject$methodName)
  68.     {
  69.         $tokenAnnotation ApiAuthorization::class;
  70.         $hasAnnotation false;
  71.         $classAnnotation $this->reader->getClassAnnotation(
  72.             new ReflectionClass(ClassUtils::getClass($controllerObject)),
  73.             $tokenAnnotation
  74.         );
  75.         if ($classAnnotation) {
  76.             $hasAnnotation true;
  77.         }
  78.         $controllerReflectionObject = new ReflectionObject($controllerObject);
  79.         $reflectionMethod $controllerReflectionObject->getMethod($methodName);
  80.         $methodAnnotation $this->reader->getMethodAnnotation($reflectionMethod$tokenAnnotation);
  81.         if ($methodAnnotation) {
  82.             $hasAnnotation true;
  83.         }
  84.         return $hasAnnotation;
  85.     }
  86. }