vendor/can/rest/src/Can/RestBundle/EventListener/FetchListener.php line 51

Open in your IDE?
  1. <?php
  2. namespace Can\RestBundle\EventListener;
  3. use Can\RestBundle\CanRestBundle;
  4. use Can\RestBundle\Fetch\Fetch;
  5. use Can\RestBundle\Fetch\FetchConfiguration;
  6. use Can\RestBundle\Fetch\FetchHandlerInterface;
  7. use Can\RestBundle\Fetch\FetchHeader;
  8. use Symfony\Component\HttpFoundation\Request;
  9. use Symfony\Component\HttpFoundation\Response;
  10. use Symfony\Component\HttpKernel\Event\RequestEvent;
  11. /**
  12.  * Manages fetch headers.
  13.  *
  14.  * @group fetch
  15.  *
  16.  * @package can/rest-bundle
  17.  * @author lechecacharro <lechecacharro@gmail.com>
  18.  */
  19. class FetchListener
  20. {
  21.     /**
  22.      * @var FetchConfiguration
  23.      */
  24.     private $configuration;
  25.     /**
  26.      * @var FetchHandlerInterface
  27.      */
  28.     private $handler;
  29.     /**
  30.      * FetchListener constructor.
  31.      *
  32.      * @param FetchConfiguration    $configuration
  33.      * @param FetchHandlerInterface $handler
  34.      */
  35.     public function __construct(FetchConfiguration $configurationFetchHandlerInterface $handler)
  36.     {
  37.         $this->configuration $configuration;
  38.         $this->handler $handler;
  39.     }
  40.     /**
  41.      * @param RequestEvent $event
  42.      */
  43.     public function onRequest(RequestEvent $event): void
  44.     {
  45.         if (! $this->configuration->isEnabled()) {
  46.             return;
  47.         }
  48.         $request $event->getRequest();
  49.         if (! $request->attributes->get(CanRestBundle::ATTR_SERVICE_ZONE)) {
  50.             return;
  51.         }
  52.         $fetch = new Fetch(
  53.             $this->getRequestHeader($requestFetchHeader::SEC_FETCH_DEST),
  54.             $this->getRequestHeader($requestFetchHeader::SEC_FETCH_MODE),
  55.             $this->getRequestHeader($requestFetchHeader::SEC_FETCH_SITE),
  56.             strtolower($this->getRequestHeader($requestFetchHeader::SEC_FETCH_USER)) === 'true'
  57.         );
  58.         $request->attributes->set(CanRestBundle::ATTR_FETCH$fetch);
  59.         if ($fetch->isEmpty()) {
  60.             return;
  61.         }
  62.         $fetchApproved $this->handler->handle($request$fetch);
  63.         if (! $fetchApproved) {
  64.             // Reject the request returning an error response, using
  65.             // the HTTP status code specified by the fetchErrorStatus
  66.             // configuration option (a client error)
  67.             $event->setResponse(new Response(''$this->configuration->getFetchErrorStatus()));
  68.         }
  69.         $request->attributes->set(CanRestBundle::ATTR_FETCH_APPROVED$fetchApproved);
  70.     }
  71.     /**
  72.      * @param Request $request
  73.      * @param string  $header
  74.      *
  75.      * @return string
  76.      */
  77.     protected function getRequestHeader(Request $requeststring $header): string
  78.     {
  79.         $value $request->headers->get($header);
  80.         if (is_array($value)) {
  81.             $value $value[0];
  82.         }
  83.         return strval($value);
  84.     }
  85. }