Passed
Pull Request — master (#12)
by
unknown
05:52
created

NavigationContainerMatcher   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 27
rs 10
c 0
b 0
f 0
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A isCurrent() 0 14 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Everlution\NavigationBundle\Bridge;
6
7
use Everlution\Navigation\ContainerInterface;
8
use Everlution\Navigation\Item\MatchableInterface;
9
use Everlution\Navigation\Match\UrlMatchVoterInterface;
10
use Everlution\Navigation\ContainerBuilder\ContainerMatcherInterface;
0 ignored issues
show
Bug introduced by
The type Everlution\Navigation\Co...ntainerMatcherInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
use Symfony\Component\HttpFoundation\Request;
12
use Symfony\Component\HttpFoundation\RequestStack;
13
14
/**
15
 * Class Matcher.
16
 *
17
 * @author Ivan Barlog <[email protected]>
18
 */
19
class NavigationContainerMatcher implements ContainerMatcherInterface
20
{
21
    /** @var UrlMatchVoterInterface */
22
    private $voter;
23
    /** @var Request */
24
    private $request;
25
26
    public function __construct(RequestStack $requestStack, UrlMatchVoterInterface $voter)
27
    {
28
        $this->request = $requestStack->getMasterRequest();
29
        $this->voter = $voter;
30
    }
31
32
    public function isCurrent(ContainerInterface $item): bool
33
    {
34
        if (!$item instanceof MatchableInterface) {
35
            return false;
36
        }
37
38
        // we search matches within URL and route
39
        foreach ([$this->request->getPathInfo(), $this->request->get('_route', '')] as $url) {
40
            if ($this->voter->matches($url, $item)) {
41
                return true;
42
            }
43
        }
44
45
        return false;
46
    }
47
}
48