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

NavigationContainerHelper::getUrl()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
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\ContainerRegistry;
0 ignored issues
show
Bug introduced by
The type Everlution\Navigation\ContainerRegistry 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...
9
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...
10
use Everlution\Navigation\ContainerBuilder\NavigationBuilder;
0 ignored issues
show
Bug introduced by
The type Everlution\Navigation\Co...ilder\NavigationBuilder 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 Everlution\Navigation\ContainerBuilder\NavigationContainerInterface;
0 ignored issues
show
Bug introduced by
The type Everlution\Navigation\Co...ationContainerInterface 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...
12
use Everlution\Navigation\Item\ItemInterface;
13
use Everlution\Navigation\Url\CannotProvideUrlForItemException;
14
use Everlution\Navigation\Url\UrlProviderContainer;
15
use Everlution\NavigationBundle\Bridge\Item\TranslatableItemLabelInterface;
16
use Everlution\NavigationBundle\Bridge\NavigationAliasContainer;
17
use Symfony\Component\Translation\TranslatorInterface;
18
19
/**
20
 * Class NavigationHelper.
21
 *
22
 * @author Ivan Barlog <[email protected]>
23
 */
24
class NavigationContainerHelper
25
{
26
    /** @var ContainerRegistry */
27
    private $registry;
28
    /** @var NavigationAliasContainer */
29
    private $aliasContainer;
30
    /** @var MatcherInterface */
31
    private $matcher;
32
    /** @var ContainerMatcherInterface */
33
    private $containerMatcher;
34
    /** @var NavigationBuilder[] */
35
    private $container = [];
36
    /** @var UrlProviderContainer */
37
    private $urlProviders;
38
    /** @var TranslatorInterface */
39
    private $translator;
40
41
    public function __construct(
42
        ContainerRegistry $registry,
43
        NavigationAliasContainer $aliasContainer,
44
        MatcherInterface $matcher,
45
        ContainerMatcherInterface $containerMatcher,
46
        UrlProviderContainer $container,
47
        TranslatorInterface $translator
48
    ) {
49
        $this->registry = $registry;
50
        $this->aliasContainer = $aliasContainer;
51
        $this->matcher = $matcher;
52
        $this->containerMatcher = $containerMatcher;
53
        $this->urlProviders = $container;
54
        $this->translator = $translator;
55
    }
56
57
    public function getLabel(ItemInterface $item, string $domain = null, string $locale = null): string
58
    {
59
        $label = $item->getLabel();
60
        $parameters = $label instanceof TranslatableItemLabelInterface ? $label->getParameters() : [];
61
62
        return $this->translator->trans($label->getValue(), $parameters, $domain, $locale);
63
    }
64
65
    public function isCurrent(ItemInterface $item): bool
66
    {
67
        return $this->matcher->isCurrent($item);
68
    }
69
70
    public function getUrl(ItemInterface $item): string
71
    {
72
        try {
73
            return $this->urlProviders->getUrl($item);
74
        } catch (CannotProvideUrlForItemException $exception) {
75
            return '#';
76
        }
77
    }
78
79
    public function getNavigation(string $navigation): NavigationBuilder
80
    {
81
        if (false === array_key_exists($navigation, $this->container)) {
82
            $container = $this->getContainer($navigation);
83
            $this->container[$navigation] = new NavigationBuilder($container, $this->containerMatcher);
84
        }
85
86
        return $this->container[$navigation];
87
    }
88
89
    private function getContainer(string $navigation): NavigationContainerInterface
90
    {
91
        return $this->registry->getContainer($this->aliasContainer->get($navigation));
92
    }
93
}
94