Completed
Push — master ( fde323...c9c573 )
by WEBEWEB
01:42
created

NavigationTree   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 109
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 16
lcom 0
cbo 1
dl 0
loc 109
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
B activeNodes() 0 45 7
A activeTree() 0 3 1
B getBreadcrumbs() 0 26 7
1
<?php
2
3
/**
4
 * This file is part of the bootstrap-bundle package.
5
 *
6
 * (c) 2017 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
/**
15
 * Navigation tree.
16
 *
17
 * @author webeweb <https://github.com/webeweb/>
18
 * @package WBW\Bundle\BootstrapBundle\Navigation
19
 */
20
class NavigationTree extends AbstractNavigationNode {
21
22
    /**
23
     * Constructor.
24
     *
25
     * @param $name The name.
26
     */
27
    public function __construct($name) {
28
        parent::__construct($name);
29
    }
30
31
    /**
32
     * Actives the nodes.
33
     *
34
     * @param string $url The active URL.
35
     * @param array $nodes The nodes.
36
     * @param integer $level The node level.
37
     * @return boolean Returns true in case of success, false othewise.
38
     */
39
    protected function activeNodes($url, array $nodes = [], $level = 0) {
40
41
        // Initialize the result.
42
        $result = false;
43
44
        // Handle each node.
45
        foreach ($nodes as $n) {
46
47
            // Check the node.
48
            if (false === ($n instanceOf AbstractNavigationNode)) {
49
                continue;
50
            }
51
52
            // Init.
53
            $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...
54
            $delete  = false;
55
56
            // Determines if the current node matches the URL.
57
            if ($url === $n->getRoute()) {
58
                $current = true;
59
                $delete  = true;
60
            } else {
61
                $current = $this->activeNodes($url, $n->getNodes(), $level + 1);
62
            }
63
64
            // Handle next node.
65
            if (false === $current) {
66
                continue;
67
            }
68
69
            // Mark the node as active.
70
            $n->setActive(true);
71
72
            // Remove the icon only on the last level.
73
            if (true === $delete && 0 < $level) {
74
                $n->setIcon(null);
75
            }
76
77
            // Set the result.
78
            $result = true;
79
        }
80
81
        // Return the result.
82
        return $result;
83
    }
84
85
    /**
86
     * Active the tree.
87
     *
88
     * @param string $url The URL.
89
     * @return void
90
     */
91
    public function activeTree($url) {
92
        $this->activeNodes($url, $this->getNodes());
93
    }
94
95
    /**
96
     * Get the breadcrumbs.
97
     *
98
     * @param AbstractNavigationNode $node The navigation node.
99
     * @return AsbtractNavigationNode[] Returns the navigation nodes.
100
     */
101
    public function getBreadcrumbs(AbstractNavigationNode $node = null) {
102
103
        // Create the breadcrumbs.
104
        $breadcrumbs = [];
105
106
        // Correct the parameter if necessary.
107
        if (null === $node) {
108
            $node = $this;
109
        }
110
111
        // Check the instance.
112
        if (true === ($node instanceof NavigationNode || $node instanceof BreadcrumbNode) && true === $node->getActive()) {
113
            $breadcrumbs[] = $node;
114
        }
115
116
        // Handle each node.
117
        foreach ($node->getNodes() as $current) {
118
            if (false === ($current instanceof AbstractNavigationNode)) {
119
                continue;
120
            }
121
            $breadcrumbs = array_merge($breadcrumbs, $this->getBreadcrumbs($current));
122
        }
123
124
        // Return the breadcrumbs.
125
        return $breadcrumbs;
126
    }
127
128
}
129