Completed
Push — master ( 91e526...991fcc )
by Christian
02:43 queued 19s
created

VersionListener::onKernelRequest()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 5.0187

Importance

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