Matcher   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
c 1
b 0
f 0
dl 0
loc 27
rs 10
wmc 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A isCurrent() 0 14 5
A __construct() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Everlution\NavigationBundle\Bridge;
6
7
use Everlution\Navigation\Builder\MatcherInterface;
8
use Everlution\Navigation\Item\MatchableInterface;
9
use Everlution\Navigation\Match\UrlMatchVoterInterface;
10
use Symfony\Component\HttpFoundation\Request;
11
use Symfony\Component\HttpFoundation\RequestStack;
12
13
/**
14
 * Class Matcher.
15
 *
16
 * @author Ivan Barlog <[email protected]>
17
 */
18
class Matcher implements MatcherInterface
19
{
20
    /** @var UrlMatchVoterInterface */
21
    private $voter;
22
    /** @var Request */
23
    private $request;
24
25
    public function __construct(RequestStack $requestStack, UrlMatchVoterInterface $voter)
26
    {
27
        $this->request = $requestStack->getMainRequest();
28
        $this->voter = $voter;
29
    }
30
31
    public function isCurrent($item): bool
32
    {
33
        if (!$this->request || !$item instanceof MatchableInterface) {
34
            return false;
35
        }
36
37
        // we search matches within URL and route
38
        foreach ([$this->request->getPathInfo(), $this->request->get('_route', '')] as $url) {
39
            if ($this->voter->matches($url, $item)) {
40
                return true;
41
            }
42
        }
43
44
        return false;
45
    }
46
}
47