Passed
Pull Request — master (#22)
by
unknown
03:09
created

Matcher::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
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;
0 ignored issues
show
Bug introduced by
A parse error occurred: Syntax error, unexpected T_MATCH, expecting T_STRING or '{' on line 9 at column 26
Loading history...
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->getMasterRequest();
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