vendor/drosalys/api-bundle/src/EventSubscriber/ActionResponseSubscriber.php line 61

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the drosalys/api-bundle package.
  4.  *
  5.  * (c) Benjamin Georgeault
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Drosalys\Bundle\ApiBundle\EventSubscriber;
  11. use Drosalys\Bundle\ApiBundle\Action\Action;
  12. use Drosalys\Bundle\ApiBundle\Action\Info\EventInfo;
  13. use Drosalys\Bundle\ApiBundle\Event\AbstractViewEvent;
  14. use Drosalys\Bundle\ApiBundle\Event\PostPersistEvent;
  15. use Drosalys\Bundle\ApiBundle\Event\PostBuildResponseEvent;
  16. use Drosalys\Bundle\ApiBundle\Event\PrePersistEvent;
  17. use Drosalys\Bundle\ApiBundle\Event\PreBuildResponseEvent;
  18. use Drosalys\Bundle\ApiBundle\Event\ReplaceBuildResponseEvent;
  19. use Drosalys\Bundle\ApiBundle\Event\ReplacePersistEvent;
  20. use Drosalys\Bundle\ApiBundle\Persister\PersisterHandler\PersisterHandlerInterface;
  21. use Drosalys\Bundle\ApiBundle\Request\ActionRequestTrait;
  22. use Drosalys\Bundle\ApiBundle\Response\ResponseHandler\ResponseHandlerInterface;
  23. use Psr\EventDispatcher\EventDispatcherInterface;
  24. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  25. use Symfony\Component\HttpFoundation\Request;
  26. use Symfony\Component\HttpFoundation\Response;
  27. use Symfony\Component\HttpKernel\Event\ViewEvent;
  28. use Symfony\Component\HttpKernel\KernelEvents;
  29. /**
  30.  * Class ActionResponseSubscriber
  31.  *
  32.  * @author Benjamin Georgeault
  33.  */
  34. class ActionResponseSubscriber implements EventSubscriberInterface
  35. {
  36.     use ActionRequestTrait;
  37.     /**
  38.      * ActionResponseSubscriber constructor.
  39.      * @param ResponseHandlerInterface[] $responseHandlers
  40.      * @param PersisterHandlerInterface[] $persisterHandlers
  41.      */
  42.     public function __construct(
  43.         private EventDispatcherInterface $dispatcher,
  44.         private iterable $responseHandlers,
  45.         private iterable $persisterHandlers,
  46.     ) {
  47.     }
  48.     public static function getSubscribedEvents(): array
  49.     {
  50.         return [
  51.             KernelEvents::VIEW => ['__invoke'63],
  52.         ];
  53.     }
  54.     public function __invoke(ViewEvent $event): void
  55.     {
  56.         if (null === $action $this->retrieveActionFromRequest($request $event->getRequest())) {
  57.             return;
  58.         }
  59.         $this->applyPersister($action$request$data $event->getControllerResult());
  60.         if (null !== $response $this->buildResponse($action$request$data)) {
  61.             $event->setResponse($response);
  62.         }
  63.     }
  64.     private function buildResponse(Action $actionRequest $requestmixed $data): ?Response
  65.     {
  66.         if ((null !== $info $action->getBuildResponseInfo()) && ($info->hasReplace())) {
  67.             return ($info->getReplace())(new ReplaceBuildResponseEvent($action$request$data));
  68.         }
  69.         foreach ($this->responseHandlers as $handler) {
  70.             if ($handler->support($action$request$data)) {
  71.                 if ((null !== $info $action->getBuildResponseInfo()) && ($info->hasPre())) {
  72.                     ($info->getPre())(new PreBuildResponseEvent($action$request$data));
  73.                 }
  74.                 $this->dispatcher->dispatch(new PreBuildResponseEvent($action$request$data));
  75.                 $response $handler->buildResponse($action$request$data);
  76.                 if ((null !== $info) && ($info->hasPost())) {
  77.                     ($info->getPost())(new PostBuildResponseEvent($action$request$data$response));
  78.                 }
  79.                 $this->dispatcher->dispatch(new PostBuildResponseEvent($action$request$data$response));
  80.                 return $response;
  81.             }
  82.         }
  83.         return null;
  84.     }
  85.     private function applyPersister(Action $actionRequest $requestmixed $data): void
  86.     {
  87.         if ($request->attributes->has(DeserializeActionControllerSubscriber::REQUEST_ERROR_KEY)) {
  88.             return;
  89.         }
  90.         if ((null !== $info $action->getPersistInfo()) && ($info->hasReplace())) {
  91.             ($info->getReplace())(new ReplacePersistEvent($action$request$data));
  92.             return;
  93.         }
  94.         if (null === $action->getDeserializeInfo() && $action->getMethod() !== 'DELETE') {
  95.             return;
  96.         }
  97.         foreach ($this->persisterHandlers as $handler) {
  98.             if ($handler->support($action$request$data)) {
  99.                 if ((null !== $info) && ($info->hasPre())) {
  100.                     ($info->getPre())(new PrePersistEvent($action$request$data));
  101.                 }
  102.                 $this->dispatcher->dispatch(new PrePersistEvent($action$request$data));
  103.                 $handler->persist($action$request$data);
  104.                 if ((null !== $info) && ($info->hasPost())) {
  105.                     ($info->getPost())(new PostPersistEvent($action$request$data));
  106.                 }
  107.                 $this->dispatcher->dispatch(new PostPersistEvent($action$request$data));
  108.                 break;
  109.             }
  110.         }
  111.     }
  112. }