AbstractBreadcrumbTwigExtension   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 4
dl 0
loc 56
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A bootstrapBreadcrumb() 0 9 4
A bootstrapBreadcrumbs() 0 17 2
1
<?php
2
3
/*
4
 * This file is part of the bootstrap-bundle package.
5
 *
6
 * (c) 2018 WEBEWEB
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace WBW\Bundle\BootstrapBundle\Twig\Extension\Component;
13
14
use Twig\Environment;
15
use WBW\Bundle\BootstrapBundle\Twig\Extension\AbstractTwigExtension;
16
use WBW\Bundle\CoreBundle\Component\Translation\BaseTranslatorInterface;
17
use WBW\Bundle\CoreBundle\Navigation\AbstractNavigationNode;
18
use WBW\Bundle\CoreBundle\Navigation\NavigationTree;
19
use WBW\Bundle\CoreBundle\Navigation\NavigationTreeHelper;
20
use WBW\Bundle\CoreBundle\Service\TranslatorTrait;
21
22
/**
23
 * Abstract breadcrumb Twig extension.
24
 *
25
 * @author webeweb <https://github.com/webeweb/>
26
 * @package WBW\Bundle\BootstrapBundle\Twig\Extension\Component
27
 * @abstract
28
 */
29
abstract class AbstractBreadcrumbTwigExtension extends AbstractTwigExtension {
30
31
    use TranslatorTrait;
32
33
    /**
34
     * Constructor.
35
     *
36
     * @param Environment $twigEnvironment The Twig environment.
37
     * @param BaseTranslatorInterface $translator The translator.
38
     */
39
    public function __construct(Environment $twigEnvironment, $translator) {
40
        parent::__construct($twigEnvironment);
41
        $this->setTranslator($translator);
42
    }
43
44
    /**
45
     * Displays a Bootstrap breadcrumb.
46
     *
47
     * @param AbstractNavigationNode $node The node.
48
     * @param bool|null $last Last node ?.
49
     * @return string Returns the Bootstrap breadcrumb.
50
     */
51
    private function bootstrapBreadcrumb(AbstractNavigationNode $node, ?bool $last): string {
52
53
        $attributes = true === $node->getActive() && true === $last ? ["class" => "active"] : [];
54
55
        $content   = $this->getTranslator()->trans($node->getLabel());
56
        $innerHTML = true === $last ? $content : static::coreHTMLElement("a", $content, ["href" => $node->getUri()]);
57
58
        return static::coreHTMLElement("li", $innerHTML, $attributes);
59
    }
60
61
    /**
62
     * Displays a Bootstrap breadcrumbs.
63
     *
64
     * @param NavigationTree $tree The tree.
65
     * @return string Returns the Bootstrap breadcrumbs.
66
     */
67
    protected function bootstrapBreadcrumbs(NavigationTree $tree): string {
68
69
        $attributes = [];
70
71
        $attributes["class"] = ["breadcrumb"];
72
73
        $innerHTML = [];
74
75
        $nodes = NavigationTreeHelper::getBreadcrumbs($tree);
76
        $count = count($nodes);
77
78
        for ($i = 0; $i < $count; ++$i) {
79
            $innerHTML[] = $this->bootstrapBreadcrumb($nodes[$i], $count === $i + 1);
80
        }
81
82
        return static::coreHTMLElement("ol", "\n" . implode("\n", $innerHTML) . "\n", $attributes);
83
    }
84
}
85