Completed
Push — 2.x ( 88f22a...8afee2 )
by Christian
18s queued 11s
created

VersionListener   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 94.12%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 4
dl 0
loc 40
ccs 16
cts 17
cp 0.9412
rs 10
c 0
b 0
f 0

2 Methods

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