MenuItem::setParent()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
/*
3
  ÁTICA - Aplicación web para la gestión documental de centros educativos
4
5
  Copyright (C) 2015-2017: Luis Ramón López López
6
7
  This program is free software: you can redistribute it and/or modify
8
  it under the terms of the GNU Affero General Public License as published by
9
  the Free Software Foundation, either version 3 of the License, or
10
  (at your option) any later version.
11
12
  This program is distributed in the hope that it will be useful,
13
  but WITHOUT ANY WARRANTY; without even the implied warranty of
14
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
  GNU Affero General Public License for more details.
16
17
  You should have received a copy of the GNU Affero General Public License
18
  along with this program.  If not, see [http://www.gnu.org/licenses/].
19
*/
20
21
namespace AppBundle\Menu;
22
23
use Doctrine\Common\Collections\ArrayCollection;
24
use Doctrine\Common\Collections\Collection;
25
26
class MenuItem
27
{
28
    /**
29
     * @var string
30
     */
31
    protected $name;
32
33
    /**
34
     * @var string
35
     */
36
    protected $caption;
37
38
    /**
39
     * @var string
40
     */
41
    protected $description;
42
43
    /**
44
     * @var string
45
     */
46
    protected $routeName;
47
48
    /**
49
     * @var array
50
     */
51
    protected $routeParams = [];
52
53
    /**
54
     * @var string
55
     */
56
    protected $icon;
57
58
    /**
59
     * @var string
60
     */
61
    protected $color;
62
63
    /**
64
     * @var Collection
65
     */
66
    protected $children;
67
68
    /**
69
     * @var MenuItem|null
70
     */
71
    protected $parent;
72
73
    /**
74
     * @var integer
75
     */
76
    protected $priority;
77
78
    /**
79
     * MenuItem constructor
80
     */
81
    public function __construct()
82
    {
83
        $this->children = new ArrayCollection();
84
        $this->parent = null;
85
        $this->priority = 0;
86
    }
87
88
    /**
89
     * @return string
90
     */
91
    public function getName()
92
    {
93
        return $this->name;
94
    }
95
96
    /**
97
     * @param string $name
98
     * @return MenuItem
99
     */
100
    public function setName($name)
101
    {
102
        $this->name = $name;
103
        return $this;
104
    }
105
106
    /**
107
     * @return string
108
     */
109
    public function getCaption()
110
    {
111
        return $this->caption;
112
    }
113
114
    /**
115
     * @param string $caption
116
     * @return MenuItem
117
     */
118
    public function setCaption($caption)
119
    {
120
        $this->caption = $caption;
121
        return $this;
122
    }
123
124
    /**
125
     * @return string
126
     */
127
    public function getDescription()
128
    {
129
        return $this->description;
130
    }
131
132
    /**
133
     * @param string $description
134
     * @return MenuItem
135
     */
136
    public function setDescription($description)
137
    {
138
        $this->description = $description;
139
        return $this;
140
    }
141
142
    /**
143
     * @return string
144
     */
145
    public function getRouteName()
146
    {
147
        return $this->routeName;
148
    }
149
150
    /**
151
     * @param string $routeName
152
     * @return MenuItem
153
     */
154
    public function setRouteName($routeName)
155
    {
156
        $this->routeName = $routeName;
157
        return $this;
158
    }
159
160
    /**
161
     * @return array
162
     */
163
    public function getRouteParams()
164
    {
165
        return $this->routeParams;
166
    }
167
168
    /**
169
     * @param array $routeParams
170
     * @return MenuItem
171
     */
172
    public function setRouteParams($routeParams)
173
    {
174
        $this->routeParams = $routeParams;
175
        return $this;
176
    }
177
178
    /**
179
     * @return string
180
     */
181
    public function getIcon()
182
    {
183
        return $this->icon;
184
    }
185
186
    /**
187
     * @param string $icon
188
     * @return MenuItem
189
     */
190
    public function setIcon($icon)
191
    {
192
        $this->icon = $icon;
193
        return $this;
194
    }
195
196
    /**
197
     * @return string
198
     */
199
    public function getColor()
200
    {
201
        return $this->color;
202
    }
203
204
    /**
205
     * @param string $color
206
     * @return MenuItem
207
     */
208
    public function setColor($color)
209
    {
210
        $this->color = $color;
211
        return $this;
212
    }
213
214
    /**
215
     * @return Collection
216
     */
217
    public function getChildren()
218
    {
219
        return $this->children;
220
    }
221
222
    /**
223
     * @param MenuItem $child
224
     * @return MenuItem
225
     */
226
    public function addChild(MenuItem $child)
227
    {
228
        $this->children->add($child);
229
230
        $iterator = $this->children->getIterator();
231
        $iterator->uasort(function(MenuItem $a, MenuItem $b) {
232
            if ($a->getPriority() == $b->getPriority()) {
233
                return $a->getName() < $b->getName() ? -1 : 1;
234
            }
235
            return ($a->getPriority() < $b->getPriority()) ? -1 : 1;
236
        });
237
        $this->children = new ArrayCollection(iterator_to_array($iterator));
238
239
        $child->setParent($this);
240
        return $this;
241
    }
242
243
    /**
244
     * @param MenuItem $child
245
     * @return MenuItem
246
     */
247
    public function removeChild(MenuItem $child)
248
    {
249
        $index = $this->children->indexOf($child);
250
        if (false !== $index) {
251
            $this->children->remove($index);
252
        }
253
        return $this;
254
    }
255
256
    /**
257
     * @return MenuItem|null
258
     */
259
    public function getParent()
260
    {
261
        return $this->parent;
262
    }
263
264
    /**
265
     * @param MenuItem|null $parent
266
     * @return MenuItem
267
     */
268
    public function setParent(MenuItem $parent)
269
    {
270
        $this->parent = $parent;
271
        return $this;
272
    }
273
274
    /**
275
     * @return int
276
     */
277
    public function getPriority()
278
    {
279
        return $this->priority;
280
    }
281
282
    /**
283
     * @param int $priority
284
     * @return MenuItem
285
     */
286
    public function setPriority($priority)
287
    {
288
        $this->priority = $priority;
289
        return $this;
290
    }
291
292
    /**
293
     * @return MenuItem[]
294
     */
295 View Code Duplication
    public function getPath()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
296
    {
297
        $path = [];
298
        $current = $this;
299
300
        while (null !== $current) {
301
            array_unshift($path, $current);
302
            $current = $current->getParent();
303
        }
304
305
        return $path;
306
    }
307
}
308