custom/plugins/PickwareErpStarter/vendor/pickware/validation-bundle/src/Subscriber/JsonValidationAnnotationSubscriber.php line 50

Open in your IDE?
  1. <?php
  2. /*
  3.  * Copyright (c) Pickware GmbH. All rights reserved.
  4.  * This file is part of software that is released under a proprietary license.
  5.  * You must not copy, modify, distribute, make publicly available, or execute
  6.  * its contents or parts thereof without express permission by the copyright
  7.  * holder, unless otherwise permitted by law.
  8.  */
  9. declare(strict_types=1);
  10. namespace Pickware\ValidationBundle\Subscriber;
  11. use Doctrine\Common\Annotations\AnnotationReader;
  12. use Pickware\ValidationBundle\Annotation\JsonValidation;
  13. use Pickware\ValidationBundle\JsonValidator;
  14. use Pickware\ValidationBundle\JsonValidatorException;
  15. use ReflectionClass;
  16. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  17. use Symfony\Component\HttpFoundation\Response;
  18. use Symfony\Component\HttpKernel\Event\ControllerEvent;
  19. use Symfony\Component\HttpKernel\KernelEvents;
  20. /**
  21.  * A subscriber that checks whether the executed controller method is annotated with @JsonValidation and executes
  22.  * a JSON validation for the request body if so.
  23.  */
  24. class JsonValidationAnnotationSubscriber implements EventSubscriberInterface
  25. {
  26.     private JsonValidator $jsonValidator;
  27.     private AnnotationReader $annotationReader;
  28.     public function __construct(JsonValidator $jsonValidator)
  29.     {
  30.         $this->jsonValidator $jsonValidator;
  31.         $this->annotationReader = new AnnotationReader();
  32.     }
  33.     public static function getSubscribedEvents(): array
  34.     {
  35.         return [
  36.             KernelEvents::CONTROLLER => [
  37.                 'onKernelController',
  38.                 // Use a low priority to ensure all other subscribers like authorization and context resolving did already run.
  39.                 -1000000,
  40.             ],
  41.         ];
  42.     }
  43.     public function onKernelController(ControllerEvent $event): void
  44.     {
  45.         if (!is_array($event->getController())) {
  46.             return;
  47.         }
  48.         // phpcs:ignore
  49.         [=> $controllerObject=> $method] = $event->getController();
  50.         $reflectionClass = new ReflectionClass($controllerObject);
  51.         $method $reflectionClass->getMethod($method);
  52.         $jsonValidationAnnotation $this->annotationReader->getMethodAnnotation($methodJsonValidation::class);
  53.         if (!$jsonValidationAnnotation) {
  54.             return;
  55.         }
  56.         $request $event->getRequest();
  57.         $jsonValidationSchemaFilePath dirname($reflectionClass->getFileName()) . '/' $jsonValidationAnnotation->schemaFilePath;
  58.         try {
  59.             $this->jsonValidator->validateJsonAgainstSchema($request->getContent(), $jsonValidationSchemaFilePath);
  60.         } catch (JsonValidatorException $exception) {
  61.             $response $exception->serializeToJsonApiError()->setStatus(Response::HTTP_BAD_REQUEST)->toJsonApiErrorResponse();
  62.             $event->setController(fn() => $response);
  63.         }
  64.     }
  65. }