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

VersionListener   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 93.75%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 4
dl 0
loc 32
ccs 15
cts 16
cp 0.9375
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A onKernelRequest() 0 20 5
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