vendor/noxlogic/ratelimit-bundle/Noxlogic/RateLimitBundle/EventListener/HeaderModificationListener.php line 22

Open in your IDE?
  1. <?php
  2. namespace Noxlogic\RateLimitBundle\EventListener;
  3. use Noxlogic\RateLimitBundle\Service\RateLimitInfo;
  4. use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
  5. class HeaderModificationListener extends BaseListener
  6. {
  7.     /**
  8.      * @param array $defaultParameters
  9.      */
  10.     public function __construct($defaultParameters = array())
  11.     {
  12.         $this->parameters $defaultParameters;
  13.     }
  14.     /**
  15.      * @param FilterResponseEvent $event
  16.      */
  17.     public function onKernelResponse(FilterResponseEvent $event)
  18.     {
  19.         $request $event->getRequest();
  20.         // Check if we have a rate-limit-info object in our request attributes. If not, we didn't need to limit.
  21.         $rateLimitInfo $request->attributes->get('rate_limit_info'null);
  22.         if (! $rateLimitInfo) {
  23.             return;
  24.         }
  25.         // Check if we need to add our x-rate-limits to the headers
  26.         if (! $this->getParameter('display_headers')) {
  27.             return;
  28.         }
  29.         /** @var RateLimitInfo $rateLimitInfo */
  30.         $remaining $rateLimitInfo->getLimit() - $rateLimitInfo->getCalls();
  31.         if ($remaining 0) {
  32.             $remaining 0;
  33.         }
  34.         $response $event->getResponse();
  35.         $response->headers->set($this->getParameter('header_limit_name'), $rateLimitInfo->getLimit());
  36.         $response->headers->set($this->getParameter('header_remaining_name'), $remaining);
  37.         $response->headers->set($this->getParameter('header_reset_name'), $rateLimitInfo->getResetTimestamp());
  38.     }
  39. }