Completed
Push — master ( 91fdab...75a7b9 )
by
unknown
13:37
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[] $adaptors
15
     */
16
    private $adaptors = array();
17
18
    /**
19
     * @var MenuAdaptorInterface[] $adaptors
20
     */
21
    private $sorted = array();
22
23
    /**
24
     * @var TopMenuItem[] $topMenuItems
25
     */
26
    private $topMenuItems = null;
27
28
    /**
29
     * @var ContainerInterface $container
30
     */
31
    private $container;
32
33
    /**
34
     * @var MenuItem|null
35
     */
36
    private $currentCache = null;
37
38
39
    /**
40
     * Constructor
41
     *
42
     * @param ContainerInterface $container The container
43
     */
44
    public function __construct(ContainerInterface $container)
45
    {
46
        $this->container = $container;
47
    }
48
49
    /**
50
     * Add menu adaptor
51
     *
52
     * @param MenuAdaptorInterface $adaptor
53
     */
54
    public function addAdaptMenu(MenuAdaptorInterface $adaptor, $priority = 0)
55
    {
56
        $this->adaptors[$priority][] = $adaptor;
57
        unset($this->sorted);
58
    }
59
60
    /**
61
     * Get current menu item
62
     *
63
     * @return MenuItem|null
64
     */
65
    public function getCurrent()
66
    {
67
        if ($this->currentCache !== null) {
68
            return $this->currentCache;
69
        }
70
        /* @var $active MenuItem */
71
        $active = null;
72
        do {
73
            /* @var MenuItem[] $children */
74
            $children         = $this->getChildren($active);
75
            $foundActiveChild = false;
76
            foreach ($children as $child) {
77
                if ($child->getActive()) {
78
                    $foundActiveChild = true;
79
                    $active           = $child;
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
116
            if ($current instanceof TopMenuItem) {
117
                return $current;
118
            }
119
            $current = $current->getParent();
120
        }
121
122
        return null;
123
    }
124
125
    /**
126
     * Get all top menu items
127
     *
128
     * @return MenuItem[]
129
     */
130
    public function getTopChildren()
131
    {
132
        if (is_null($this->topMenuItems)) {
133
            /* @var $request Request */
134
            $request            = $this->container->get('request_stack')->getCurrentRequest();
135
            $this->topMenuItems = array();
136
            foreach ($this->getAdaptors() as $menuAdaptor) {
137
                $menuAdaptor->adaptChildren($this, $this->topMenuItems, null, $request);
138
            }
139
        }
140
141
        return $this->topMenuItems;
142
    }
143
144
    /**
145
     * Get immediate children of the specified menu item
146
     *
147
     * @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...
148
     *
149
     * @return MenuItem[]
150
     */
151
    public function getChildren(MenuItem $parent = null)
152
    {
153
        if ($parent === null) {
154
            return $this->getTopChildren();
155
        }
156
        /* @var $request Request */
157
        $request = $this->container->get('request_stack')->getCurrentRequest();
158
        $result  = array();
159
        foreach ($this->getAdaptors() as $menuAdaptor) {
160
            $menuAdaptor->adaptChildren($this, $result, $parent, $request);
161
        }
162
163
        return $result;
164
    }
165
166
    private function getAdaptors()
167
    {
168
        if (!isset($this->sorted)) {
169
            $this->sortAdaptors();
170
        }
171
172
        return $this->sorted;
173
    }
174
175
    /**
176
     * Sorts the internal list of adaptors by priority.
177
     */
178 View Code Duplication
    private function sortAdaptors()
179
    {
180
        $this->sorted = array();
181
182
        if (isset($this->adaptors)) {
183
            krsort($this->adaptors);
184
            $this->sorted = call_user_func_array('array_merge', $this->adaptors);
185
        }
186
    }
187
}
188