vendor/can/rest/src/Can/RestBundle/EventListener/RateLimitHeadersListener.php line 41

Open in your IDE?
  1. <?php
  2. namespace Can\RestBundle\EventListener;
  3. use Can\RestBundle\CanRestBundle;
  4. use Can\RestBundle\RateLimit\RateLimitConfiguration;
  5. use Can\RestBundle\RateLimit\RateLimitHeader;
  6. use Can\RestBundle\RateLimit\RateLimitInfo;
  7. use Symfony\Component\HttpKernel\Event\ResponseEvent;
  8. /**
  9.  * Adds the rate limit headers to the response.
  10.  *
  11.  * @group ratelimit
  12.  *
  13.  * @package can/rest-bundle
  14.  * @author lechecacharro <lechecacharro@gmail.com>
  15.  */
  16. class RateLimitHeadersListener
  17. {
  18.     /**
  19.      * @var RateLimitConfiguration
  20.      */
  21.     private $configuration;
  22.     /**
  23.      * RateLimitHeadersListener constructor.
  24.      *
  25.      * @param RateLimitConfiguration $configuration
  26.      */
  27.     public function __construct(RateLimitConfiguration $configuration)
  28.     {
  29.         $this->configuration $configuration;
  30.     }
  31.     /**
  32.      * @param ResponseEvent $event
  33.      */
  34.     public function onResponse(ResponseEvent $event): void
  35.     {
  36.         if (! $this->configuration->isEnabled()) {
  37.             return;
  38.         }
  39.         $request $event->getRequest();
  40.         if (! $request->attributes->get(CanRestBundle::ATTR_SERVICE_ZONE)) {
  41.             return;
  42.         }
  43.         /** @var RateLimitInfo $rateLimitInfo */
  44.         $rateLimitInfo $event->getRequest()->attributes->get(CanRestBundle::ATTR_RATE_LIMIT_INFOnull);
  45.         if (null === $rateLimitInfo) {
  46.             return;
  47.         }
  48.         $response $event->getResponse();
  49.         $response->headers->set($this->configuration->getLimitHeader(),     $rateLimitInfo->getLimit());
  50.         $response->headers->set($this->configuration->getRemainingHeader(), $rateLimitInfo->getRemaining());
  51.         $response->headers->set($this->configuration->getResetHeader(),     $rateLimitInfo->getWindowResetTime());
  52.         $response->headers->set($this->configuration->getWindowHeader(),    $rateLimitInfo->getWindowLength());
  53.         if ($this->configuration->isDebug()) {
  54.             $response->headers->set(RateLimitHeader::KEY,
  55.                 $request->attributes->get(CanRestBundle::ATTR_RATE_LIMIT_KEY)
  56.             );
  57.             if (null !== CanRestBundle::ATTR_RATE_LIMIT_CONSTRAINT) {
  58.                 $response->headers->set(RateLimitHeader::CONSTRAINT,
  59.                     $request->attributes->get(CanRestBundle::ATTR_RATE_LIMIT_CONSTRAINT)
  60.                 );
  61.             }
  62.             if (null !== CanRestBundle::ATTR_RATE_LIMIT_PLAN) {
  63.                 $response->headers->set(RateLimitHeader::PLAN,
  64.                     $request->attributes->get(CanRestBundle::ATTR_RATE_LIMIT_PLAN)
  65.                 );
  66.             }
  67.         }
  68.         if ($rateLimitInfo->isRateLimitExceeded()) {
  69.             $response->headers->set('Connection''close');
  70.         }
  71.     }
  72. }