Completed
Push — master ( 1de9b7...830752 )
by Kristof
38:46 queued 24:09
created

Kunstmaan/AdminBundle/Helper/Menu/MenuBuilder.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Kunstmaan\AdminBundle\Helper\Menu;
4
5
use Symfony\Component\DependencyInjection\ContainerInterface;
6
use Symfony\Component\HttpFoundation\Request;
7
8
/**
9
 * The MenuBuilder will build the top menu and the side menu of the admin interface
10
 */
11
class MenuBuilder
12
{
13
    /**
14
     * @var MenuAdaptorInterface[]
15
     */
16
    private $adaptors = array();
17
18
    /**
19
     * @var MenuAdaptorInterface[]
20
     */
21
    private $sorted = array();
22
23
    /**
24
     * @var TopMenuItem[]
25
     */
26
    private $topMenuItems = null;
27
28
    /**
29
     * @var ContainerInterface
30
     */
31
    private $container;
32
33
    /**
34
     * @var MenuItem|null
35
     */
36
    private $currentCache = null;
37
38
    /**
39
     * Constructor
40
     *
41
     * @param ContainerInterface $container The container
42
     */
43
    public function __construct(ContainerInterface $container)
44
    {
45
        $this->container = $container;
46
    }
47
48
    /**
49
     * Add menu adaptor
50
     *
51
     * @param MenuAdaptorInterface $adaptor
52
     */
53
    public function addAdaptMenu(MenuAdaptorInterface $adaptor, $priority = 0)
54
    {
55
        $this->adaptors[$priority][] = $adaptor;
56
        unset($this->sorted);
57
    }
58
59
    /**
60
     * Get current menu item
61
     *
62
     * @return MenuItem|null
63
     */
64
    public function getCurrent()
65
    {
66
        if ($this->currentCache !== null) {
67
            return $this->currentCache;
68
        }
69
        /* @var $active MenuItem */
70
        $active = null;
71
        do {
72
            /* @var MenuItem[] $children */
73
            $children = $this->getChildren($active);
74
            $foundActiveChild = false;
75
            foreach ($children as $child) {
76
                if ($child->getActive()) {
77
                    $foundActiveChild = true;
78
                    $active = $child;
79
80
                    break;
81
                }
82
            }
83
        } while ($foundActiveChild);
84
        $this->currentCache = $active;
85
86
        return $active;
87
    }
88
89
    /**
90
     * Get breadcrumb path for current menu item
91
     *
92
     * @return MenuItem[]
93
     */
94
    public function getBreadCrumb()
95
    {
96
        $result = array();
97
        $current = $this->getCurrent();
98
        while (!is_null($current)) {
99
            array_unshift($result, $current);
100
            $current = $current->getParent();
101
        }
102
103
        return $result;
104
    }
105
106
    /**
107
     * Get top parent menu of current menu item
108
     *
109
     * @return TopMenuItem|null
110
     */
111
    public function getLowestTopChild()
112
    {
113
        $current = $this->getCurrent();
114
        while (!is_null($current)) {
115
            if ($current instanceof TopMenuItem) {
116
                return $current;
117
            }
118
            $current = $current->getParent();
119
        }
120
121
        return null;
122
    }
123
124
    /**
125
     * Get all top menu items
126
     *
127
     * @return MenuItem[]
128
     */
129
    public function getTopChildren()
130
    {
131
        if (is_null($this->topMenuItems)) {
132
            /* @var $request Request */
133
            $request = $this->container->get('request_stack')->getCurrentRequest();
134
            $this->topMenuItems = array();
135
            foreach ($this->getAdaptors() as $menuAdaptor) {
136
                $menuAdaptor->adaptChildren($this, $this->topMenuItems, null, $request);
137
            }
138
        }
139
140
        return $this->topMenuItems;
141
    }
142
143
    /**
144
     * Get immediate children of the specified menu item
145
     *
146
     * @param MenuItem $parent
0 ignored issues
show
Should the type for parameter $parent not be null|MenuItem?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
147
     *
148
     * @return MenuItem[]
149
     */
150
    public function getChildren(MenuItem $parent = null)
151
    {
152
        if ($parent === null) {
153
            return $this->getTopChildren();
154
        }
155
        /* @var $request Request */
156
        $request = $this->container->get('request_stack')->getCurrentRequest();
157
        $result = array();
158
        foreach ($this->getAdaptors() as $menuAdaptor) {
159
            $menuAdaptor->adaptChildren($this, $result, $parent, $request);
160
        }
161
162
        return $result;
163
    }
164
165
    private function getAdaptors()
166
    {
167
        if (!isset($this->sorted)) {
168
            $this->sortAdaptors();
169
        }
170
171
        return $this->sorted;
172
    }
173
174
    /**
175
     * Sorts the internal list of adaptors by priority.
176
     */
177 View Code Duplication
    private function sortAdaptors()
178
    {
179
        $this->sorted = array();
180
181
        if (isset($this->adaptors)) {
182
            krsort($this->adaptors);
183
            $this->sorted = call_user_func_array('array_merge', $this->adaptors);
184
        }
185
    }
186
}
187