Passed
Pull Request — master (#2043)
by Tarmo
09:30
created

ResponseSubscriber::getSubscribedEvents()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 4
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 6
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
declare(strict_types = 1);
3
/**
4
 * /src/EventSubscriber/ResponseSubscriber.php
5
 *
6
 * @author TLe, Tarmo Leppänen <[email protected]>
7
 */
8
9
namespace App\EventSubscriber;
10
11
use App\Service\Version;
12
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
13
use Symfony\Component\HttpKernel\Event\ResponseEvent;
14
15
/**
16
 * Class ResponseSubscriber
17
 *
18
 * @package App\EventSubscriber
19
 * @author TLe, Tarmo Leppänen <[email protected]>
20
 */
21
class ResponseSubscriber implements EventSubscriberInterface
22
{
23 597
    public function __construct(
24
        private readonly Version $version,
25
    ) {
26
    }
27
28
    /**
29
     * {@inheritdoc}
30
     *
31
     * @return array<string, array<int, string|int>>
32
     */
33 1
    public static function getSubscribedEvents(): array
34
    {
35
        return [
36
            ResponseEvent::class => [
37 1
                'onKernelResponse',
38
                10,
39
            ],
40
        ];
41
    }
42
43
    /**
44
     * Subscriber method to attach API version to every response.
45
     */
46 597
    public function onKernelResponse(ResponseEvent $event): void
47
    {
48
        // Attach new header
49 597
        $event->getResponse()->headers->add([
50 597
            'X-API-VERSION' => $this->version->get(),
51
        ]);
52
    }
53
}
54