Completed
Push — master ( bda6d8...63200e )
by WEBEWEB
01:40
created

NavigationTreeHelper::activeNodes()   A

Complexity

Conditions 5
Paths 6

Size

Total Lines 38

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 38
rs 9.0008
c 0
b 0
f 0
cc 5
nc 6
nop 3
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\Navigation;
13
14
use Symfony\Component\HttpFoundation\Request;
15
16
/**
17
 * Navigation tree helper.
18
 *
19
 * @author webeweb <https://github.com/webeweb/>
20
 * @package WBW\Bundle\BootstrapBundle\Navigation
21
 */
22
class NavigationTreeHelper {
23
24
    /**
25
     * Actives the nodes.
26
     *
27
     * @param Request $request The request.
28
     * @param array $nodes The nodes.
29
     * @param int $level The node level.
30
     * @return boolean Returns true in case of success, false othewise.
31
     */
32
    protected static function activeNodes(Request $request, array $nodes = [], $level = 0) {
33
34
        // Initialize the result.
35
        $result = false;
36
37
        // Handle each node.
38
        foreach ($nodes as $n) {
39
40
            // Check the node.
41
            if (false === ($n instanceOf AbstractNavigationNode)) {
42
                continue;
43
            }
44
45
            // Init.
46
            $current = false;
0 ignored issues
show
Unused Code introduced by
$current is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
47
48
            // Determines if the current node matches the URL.
49
            if (true === self::nodeMatch($n, $request)) {
50
                $current = true;
51
            } else {
52
                $current = self::activeNodes($request, $n->getNodes(), $level + 1);
53
            }
54
55
            // Handle the next node.
56
            if (false === $current) {
57
                continue;
58
            }
59
60
            // Mark the node as active.
61
            $n->setActive(true);
62
63
            // Set the result.
64
            $result = true;
65
        }
66
67
        // Return the result.
68
        return $result;
69
    }
70
71
    /**
72
     * Active the tree.
73
     *
74
     * @param NavigationTree $tree The tree.
75
     * @param Request $request The request.
76
     * @return void
77
     */
78
    public static function activeTree(NavigationTree $tree, Request $request) {
79
        self::activeNodes($request, $tree->getNodes());
80
    }
81
82
    /**
83
     * Get the breadcrumbs.
84
     *
85
     * @param AbstractNavigationNode $node The tree.
86
     * @return AsbtractNavigationNode[] Returns the breadcrumbs.
87
     */
88
    public static function getBreadcrumbs(AbstractNavigationNode $node) {
89
90
        // Create the breadcrumbs.
91
        $breadcrumbs = [];
92
93
        // Check the instance.
94
        if (true === ($node instanceof NavigationNode || $node instanceof BreadcrumbNode) && true === $node->getActive()) {
95
            $breadcrumbs[] = $node;
96
        }
97
98
        // Handle each node.
99
        foreach ($node->getNodes() as $current) {
100
            if (false === ($current instanceof AbstractNavigationNode)) {
101
                continue;
102
            }
103
            $breadcrumbs = array_merge($breadcrumbs, self::getBreadcrumbs($current));
104
        }
105
106
        // Return the breadcrumbs.
107
        return $breadcrumbs;
108
    }
109
110
    /**
111
     * Determines if a node match an URL.
112
     *
113
     * @param AbstractNavigationNode $node The node.
114
     * @param Request $request The request.
115
     * @return boolean Returns true in case of success, false otherwise.
116
     */
117
    protected static function nodeMatch(AbstractNavigationNode $node, Request $request) {
118
119
        // Initialize the result.
120
        $result = false;
121
122
        //
123
        switch ($node->getMatcher()) {
124
125
            case NavigationInterface::NAVIGATION_MATCHER_REGEXP:
126
                $result = preg_match("/" . $node->getRoute() . "/", $request->getUri());
127
                break;
128
129
            case NavigationInterface::NAVIGATION_MATCHER_ROUTER:
130
                $result = $request->get("_route") === $node->getRoute();
131
                break;
132
133
            case NavigationInterface::NAVIGATION_MATCHER_URL:
134
                $result = $request->getUri() === $node->getRoute() || $request->getRequestUri() === $node->getRoute();
135
                break;
136
        }
137
138
        // Return the result.
139
        return boolval($result);
140
    }
141
142
}
143