ItemExtension   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 18
c 1
b 0
f 0
dl 0
loc 43
rs 10
wmc 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A getFunctions() 0 7 1
A renderItem() 0 13 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\NavigationBundle\Bridge\Item\Container\ItemContainerInterface;
9
use Twig\Environment;
10
use Twig\Extension\AbstractExtension;
11
use Twig\TwigFunction;
12
13
/**
14
 * Class ItemExtension.
15
 *
16
 * @author Ivan Barlog <[email protected]>
17
 */
18
class ItemExtension extends AbstractExtension
19
{
20
    /** @var NavigationHelper */
21
    private $helper;
22
    /** @var ItemContainerInterface */
23
    private $container;
24
    /** @var MatcherInterface */
25
    private $matcher;
26
27
    public function __construct(
28
        NavigationHelper $helper,
29
        ItemContainerInterface $container,
30
        MatcherInterface $matcher
31
    ) {
32
        $this->helper = $helper;
33
        $this->container = $container;
34
        $this->matcher = $matcher;
35
    }
36
37
    public function renderItem(
38
        Environment $environment,
39
        string $name,
40
        string $template = '@EverlutionNavigation/bootstrap_navigation_item.html.twig'
41
    ): string {
42
        $item = $this->container->get($name);
43
44
        return $environment->render(
45
            $template,
46
            [
47
                'item' => $item,
48
                'helper' => $this->helper,
49
                'is_active' => $this->matcher->isCurrent($item),
50
            ]
51
        );
52
    }
53
54
    public function getFunctions()
55
    {
56
        return [
57
            new TwigFunction(
58
                'render_item',
59
                [$this, 'renderItem'],
60
                ['needs_environment' => true, 'is_safe' => ['html']]
61
            ),
62
        ];
63
    }
64
}
65