Completed
Pull Request — 2.x (#2227)
by
unknown
08:36
created

VersionListener   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 92.31%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 4
dl 0
loc 33
ccs 12
cts 13
cp 0.9231
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A onKernelRequest() 0 21 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\RequestEvent;
17
18
/**
19
 * @internal
20
 */
21
class VersionListener
22
{
23
    private $versionResolver;
24
    private $defaultVersion;
25
26
    public function __construct(VersionResolverInterface $versionResolver, ?string $defaultVersion = null)
27 9
    {
28
        $this->versionResolver = $versionResolver;
29 9
        $this->defaultVersion = $defaultVersion;
30 9
    }
31 9
32
    public function onKernelRequest(RequestEvent $event): void
33
    {
34
        $request = $event->getRequest();
35
36 9
        if (!$request->attributes->get(FOSRestBundle::ZONE_ATTRIBUTE, true)) {
37
            return;
38 9
        }
39
40 9
        $version = $this->versionResolver->resolve($request);
41 1
42
        if (null === $version && null !== $this->defaultVersion) {
43
            $version = $this->defaultVersion;
44 8
        }
45
46 8
        // Return if nothing to do
47
        if (null === $version) {
48
            return;
49
        }
50 8
51 3
        $request->attributes->set('version', $version);
52
    }
53
}
54