Passed
Pull Request — master (#12)
by
unknown
03:27
created

NavigationHelper::getLabel()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 3
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A NavigationHelper::isAncestor() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Everlution\NavigationBundle\Twig;
6
7
use Everlution\Navigation\Builder\MatcherInterface;
8
use Everlution\Navigation\Builder\NavigationBuilder;
9
use Everlution\Navigation\Builder\NoCurrentItemFoundException;
10
use Everlution\Navigation\ContainerInterface;
11
use Everlution\Navigation\FilteredContainer;
12
use Everlution\Navigation\FilteredContainerInterface;
13
use Everlution\Navigation\Item\ItemInterface;
14
use Everlution\Navigation\Registry;
15
use Everlution\Navigation\Url\CannotProvideUrlForItemException;
16
use Everlution\Navigation\Url\UrlProviderContainer;
17
use Everlution\NavigationBundle\Bridge\Item\TranslatableItemLabelInterface;
18
use Everlution\NavigationBundle\Bridge\NavigationAliasContainer;
19
use Symfony\Component\Translation\TranslatorInterface;
20
21
/**
22
 * Class NavigationHelper.
23
 *
24
 * @author Ivan Barlog <[email protected]>
25
 */
26
class NavigationHelper
27
{
28
    /** @var Registry */
29
    private $registry;
30
    /** @var NavigationAliasContainer */
31
    private $aliasContainer;
32
    /** @var MatcherInterface */
33
    private $matcher;
34
    /** @var NavigationBuilder[] */
35
    private $container = [];
36
37
    public function __construct(
38
        Registry $registry,
39
        NavigationAliasContainer $aliasContainer,
40
        MatcherInterface $matcher
41
    ) {
42
        $this->registry = $registry;
43
        $this->aliasContainer = $aliasContainer;
44
        $this->matcher = $matcher;
45
    }
46
47
    public function isCurrent(string $identifier, ItemInterface $item): bool
48
    {
49
        try {
50
            return $this->getNavigation($identifier)->getCurrent() === $item;
51
        } catch (NoCurrentItemFoundException $exception) {
52
            return false;
53
        }
54
    }
55
56
    public function isAncestor(string $identifier, ItemInterface $item): bool
57
    {
58
        return $this->getNavigation($identifier)->isAncestor($item);
59
    }
60
61
    public function getNavigation(string $navigation): NavigationBuilder
62
    {
63
        if (false === array_key_exists($navigation, $this->container)) {
64
            $container = $this->getContainer($navigation);
65
            $this->container[$navigation] = new NavigationBuilder($container, $this->matcher);
66
        }
67
68
        return $this->container[$navigation];
69
    }
70
71
    private function getContainer(string $navigation): ContainerInterface
72
    {
73
        $container = $this->registry->getContainer($this->aliasContainer->get($navigation));
74
75
        if ($container instanceof FilteredContainerInterface) {
76
            return new FilteredContainer($container);
77
        }
78
79
        return $container;
80
    }
81
}
82