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

VersionListener::onKernelRequest()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 5.0342

Importance

Changes 0
Metric Value
dl 0
loc 21
ccs 8
cts 9
cp 0.8889
rs 9.2728
c 0
b 0
f 0
cc 5
nc 5
nop 1
crap 5.0342
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