Passed
Pull Request — master (#12)
by
unknown
06:01
created

AdvancedNavigationExtension::renderNavigation()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 3
dl 0
loc 11
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\NoCurrentItemFoundException;
8
use Twig\Environment;
9
10
/**
11
 * Class AdvancedNavigationExtension
12
 *
13
 * @author Martin Lutter <[email protected]>
14
 */
15
class AdvancedNavigationExtension extends \Twig_Extension
16
{
17
    /** @var NavigationHelper */
18
    private $navigationHelper;
19
    /** @var AdvancedNavigationHelper */
20
    private $containerHelper;
21
22
    public function __construct(NavigationHelper $navigationHelper, AdvancedNavigationHelper $containerHelper)
23
    {
24
        $this->navigationHelper = $navigationHelper;
25
        $this->containerHelper = $containerHelper;
26
    }
27
28
    /**
29
     * {@inheritdoc}
30
     */
31
    public function renderNavigation(
32
        Environment $environment,
33
        string $identifier,
34
        string $template = '@EverlutionNavigation/bootstrap_navigation.html.twig'
35
    ): string {
36
        return $environment->render(
37
            $template,
38
            [
39
                'root' => $this->containerHelper->getNavigation($identifier)->getCurrent(),
40
                'identifier' => $identifier,
41
                'helper' => $this->containerHelper,
42
            ]
43
        );
44
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49
    public function renderBreadcrumbs(
50
        Environment $environment,
51
        string $identifier,
52
        string $template = '@EverlutionNavigation/bootstrap_breadcrumbs.html.twig'
53
    ): string {
54
        try {
55
            $items = $this->navigationHelper->getNavigation($identifier)->getBreadcrumbs();
56
        } catch (NoCurrentItemFoundException $exception) {
57
            return 'missing breadcrumbs';
58
        }
59
60
        return $environment->render(
61
            $template,
62
            [
63
                'items' => $items,
64
                'identifier' => $identifier,
65
                'helper' => $this->navigationHelper,
66
            ]
67
        );
68
    }
69
70
    public function getFunctions()
71
    {
72
        return [
73
            new \Twig_SimpleFunction(
74
                'render_navigation',
75
                [$this, 'renderNavigation'],
76
                ['needs_environment' => true, 'is_safe' => ['html']]
77
            ),
78
            new \Twig_SimpleFunction(
79
                'render_breadcrumbs',
80
                [$this, 'renderBreadcrumbs'],
81
                ['needs_environment' => true, 'is_safe' => ['html']]
82
            ),
83
        ];
84
    }
85
86
    public function getFilters()
87
    {
88
        return [
89
            new \Twig_SimpleFilter('url', [$this->navigationHelper, 'getUrl']),
90
        ];
91
    }
92
}
93