AllowedMethodsListener::onKernelResponse()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3.009

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 9
cts 10
cp 0.9
rs 9.7333
c 0
b 0
f 0
cc 3
nc 3
nop 1
crap 3.009
1
<?php
2
3
/*
4
 * This file is part of the FOSRestBundle package.
5
 *
6
 * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
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 FOS\RestBundle\EventListener;
13
14
use FOS\RestBundle\FOSRestBundle;
15
use FOS\RestBundle\Response\AllowedMethodsLoader\AllowedMethodsLoaderInterface;
16
use Symfony\Component\HttpKernel\Event\ResponseEvent;
17
18
/**
19
 * Listener to append Allow-ed methods for a given route/resource.
20
 *
21
 * @author Boris Guéry <[email protected]>
22
 *
23
 * @internal
24
 */
25
class AllowedMethodsListener
26
{
27
    private $loader;
28
29 1
    public function __construct(AllowedMethodsLoaderInterface $loader)
30
    {
31 1
        $this->loader = $loader;
32 1
    }
33
34 1
    public function onKernelResponse(ResponseEvent $event): void
35
    {
36 1
        $request = $event->getRequest();
37
38 1
        if (!$request->attributes->get(FOSRestBundle::ZONE_ATTRIBUTE, true)) {
39
            return;
40
        }
41
42 1
        $allowedMethods = $this->loader->getAllowedMethods();
43
44 1
        if (isset($allowedMethods[$event->getRequest()->get('_route')])) {
45 1
            $event->getResponse()
46 1
                ->headers
47 1
                ->set('Allow', implode(', ', $allowedMethods[$event->getRequest()->get('_route')]));
48
        }
49 1
    }
50
}
51