src/EventListener/PostValidate/PostValidateListener.php line 36

Open in your IDE?
  1. <?php
  2. namespace App\EventListener\PostValidate;
  3. use ApiPlatform\Core\Metadata\Resource\ToggleableOperationAttributeTrait;
  4. use ApiPlatform\Core\Util\RequestAttributesExtractor;
  5. use ApiPlatform\Core\Validator\EventListener\ValidateListener;
  6. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. use Symfony\Component\HttpFoundation\Response;
  9. use Symfony\Component\HttpKernel\Event\ViewEvent;
  10. use Symfony\Component\HttpKernel\KernelEvents;
  11. class PostValidateListener implements EventSubscriberInterface
  12. {
  13.     use ToggleableOperationAttributeTrait;
  14.     const POST_VALIDATE_TRANSFORMATION 'POST_VALIDATE_TRANSFORMATION';
  15.     /**
  16.      * @var EventDispatcherInterface
  17.      */
  18.     private $eventDispatcher;
  19.     public function __construct(EventDispatcherInterface $eventDispatcher)
  20.     {
  21.         $this->eventDispatcher $eventDispatcher;
  22.     }
  23.     public static function getSubscribedEvents()
  24.     {
  25.         return [
  26.             KernelEvents::VIEW => 'onKernelView',
  27.         ];
  28.     }
  29.     public function onKernelView(ViewEvent $event)
  30.     {
  31.         $controllerResult $event->getControllerResult();
  32.         $request $event->getRequest();
  33.         if ($controllerResult instanceof Response
  34.             || $request->isMethodSafe()
  35.             || $request->isMethod('DELETE')
  36.             || !($attributes RequestAttributesExtractor::extractAttributes($request))
  37.             || !$attributes['receive']
  38.             || $this->isOperationAttributeDisabled($attributesValidateListener::OPERATION_ATTRIBUTE_KEY)
  39.         ) {
  40.             return;
  41.         }
  42.         $this->eventDispatcher->dispatch($eventself::POST_VALIDATE_TRANSFORMATION);
  43.     }
  44. }