Completed
Push — master ( e1ea0b...a3d6f2 )
by WEBEWEB
02:00
created

NavigationTreeHelper::nodeMatch()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 24
rs 9.2248
c 0
b 0
f 0
cc 5
nc 5
nop 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\Helper;
13
14
use Symfony\Component\HttpFoundation\Request;
15
use WBW\Bundle\BootstrapBundle\Navigation\AbstractNavigationNode;
16
use WBW\Bundle\BootstrapBundle\Navigation\BreadcrumbNode;
17
use WBW\Bundle\BootstrapBundle\Navigation\NavigationInterface;
18
use WBW\Bundle\BootstrapBundle\Navigation\NavigationNode;
19
use WBW\Bundle\BootstrapBundle\Navigation\NavigationTree;
20
21
/**
22
 * Navigation tree helper.
23
 *
24
 * @author webeweb <https://github.com/webeweb/>
25
 * @package WBW\Bundle\BootstrapBundle\Helper
26
 */
27
class NavigationTreeHelper {
28
29
    /**
30
     * Actives the nodes.
31
     *
32
     * @param Request $request The request.
33
     * @param array $nodes The nodes.
34
     * @param integer $level The node level.
35
     * @return boolean Returns true in case of success, false othewise.
36
     */
37
    protected static function activeNodes(Request $request, array $nodes = [], $level = 0) {
38
39
        // Initialize the result.
40
        $result = false;
41
42
        // Handle each node.
43
        foreach ($nodes as $n) {
44
45
            // Check the node.
46
            if (false === ($n instanceOf AbstractNavigationNode)) {
47
                continue;
48
            }
49
50
            // Init.
51
            $current = false;
1 ignored issue
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...
52
            $delete  = false;
53
54
            // Determines if the current node matches the URL.
55
            if (true === self::nodeMatch($n, $request)) {
56
                $current = true;
57
                $delete  = true;
58
            } else {
59
                $current = self::activeNodes($request, $n->getNodes(), $level + 1);
60
            }
61
62
            // Handle the next node.
63
            if (false === $current) {
64
                continue;
65
            }
66
67
            // Mark the node as active.
68
            $n->setActive(true);
69
70
            // Remove the icon only on the last level.
71
            if (true === $delete && 0 < $level) {
72
                $n->setIcon(null);
73
            }
74
75
            // Set the result.
76
            $result = true;
77
        }
78
79
        // Return the result.
80
        return $result;
81
    }
82
83
    /**
84
     * Active the tree.
85
     *
86
     * @param NavigationTree $tree The tree.
87
     * @param Request $request The request.
88
     * @return void
89
     */
90
    public static function activeTree(NavigationTree $tree, Request $request) {
91
        self::activeNodes($request, $tree->getNodes());
92
    }
93
94
    /**
95
     * Get the breadcrumbs.
96
     *
97
     * @param AbstractNavigationNode $node The tree.
98
     * @return AsbtractNavigationNode[] Returns the breadcrumbs.
99
     */
100
    public static function getBreadcrumbs(AbstractNavigationNode $node) {
101
102
        // Create the breadcrumbs.
103
        $breadcrumbs = [];
104
105
        // Check the instance.
106
        if (true === ($node instanceof NavigationNode || $node instanceof BreadcrumbNode) && true === $node->getActive()) {
107
            $breadcrumbs[] = $node;
108
        }
109
110
        // Handle each node.
111
        foreach ($node->getNodes() as $current) {
112
            if (false === ($current instanceof AbstractNavigationNode)) {
113
                continue;
114
            }
115
            $breadcrumbs = array_merge($breadcrumbs, self::getBreadcrumbs($current));
116
        }
117
118
        // Return the breadcrumbs.
119
        return $breadcrumbs;
120
    }
121
122
    /**
123
     * Determines if a node match an URL.
124
     *
125
     * @param AbstractNavigationNode $node The node.
126
     * @param Request $request The request.
127
     * @return boolean Returns true in case of success, false otherwise.
128
     */
129
    protected static function nodeMatch(AbstractNavigationNode $node, Request $request) {
130
131
        // Initialize the result.
132
        $result = false;
133
134
        //
135
        switch ($node->getMatcher()) {
136
137
            case NavigationInterface::NAVIGATION_MATCHER_REGEXP:
138
                $result = preg_match("/" . $node->getRoute() . "/", $request->getRequestUri());
139
                break;
140
141
            case NavigationInterface::NAVIGATION_MATCHER_ROUTER:
142
                $result = $request->get("_route") === $node->getRoute();
143
                break;
144
145
            case NavigationInterface::NAVIGATION_MATCHER_URL:
146
                $result = $request->getUri() === $node->getRoute() || $request->getRequestUri() === $node->getRoute();
147
                break;
148
        }
149
150
        // Return the result.
151
        return boolval($result);
152
    }
153
154
}
155