for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
<?php
/*
* This file is part of the ApiRateLimitBundle
*
* (c) Indra Gunawan <[email protected]>
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Indragunawan\ApiRateLimitBundle\EventListener;
use Symfony\Component\HttpKernel\Event\ResponseEvent;
/**
* @author Indra Gunawan <[email protected]>
class HeaderModificationListener
{
* @var array
private $header;
public function __construct(array $header)
$this->header = $header;
}
public function onKernelResponse(ResponseEvent $event)
if (false === $this->header['display']) {
return;
$request = $event->getRequest();
$rateLimitInfo = $request->attributes->get('_api_rate_limit_info', null);
if (null === $rateLimitInfo) {
$response = $event->getResponse();
$response->headers->set($this->header['names']['limit'], $rateLimitInfo['limit'] ?? 0);
$response->headers->set($this->header['names']['remaining'], $rateLimitInfo['remaining'] ?? 0);
$response->headers->set($this->header['names']['reset'], $rateLimitInfo['reset'] ?? 0);