Completed
Pull Request — master (#45)
by Laurent
04:03
created

RequestVoter   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 26
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 5
c 2
b 1
f 0
lcom 1
cbo 2
dl 0
loc 26
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A matchItem() 0 16 4
1
<?php
2
/**
3
 * RequestVoter KnpMenu.
4
 *
5
 * PHP Version 5
6
 *
7
 * @author     Quétier Laurent <[email protected]>
8
 * @copyright  2014 Dev-Int GLSR
9
 * @license    http://opensource.org/licenses/gpl-license.php GNU Public License
10
 *
11
 * @version    since 1.0.0
12
 *
13
 * @link       https://github.com/Dev-Int/glsr
14
 */
15
namespace AppBundle\Menu;
16
17
use Knp\Menu\ItemInterface;
18
use Knp\Menu\Matcher\Voter\VoterInterface;
19
use Symfony\Component\HttpFoundation\RequestStack;
20
21
/**
22
 * class RequestVoter.
23
 *
24
 * @category   Menu
25
 */
26
class RequestVoter implements VoterInterface
27
{
28
    private $requestStack;
29
    
30
    public function __construct(RequestStack $requestStack)
31
    {
32
        $this->requestStack = $requestStack;
33
    }
34
 
35
    public function matchItem(ItemInterface $item)
36
    {
37
        $request = $this->requestStack->getCurrentRequest();
38
 
39
        if ($item->getUri() === $request->getRequestUri()) {
40
            // URL's completely match
41
            return true;
42
        } elseif ($item->getUri() !== $request->getBaseUrl() . '/' &&
43
            substr($request->getRequestUri(), 0, strlen($item->getUri()))
44
            === $item->getUri()) {
45
            // URL isn't just "/" and the first part of the URL match
46
            return true;
47
        }
48
 
49
        return null;
50
    }
51
}
52