Completed
Push — master ( 03ec6c...0df031 )
by
unknown
12:45 queued 02:44
created

VersionListener::onKernelRequest()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 5.0113

Importance

Changes 0
Metric Value
dl 0
loc 20
ccs 12
cts 13
cp 0.9231
rs 9.2888
c 0
b 0
f 0
cc 5
nc 5
nop 1
crap 5.0113
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\Version\VersionResolverInterface;
16
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
17
18
/**
19
 * @internal
20
 */
21
class VersionListener
22
{
23
    private $versionResolver;
24
    private $defaultVersion;
25
26
    public function __construct(VersionResolverInterface $versionResolver, $defaultVersion = null)
27
    {
28
        $this->versionResolver = $versionResolver;
29 8
        $this->defaultVersion = $defaultVersion;
30
    }
31 8
32 8
    public function onKernelRequest(GetResponseEvent $event)
33 8
    {
34 8
        $request = $event->getRequest();
35
36 8
        if (!$request->attributes->get(FOSRestBundle::ZONE_ATTRIBUTE, true)) {
37
            return;
38 8
        }
39
40 8
        $version = $this->versionResolver->resolve($request);
41 1
        if (false === $version && null !== $this->defaultVersion) {
42
            $version = $this->defaultVersion;
43
        }
44 7
45 7
        // Return if nothing to do
46
        if (false === $version) {
47 7
            return;
48 2
        }
49 2
50
        $request->attributes->set('version', $version);
51
    }
52
}
53