VersionListener::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
ccs 3
cts 3
cp 1
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Pgs\RestfonyBundle\EventListener;
4
5
use FOS\RestBundle\View\ConfigurableViewHandlerInterface;
6
use FOS\RestBundle\View\ViewHandlerInterface;
7
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
8
9
class VersionListener
10
{
11
    private $viewHandler;
12
    private $regex;
13
    private $version = false;
14
15 1
    public function __construct(ViewHandlerInterface $viewHandler)
16
    {
17 1
        $this->viewHandler = $viewHandler;
18 1
    }
19
20
    /**
21
     * Gets the version.
22
     *
23
     * @return mixed
24
     */
25 1
    public function getVersion()
26
    {
27 1
        return $this->version;
28
    }
29
30
    /**
31
     * Sets the regex.
32
     *
33
     * @param string $regex
34
     */
35 1
    public function setRegex($regex)
36
    {
37 1
        $this->regex = $regex;
38 1
    }
39
40 1
    public function onKernelRequest(GetResponseEvent $event)
41
    {
42 1
        $request = $event->getRequest();
43
44 1
        $version = $request->request->get('version');
45
46 1
        if ($version) {
47 1
            $this->version = $version;
48 1
            $request->attributes->set('version', $this->version);
49
50 1
            if ($this->viewHandler instanceof ConfigurableViewHandlerInterface) {
51 1
                $this->viewHandler->setExclusionStrategyVersion($this->version);
52
            }
53
        }
54 1
    }
55
}
56