Passed
Pull Request — master (#13)
by
unknown
02:51
created

ItemHelper::__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\Twig;
6
7
use Everlution\Bridge\Symfony\Translator\TranslatorInterface;
0 ignored issues
show
Bug introduced by
The type Everlution\Bridge\Symfon...tor\TranslatorInterface 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...
8
use Everlution\Navigation\Item\ItemInterface;
9
use Everlution\Navigation\Url\CannotProvideUrlForItemException;
10
use Everlution\Navigation\Url\UrlProviderContainer;
11
use Everlution\NavigationBundle\Bridge\Item\TranslatableItemLabelInterface;
12
13
/**
14
 * Class Helper.
15
 *
16
 * @author Martin Lutter <[email protected]>
17
 */
18
class ItemHelper
19
{
20
    /** @var TranslatorInterface */
21
    private $translator;
22
    /** @var UrlProviderContainer */
23
    private $urlProvider;
24
25
    public function __construct(TranslatorInterface $translator, UrlProviderContainer $urlProvider)
26
    {
27
        $this->translator = $translator;
28
        $this->urlProvider = $urlProvider;
29
    }
30
31
    public function getLabel(ItemInterface $item, string $domain = null, string $locale = null): string
32
    {
33
        $label = $item->getLabel();
34
        $parameters = $label instanceof TranslatableItemLabelInterface ? $label->getParameters() : [];
35
36
        return $this->translator->trans($label->getValue(), $parameters, $domain, $locale);
37
    }
38
39
    public function getUrl(ItemInterface $item): string
40
    {
41
        try {
42
            return $this->urlProvider->getUrl($item);
43
        } catch (CannotProvideUrlForItemException $exception) {
44
            return '#';
45
        }
46
    }
47
}
48