vendor/can/rest/src/Can/RestBundle/EventListener/PreferenceListener.php line 53

Open in your IDE?
  1. <?php
  2. namespace Can\RestBundle\EventListener;
  3. use Can\RestBundle\CanRestBundle;
  4. use Can\RestBundle\Http\Header\Prefer;
  5. use Can\RestBundle\Preference\PreferenceConfiguration;
  6. use Can\RestBundle\Preference\PreferenceEventDispatcher;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use Symfony\Component\HttpKernel\Event\RequestEvent;
  9. use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
  10. use InvalidArgumentException;
  11. /**
  12.  * A listener which collects client preferences, specified through the the
  13.  * HTTP Prefer request header.
  14.  *
  15.  * @group preference
  16.  *
  17.  * @package can/rest-bundle
  18.  * @author lechecacharro <lechecacharro@gmail.com>
  19.  */
  20. class PreferenceListener
  21. {
  22.     /**
  23.      * @var PreferenceConfiguration
  24.      */
  25.     protected $configuration;
  26.     /**
  27.      * @var PreferenceEventDispatcher
  28.      */
  29.     protected $dispatcher;
  30.     /**
  31.      * PreferenceListener constructor.
  32.      *
  33.      * @param PreferenceConfiguration   $configuration
  34.      * @param PreferenceEventDispatcher $dispatcher
  35.      */
  36.     public function __construct(PreferenceConfiguration $configurationPreferenceEventDispatcher $dispatcher)
  37.     {
  38.         $this->configuration $configuration;
  39.         $this->dispatcher $dispatcher;
  40.     }
  41.     /**
  42.      * @param RequestEvent $event
  43.      */
  44.     public function onRequest(RequestEvent $event): void
  45.     {
  46.         // Skip processing if the component is not enabled
  47.         if (! $this->configuration->isEnabled()) {
  48.             return;
  49.         }
  50.         // Do process only the master request
  51.         if (! $event->isMasterRequest()) {
  52.             return;
  53.         }
  54.         $request $event->getRequest();
  55.         // Skip processing if not in the service zone
  56.         if (! $request->attributes->get(CanRestBundle::ATTR_SERVICE_ZONE)) {
  57.             return;
  58.         }
  59.         $this->setPreferences($request);
  60.     }
  61.     /**
  62.      * Sets the list of requested preferences via the HTTP Prefer header as the
  63.      * {@link CanRestBundle::ATTR_PREFERENCES} request attribute.
  64.      *
  65.      * @param Request $request the HTTP request
  66.      *
  67.      * @throws BadRequestHttpException if the Prefer header is invalid
  68.      */
  69.     protected function setPreferences(Request $request): void
  70.     {
  71.         try {
  72.             $preferenceList Prefer::parseHeader($request->headers->get('Prefer'));
  73.         } catch (InvalidArgumentException $e) {
  74.             throw new BadRequestHttpException('Invalid Prefer header'$e);
  75.         }
  76.         // Allow third parties to modify the list of requested preferences
  77.         $event $this->dispatcher->dispatchPreferencesRequested($preferenceList);
  78.         $request->attributes->set(CanRestBundle::ATTR_PREFERENCES$event->getPreferenceList());
  79.     }
  80. }