Completed
Pull Request — 2.x (#2138)
by Christian
03:15
created

VersionListener::onKernelRequest()   B

Complexity

Conditions 10
Paths 9

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 10.0454

Importance

Changes 0
Metric Value
dl 0
loc 25
ccs 12
cts 13
cp 0.9231
rs 7.6666
c 0
b 0
f 0
cc 10
nc 9
nop 1
crap 10.0454

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\ChainVersionResolver;
16
use FOS\RestBundle\Version\VersionResolverInterface;
17
use Symfony\Component\HttpKernel\Event\RequestEvent;
18
19
/**
20
 * @internal
21
 */
22
class VersionListener
23
{
24
    private $versionResolver;
25
    private $defaultVersion;
26
27 9
    public function __construct(VersionResolverInterface $versionResolver, $defaultVersion = null)
28
    {
29 9
        $this->versionResolver = $versionResolver;
30 9
        $this->defaultVersion = $defaultVersion;
31 9
    }
32
33
    /**
34
     * @param RequestEvent $event
35
     */
36 9
    public function onKernelRequest($event)
37
    {
38 9
        $request = $event->getRequest();
39
40 9
        if (!$request->attributes->get(FOSRestBundle::ZONE_ATTRIBUTE, true)) {
41 1
            return;
42
        }
43
44 8
        $version = $this->versionResolver->resolve($request);
45
46 8
        if (!$this->versionResolver instanceof ChainVersionResolver && null !== $version && !is_string($version)) {
47
            @trigger_error(sprintf('Not returning a string or null from %s::resolve() when implementing the %s is deprecated since FOSRestBundle 2.8.', get_class($this->versionResolver), VersionResolverInterface::class), E_USER_DEPRECATED);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
48
        }
49
50 8
        if ((false === $version || null === $version) && null !== $this->defaultVersion) {
51 3
            $version = $this->defaultVersion;
52
        }
53
54
        // Return if nothing to do
55 8
        if (false === $version || null === $version) {
56 1
            return;
57
        }
58
59 7
        $request->attributes->set('version', $version);
60 7
    }
61
}
62