Completed
Push — sf2.7 ( 18de34...b252b6 )
by Laurent
18:26
created

RequestVoter   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A matchItem() 0 17 4
1
<?php
2
3
namespace AppBundle\Menu;
4
5
use Knp\Menu\ItemInterface;
6
use Knp\Menu\Matcher\Voter\VoterInterface;
7
use Symfony\Component\HttpFoundation\RequestStack;
8
9
class RequestVoter implements VoterInterface
10
{
11
    private $requestStack;
12
    
13
    public function __construct(RequestStack $requestStack)
14
    {
15
        $this->requestStack = $requestStack;
16
    }
17
 
18
    public function matchItem(ItemInterface $item)
19
    {
20
        $request = $this->requestStack->getCurrentRequest();
21
 
22
        if ($item->getUri() === $request->getRequestUri()) {
23
            // URL's completely match
24
            return true;
25
        } else if (
26
            $item->getUri() !== $request->getBaseUrl() . '/' &&
27
            substr($request->getRequestUri(), 0, strlen($item->getUri()))
28
            === $item->getUri()) {
29
            // URL isn't just "/" and the first part of the URL match
30
            return true;
31
        }
32
 
33
        return null;
34
    }
35
 
36
}