Completed
Push — master ( 7d0ef3...14c63c )
by WEBEWEB
03:01
created

AbstractBreadcrumbTwigExtension   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

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

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A bootstrapBreadcrumb() 0 12 4
A bootstrapBreadcrumbs() 0 22 2
A getTranslator() 0 3 1
A setTranslator() 0 4 1
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 Symfony\Component\Translation\TranslatorInterface;
15
use WBW\Bundle\BootstrapBundle\Helper\NavigationTreeHelper;
16
use WBW\Bundle\BootstrapBundle\Navigation\NavigationNode;
17
use WBW\Bundle\BootstrapBundle\Navigation\NavigationTree;
18
use WBW\Bundle\BootstrapBundle\Twig\Extension\AbstractBootstrapTwigExtension;
19
20
/**
21
 * Abstract breadcrumb Twig extension.
22
 *
23
 * @author Camille A. <[email protected]>
24
 * @package WBW\Bundle\BootstrapBundle\Twig\Extension\Component
25
 * @abstract
26
 */
27
abstract class AbstractBreadcrumbTwigExtension extends AbstractBootstrapTwigExtension {
28
29
    /**
30
     * Translator.
31
     *
32
     * @var TranslatorInterface
33
     */
34
    private $translator;
35
36
    /**
37
     * Constructor.
38
     *
39
     * @param TranslatorInterface $translator The translator.
40
     */
41
    protected function __construct(TranslatorInterface $translator) {
42
        parent::__construct();
43
        $this->setTranslator($translator);
44
    }
45
46
    /**
47
     * Displays a Bootstrap breadcrumb.
48
     *
49
     * @param NavigationNode $node The navigation node.
50
     * @param boolean $last Last node ?.
51
     * @return string Returns the Bootstrap breadcrumb.
52
     */
53
    private function bootstrapBreadcrumb(NavigationNode $node, $last) {
54
55
        // Initialize the attributes.
56
        $attributes = true === $node->getActive() && true === $last ? ["class" => "active"] : [];
57
58
        // Initialize the parameters.
59
        $content   = $this->getTranslator()->trans($node->getId());
60
        $innerHTML = true === $last ? $content : self::bootstrapHTMLElement("a", $content, ["href" => $node->getRoute()]);
61
62
        // Return the HTML.
63
        return self::bootstrapHTMLElement("li", $innerHTML, $attributes);
64
    }
65
66
    /**
67
     * Displays a Bootstrap breadcrumbs.
68
     *
69
     * @param NavigationTree $tree The tree.
70
     * @return string Returns the Bootstrap breadcrumbs.
71
     */
72
    protected function bootstrapBreadcrumbs(NavigationTree $tree) {
73
74
        // Initialize the attributes.
75
        $attributes = [];
76
77
        $attributes["class"] = ["breadcrumb"];
78
79
        // Initialize the parameters.
80
        $innerHTML = [];
81
82
        // Get the breadcrumb node.
83
        $nodes = NavigationTreeHelper::getBreadcrumbs($tree);
84
        $count = count($nodes);
85
86
        // Handle each breadcrumb node.
87
        for ($i = 0; $i < $count; ++$i) {
88
            $innerHTML[] = $this->bootstrapBreadcrumb($nodes[$i], $count === $i + 1);
89
        }
90
91
        // Return the HTML.
92
        return self::bootstrapHTMLElement("ol", "\n" . implode("\n", $innerHTML) . "\n", $attributes);
93
    }
94
95
    /**
96
     * Get the translator.
97
     *
98
     * @return TranslatorInterface Returns the translator.
99
     */
100
    public function getTranslator() {
101
        return $this->translator;
102
    }
103
104
    /**
105
     * Set the translator.
106
     * @param TranslatorInterface $translator The translator.
107
     * @return AbstractComponentTwigExtension Returns this component Twig extension.
108
     */
109
    protected function setTranslator(TranslatorInterface $translator = null) {
110
        $this->translator = $translator;
111
        return $this;
112
    }
113
114
}
115