Completed
Push — master ( e0b4a9...f80d4c )
by WEBEWEB
02:32
created

AbstractMenuTwigExtension::translate()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 17
rs 9.4285
cc 3
eloc 8
nc 3
nop 1
1
<?php
2
3
/**
4
 * This file is part of the adminbsb-material-design-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\AdminBSBBundle\Twig\Extension\Menu;
13
14
use Symfony\Component\Translation\TranslatorInterface;
15
use WBW\Bundle\AdminBSBBundle\Twig\Extension\AbstractAdminBSBTwigExtension;
16
use WBW\Bundle\AdminBSBBundle\Twig\Extension\UI\IconUITwigExtension;
17
use WBW\Bundle\BootstrapBundle\Navigation\NavigationNode;
18
use WBW\Bundle\BootstrapBundle\Navigation\NavigationTree;
19
use WBW\Library\Core\Utility\Argument\StringUtility;
20
21
/**
22
 * Abstract multi level menu Twig extension.
23
 *
24
 * @author webeweb <https://github.com/webeweb/>
25
 * @package WBW\Bundle\AdminBSBBundle\Twig\Extension\Menu
26
 * @abstract
27
 */
28
abstract class AbstractMenuTwigExtension extends AbstractAdminBSBTwigExtension {
29
30
    /**
31
     * Translator;
32
     *
33
     * @var TranslatorInterface
34
     */
35
    protected $translator;
36
37
    /**
38
     * Constructor.
39
     *
40
     * @param TranslatorInterface $translator The translator.
41
     */
42
    protected function __construct(TranslatorInterface $translator) {
43
        parent::__construct();
44
        $this->translator = $translator;
45
    }
46
47
    /**
48
     * Displays an AdminBSB menu.
49
     *
50
     * @param NavigationTree $tree The tree.
51
     * @return string Returns the AdminBSB menu.
52
     */
53
    protected function adminBSBMenu(NavigationTree $tree) {
54
55
        // Initialize the template.
56
        $template = [];
57
58
        $template[] = $this->adminBSBMenuHeader($tree);
59
        foreach ($tree->getNodes() as $current) {
60
            if (false === $this->isValidNode($current)) {
61
                continue;
62
            }
63
            $template[] = $this->adminBSBMenuItem($current);
64
        }
65
66
        // Return the HTML.
67
        return implode("\n", $template);
68
    }
69
70
    /**
71
     * Displays an AdminBSB menu header.
72
     *
73
     * @param NavigationTree $tree The tree.
74
     * @return string Returns the AdminBSB menu header.
75
     */
76
    private function adminBSBMenuHeader(NavigationTree $tree) {
77
78
        // Initialize the template.
79
        $template = "<li %attributes%>%innerHTML%</li>";
80
81
        // Initialize the attributes.
82
        $attributes = [];
83
84
        $attributes["class"] = "menu-header";
85
86
        // Initialize the parameters.
87
        $innerHTML = null !== $tree->getId() ? $this->translate($tree->getId()) : "";
88
89
        // Return the HTML.
90
        return StringUtility::replace($template, ["%attributes%", "%innerHTML%"], [StringUtility::parseArray($attributes), $innerHTML]);
91
    }
92
93
    /**
94
     * Displays an AdminBSB menu item.
95
     *
96
     * @param string $content The menu item content.
0 ignored issues
show
Bug introduced by
There is no parameter named $content. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
97
     * @param string $url The menu item URL.
0 ignored issues
show
Bug introduced by
There is no parameter named $url. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
98
     * @param string $icon The menu item icon.
0 ignored issues
show
Bug introduced by
There is no parameter named $icon. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
99
     * @param boolean $active Menu item active ?
0 ignored issues
show
Bug introduced by
There is no parameter named $active. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
100
     * @param array $nodes The menu item nodes.
0 ignored issues
show
Bug introduced by
There is no parameter named $nodes. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
101
     * @return string Returns the AdminBSB menu item.
102
     */
103
    private function adminBSBMenuItem(NavigationNode $node) {
104
105
        // Initialize the multi level menu.
106
        $multiLevelMenu = false;
107
        foreach ($node->getNodes() as $current) {
108
            if (false === $this->isValidNode($current)) {
109
                continue;
110
            }
111
            $multiLevelMenu = true;
112
            break;
113
        }
114
115
        // Initialize the template.
116
        $template = [];
117
118
        $template[] = "<li" . (true === $node->getActive() ? ' class="active"' : "") . ">";
119
        $template[] = $this->adminBSBMenuItemLink($node, (true === $multiLevelMenu ? "menu-toggle" : null));
120
121
        if (true === $multiLevelMenu) {
122
            $template[] = '<ul class="ml-menu">';
123
124
            foreach ($node->getNodes() as $current) {
125
                if (false === $this->isValidNode($current)) {
126
                    continue;
127
                }
128
                $template[] = $this->adminBSBMenuItem($current);
129
            }
130
            $template[] = "</ul>";
131
        }
132
133
        $template [] = "</li>";
134
135
        // Return the HTML.
136
        return implode("\n", $template);
137
    }
138
139
    /**
140
     * Displays an AdminBSB menu item label.
141
     *
142
     * @param NavigationNode $node The node.
143
     * @return string Returns the AdminBSB menu label.
144
     */
145
    private function adminBSBMenuItemLabel(NavigationNode $node) {
146
147
        // Initialize the parameters.
148
        $innerHTML = null !== $node->getId() ? $this->translate($node->getId()) : "";
149
150
        // Check the icon.
151
        if (null === $node->getIcon()) {
152
            return $innerHTML;
153
        }
154
155
        // Initialize the template.
156
        $template = "%icon%<span>%innerHTML%</span>";
157
158
        // Initialize the parameters.
159
        $glyphicon = (new IconUITwigExtension())->adminBSBBasicIconFunction(["name" => $node->getIcon()]);
160
161
        // Return the HTML.
162
        return StringUtility::replace($template, ["%icon%", "%innerHTML%"], [$glyphicon, $innerHTML]);
163
    }
164
165
    /**
166
     * Displays an AdminBSB menu item link.
167
     *
168
     * @param NavigationNode $node The node.
169
     * @param string $class The node class.
170
     * @return string Returns the AdminBSB menu item link.
171
     */
172
    private function adminBSBMenuItemLink(NavigationNode $node, $class) {
173
174
        // Initialize the template.
175
        $template = "<a %attributes%>%innerHTML%</a>";
176
177
        // Initialize the attributes.
178
        $attributes = [];
179
180
        $attributes["class"]  = $class;
181
        $attributes["href"]   = $node->getUrl();
182
        $attributes["target"] = $node->getTarget();
183
184
        // Initialize the parameters.
185
        $innerHTML = $this->adminBSBMenuItemLabel($node);
186
187
        // Return the HTML.
188
        return StringUtility::replace($template, ["%attributes%", "%innerHTML%"], [StringUtility::parseArray($attributes), $innerHTML]);
189
    }
190
191
    /**
192
     * Determines if a node is valid.
193
     *
194
     * @param mixed $node The node.
195
     * @return boolean Returns true in case of success, false otherwise.
196
     */
197
    private function isValidNode($node) {
198
        return true === ($node instanceof NavigationNode) && true === $node->isDisplayable();
0 ignored issues
show
Bug introduced by
The class WBW\Bundle\BootstrapBund...vigation\NavigationNode does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
199
    }
200
201
    /**
202
     * Translate.
203
     *
204
     * @param string $id The translation id.
205
     * @return string Returns the translation in case of succes, id otherwise.
206
     */
207
    private function translate($id) {
208
209
        // Translate with Bootstrap bundle.
210
        $outputB = $this->translator->trans($id, [], "BootstrapBundle");
211
        if ($id === $outputB) {
212
            return $outputB;
213
        }
214
215
        // Translate with AdminBSB bundle.
216
        $outputA = $this->translator->trans($id, [], "AdminBSBBundle");
217
        if ($id === $outputA) {
218
            return $outputA;
219
        }
220
221
        // Translate.
222
        return $this->translator->trans($id);
223
    }
224
225
}
226