Completed
Push — master ( 768fc3...d528d9 )
by WEBEWEB
01:51
created

AbstractMenuTwigExtension::adminBSBMenu()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.4285
c 0
b 0
f 0
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 WBW\Bundle\AdminBSBBundle\Twig\Extension\AbstractAdminBSBTwigExtension;
15
use WBW\Bundle\AdminBSBBundle\Twig\Extension\UI\IconUITwigExtension;
16
use WBW\Bundle\BootstrapBundle\Navigation\NavigationNode;
17
use WBW\Bundle\BootstrapBundle\Navigation\NavigationTree;
18
use WBW\Library\Core\Utility\Argument\StringUtility;
19
20
/**
21
 * Abstract multi level menu Twig extension.
22
 *
23
 * @author webeweb <https://github.com/webeweb/>
24
 * @package WBW\Bundle\AdminBSBBundle\Twig\Extension\Menu
25
 * @abstract
26
 */
27
abstract class AbstractMenuTwigExtension extends AbstractAdminBSBTwigExtension {
28
29
    /**
30
     * Constructor.
31
     */
32
    protected function __construct() {
33
        parent::__construct();
34
    }
35
36
    /**
37
     * Displays an AdminBSB menu.
38
     *
39
     * @param NavigationTree $tree The tree.
40
     * @return string Returns the AdminBSB menu.
41
     */
42
    protected function adminBSBMenu(NavigationTree $tree) {
43
44
        // Initialize the template.
45
        $template = [];
46
47
        $template[] = $this->adminBSBMenuHeader($tree);
48
        foreach ($tree->getNodes() as $current) {
49
            if (false === $this->isValidNode($current)) {
50
                continue;
51
            }
52
            $template[] = $this->adminBSBMenuItem($current);
53
        }
54
55
        // Return the HTML.
56
        return implode("\n", $template);
57
    }
58
59
    /**
60
     * Displays an AdminBSB menu header.
61
     *
62
     * @param NavigationTree $tree The tree.
63
     * @return string Returns the AdminBSB menu header.
64
     */
65
    private function adminBSBMenuHeader(NavigationTree $tree) {
66
67
        // Initialize the template.
68
        $template = "<li %attributes%>%innerHTML%</li>";
69
70
        // Initialize the attributes.
71
        $attributes = [];
72
73
        $attributes["class"] = "menu-header";
74
75
        // Initialize the parameters.
76
        $innerHTML = null !== $tree->getId() ? $tree->getId() : "";
77
78
        // Return the HTML.
79
        return StringUtility::replace($template, ["%attributes%", "%innerHTML%"], [StringUtility::parseArray($attributes), $innerHTML]);
80
    }
81
82
    /**
83
     * Displays an AdminBSB menu item.
84
     *
85
     * @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...
86
     * @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...
87
     * @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...
88
     * @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...
89
     * @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...
90
     * @return string Returns the AdminBSB menu item.
91
     */
92
    private function adminBSBMenuItem(NavigationNode $node) {
93
94
        // Initialize the multi level menu.
95
        $multiLevelMenu = false;
96
        foreach ($node->getNodes() as $current) {
97
            if (false === $this->isValidNode($current)) {
98
                continue;
99
            }
100
            $multiLevelMenu = true;
101
            break;
102
        }
103
104
        // Initialize the template.
105
        $template = [];
106
107
        $template[] = "<li" . (true === $node->getActive() ? ' class="active"' : "") . ">";
108
        $template[] = $this->adminBSBMenuItemLink($node, (true === $multiLevelMenu ? "menu-toggle" : null));
109
110
        if (true === $multiLevelMenu) {
111
            $template[] = '<ul class="ml-menu">';
112
113
            foreach ($node->getNodes() as $current) {
114
                if (false === $this->isValidNode($current)) {
115
                    continue;
116
                }
117
                $template[] = $this->adminBSBMenuItem($current);
118
            }
119
            $template[] = "</ul>";
120
        }
121
122
        $template [] = "</li>";
123
124
        // Return the HTML.
125
        return implode("\n", $template);
126
    }
127
128
    /**
129
     * Displays an AdminBSB menu item label.
130
     *
131
     * @param NavigationNode $node The node.
132
     * @return string Returns the AdminBSB menu label.
133
     */
134
    private function adminBSBMenuItemLabel(NavigationNode $node) {
135
136
        // Initialize the parameters.
137
        $innerHTML = null !== $node->getId() ? $node->getId() : "";
138
139
        // Check the icon.
140
        if (null === $node->getIcon()) {
141
            return $innerHTML;
142
        }
143
144
        // Initialize the template.
145
        $template = "%icon%<span>%innerHTML%</span>";
146
147
        // Initialize the parameters.
148
        $glyphicon = (new IconUITwigExtension())->adminBSBBasicIconFunction(["name" => $node->getIcon()]);
149
150
        // Return the HTML.
151
        return StringUtility::replace($template, ["%icon%", "%innerHTML%"], [$glyphicon, $innerHTML]);
152
    }
153
154
    /**
155
     * Displays an AdminBSB menu item link.
156
     *
157
     * @param NavigationNode $node The node.
158
     * @param string $class The node class.
159
     * @return string Returns the AdminBSB menu item link.
160
     */
161
    private function adminBSBMenuItemLink(NavigationNode $node, $class) {
162
163
        // Initialize the template.
164
        $template = "<a %attributes%>%innerHTML%</a>";
165
166
        // Initialize the attributes.
167
        $attributes = [];
168
169
        $attributes["class"] = $class;
170
        $attributes["href"]  = $node->getUrl();
171
172
        // Initialize the parameters.
173
        $innerHTML = $this->adminBSBMenuItemLabel($node);
174
175
        // Return the HTML.
176
        return StringUtility::replace($template, ["%attributes%", "%innerHTML%"], [StringUtility::parseArray($attributes), $innerHTML]);
177
    }
178
179
    /**
180
     * Determines if a node is valid.
181
     *
182
     * @param mixed $node The node.
183
     * @return boolean Returns true in case of success, false otherwise.
184
     */
185
    private function isValidNode($node) {
186
        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...
187
    }
188
189
}
190