Passed
Push — master ( c2daa3...fa62ce )
by Julito
09:28 queued 10s
created

MenuVoter::matchItem()   B

Complexity

Conditions 7
Paths 5

Size

Total Lines 33
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 19
nc 5
nop 1
dl 0
loc 33
rs 8.8333
c 0
b 0
f 0
1
<?php
2
/* For licensing terms, see /license.txt */
3
4
namespace Chamilo\CoreBundle\Menu;
5
6
use Chamilo\FaqBundle\Entity\Category;
7
use Chamilo\PageBundle\Entity\Page;
8
use Chamilo\PageBundle\Entity\Site;
9
use Knp\Menu\FactoryInterface;
10
use Knp\Menu\ItemInterface;
11
use Knp\Menu\Matcher\Voter\VoterInterface;
12
use Symfony\Component\HttpFoundation\RequestStack;
13
14
/**
15
 * Class NavBuilder.
16
 *
17
 * @package Chamilo\CoreBundle\Menu
18
 */
19
class MenuVoter implements VoterInterface
20
{
21
    /**
22
     * @var RequestStack
23
     */
24
    private $requestStack;
25
26
    /**
27
     * @param RequestStack $requestStack
28
     */
29
    public function __construct(RequestStack $requestStack)
30
    {
31
        $this->requestStack = $requestStack;
32
    }
33
34
    /**
35
     * @param ItemInterface $item
36
     *
37
     * @return bool|null
38
     */
39
    public function matchItem(ItemInterface $item)
40
    {
41
        $request = $this->requestStack->getCurrentRequest();
42
        $currentUri = $item->getUri();
43
        $currentUrl = $request->getUri();
44
45
        if ($item->getExtra('routes') !== null &&
46
            in_array($request->attributes->get('_route'), $item->getExtra('routes'))
47
        ) {
48
            return true;
49
        }
50
51
        if (strpos($currentUri, 'user_portal.php') !== false &&
52
            strpos($currentUrl, 'user_portal.php') !== false
53
        ){
54
            return true;
55
        }
56
57
        if (strpos($currentUri, '/main/') !== false) {
58
            $pos = strpos($currentUri, '/main/');
59
            $partSelected = substr($currentUri, $pos);
60
            $partSelected = str_replace('/%2E%2E/', '', $partSelected);
61
62
            $pos = strpos($currentUrl, '/main/');
63
            $partCurrent = substr($currentUrl, $pos);
64
            $partCurrent = rtrim($partCurrent, '/');
65
66
            if ($partSelected === $partCurrent) {
67
                return true;
68
            }
69
        }
70
71
        return false;
72
    }
73
}
74