HeaderModificationListener   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 13
c 1
b 0
f 0
dl 0
loc 28
ccs 14
cts 14
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A onKernelResponse() 0 16 3
1
<?php
2
3
/*
4
 * This file is part of the ApiRateLimitBundle
5
 *
6
 * (c) Indra Gunawan <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Indragunawan\ApiRateLimitBundle\EventListener;
13
14
use Symfony\Component\HttpKernel\Event\ResponseEvent;
15
16
/**
17
 * @author Indra Gunawan <[email protected]>
18
 */
19
class HeaderModificationListener
20
{
21
    /**
22
     * @var array
23
     */
24
    private $header;
25
26 3
    public function __construct(array $header)
27
    {
28 3
        $this->header = $header;
29 3
    }
30
31 3
    public function onKernelResponse(ResponseEvent $event)
32
    {
33 3
        if (false === $this->header['display']) {
34 1
            return;
35
        }
36
37 2
        $request = $event->getRequest();
38 2
        $rateLimitInfo = $request->attributes->get('_api_rate_limit_info', null);
39 2
        if (null === $rateLimitInfo) {
40 1
            return;
41
        }
42
43 1
        $response = $event->getResponse();
44 1
        $response->headers->set($this->header['names']['limit'], $rateLimitInfo['limit'] ?? 0);
45 1
        $response->headers->set($this->header['names']['remaining'], $rateLimitInfo['remaining'] ?? 0);
46 1
        $response->headers->set($this->header['names']['reset'], $rateLimitInfo['reset'] ?? 0);
47 1
    }
48
}
49