Passed
Push — master ( 9aaa0a...36b4d4 )
by Ivan
02:26
created

NavigationExtension.php$0 ➔ renderBreadcrumbs()   B

Complexity

Conditions 2

Size

Total Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

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