VersionListener   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
c 1
b 0
f 0
lcom 1
cbo 3
dl 0
loc 47
rs 10
ccs 17
cts 17
cp 1

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getVersion() 0 4 1
A setRegex() 0 4 1
A onKernelRequest() 0 15 3
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