Passed
Push — master ( f24ea2...036951 )
by Ivan
03:16
created

ItemExtension::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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