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\MediaBundle\Entity; |
||
4 | |||
5 | use Doctrine\Common\Collections\ArrayCollection; |
||
6 | use Doctrine\ORM\Mapping as ORM; |
||
7 | use Gedmo\Mapping\Annotation as Gedmo; |
||
8 | use Gedmo\Tree\Node as GedmoNode; |
||
9 | use Kunstmaan\AdminBundle\Entity\AbstractEntity; |
||
10 | use Symfony\Component\Validator\Constraints as Assert; |
||
11 | |||
12 | /** |
||
13 | * Class that defines a folder from the MediaBundle in the database |
||
14 | * |
||
15 | * @ORM\Entity(repositoryClass="Kunstmaan\MediaBundle\Repository\FolderRepository") |
||
16 | * @ORM\Table(name="kuma_folders", indexes={ |
||
17 | * @ORM\Index(name="idx_folder_internal_name", columns={"internal_name"}), |
||
18 | * @ORM\Index(name="idx_folder_name", columns={"name"}), |
||
19 | * @ORM\Index(name="idx_folder_deleted", columns={"deleted"}) |
||
20 | * }) |
||
21 | * @Gedmo\Tree(type="nested") |
||
22 | * @ORM\HasLifecycleCallbacks |
||
23 | */ |
||
24 | class Folder extends AbstractEntity implements GedmoNode |
||
25 | { |
||
26 | const TYPE_FILES = 'files'; |
||
27 | const TYPE_IMAGE = 'image'; |
||
28 | const TYPE_MEDIA = 'media'; |
||
29 | const TYPE_SLIDESHOW = 'slideshow'; |
||
30 | const TYPE_VIDEO = 'video'; |
||
31 | |||
32 | /** |
||
33 | * @var string |
||
34 | * |
||
35 | * @Gedmo\Translatable |
||
36 | * @ORM\Column(type="string") |
||
37 | * @Assert\NotBlank() |
||
38 | */ |
||
39 | protected $name; |
||
40 | |||
41 | /** |
||
42 | * @var string |
||
43 | * |
||
44 | * @Gedmo\Locale |
||
45 | * Used locale to override Translation listener`s locale |
||
46 | * this is not a mapped field of entity metadata, just a simple property |
||
47 | */ |
||
48 | protected $locale; |
||
49 | |||
50 | /** |
||
51 | * @var Folder |
||
52 | * |
||
53 | * @ORM\ManyToOne(targetEntity="Folder", inversedBy="children", fetch="LAZY") |
||
54 | * @ORM\JoinColumn(name="parent_id", referencedColumnName="id", nullable=true) |
||
55 | * @Gedmo\TreeParent |
||
56 | */ |
||
57 | protected $parent; |
||
58 | |||
59 | /** |
||
60 | * @var ArrayCollection |
||
61 | * |
||
62 | * @ORM\OneToMany(targetEntity="Folder", mappedBy="parent", fetch="LAZY") |
||
63 | * @ORM\OrderBy({"lft" = "ASC"}) |
||
64 | */ |
||
65 | protected $children; |
||
66 | |||
67 | /** |
||
68 | * @var ArrayCollection |
||
69 | * |
||
70 | * @ORM\OneToMany(targetEntity="Media", mappedBy="folder", fetch="LAZY") |
||
71 | * @ORM\OrderBy({"name" = "ASC"}) |
||
72 | */ |
||
73 | protected $media; |
||
74 | |||
75 | /** |
||
76 | * @var \DateTime |
||
77 | * |
||
78 | * @ORM\Column(type="datetime", name="created_at") |
||
79 | */ |
||
80 | protected $createdAt; |
||
81 | |||
82 | /** |
||
83 | * @var \DateTime |
||
84 | * |
||
85 | * @ORM\Column(type="datetime", name="updated_at") |
||
86 | */ |
||
87 | protected $updatedAt; |
||
88 | |||
89 | /** |
||
90 | * @var string |
||
91 | * |
||
92 | * @ORM\Column(type="string", nullable=true) |
||
93 | */ |
||
94 | protected $rel; |
||
95 | |||
96 | /** |
||
97 | * @var string |
||
98 | * |
||
99 | * @ORM\Column(type="string", name="internal_name", nullable=true) |
||
100 | */ |
||
101 | protected $internalName; |
||
102 | |||
103 | /** |
||
104 | * @var int |
||
105 | * |
||
106 | * @ORM\Column(name="lft", type="integer", nullable=true) |
||
107 | * @Gedmo\TreeLeft |
||
108 | */ |
||
109 | protected $lft; |
||
110 | |||
111 | /** |
||
112 | * @var int |
||
113 | * |
||
114 | * @ORM\Column(name="lvl", type="integer", nullable=true) |
||
115 | * @Gedmo\TreeLevel |
||
116 | */ |
||
117 | protected $lvl; |
||
118 | |||
119 | /** |
||
120 | * @var int |
||
121 | * |
||
122 | * @ORM\Column(name="rgt", type="integer", nullable=true) |
||
123 | * @Gedmo\TreeRight |
||
124 | */ |
||
125 | protected $rgt; |
||
126 | |||
127 | /** |
||
128 | * @var bool |
||
129 | * |
||
130 | * @ORM\Column(type="boolean") |
||
131 | */ |
||
132 | protected $deleted; |
||
133 | |||
134 | /** |
||
135 | * constructor |
||
136 | */ |
||
137 | 24 | View Code Duplication | public function __construct() |
0 ignored issues
–
show
|
|||
138 | { |
||
139 | 24 | $this->children = new ArrayCollection(); |
|
140 | 24 | $this->media = new ArrayCollection(); |
|
141 | 24 | $this->setCreatedAt(new \DateTime()); |
|
142 | 24 | $this->setUpdatedAt(new \DateTime()); |
|
143 | 24 | $this->deleted = false; |
|
144 | 24 | } |
|
145 | |||
146 | /** |
||
147 | * @return array |
||
148 | */ |
||
149 | public static function allTypes() |
||
150 | { |
||
151 | return [ |
||
152 | self::TYPE_MEDIA => self::TYPE_MEDIA, |
||
153 | self::TYPE_IMAGE => self::TYPE_IMAGE, |
||
154 | self::TYPE_FILES => self::TYPE_FILES, |
||
155 | self::TYPE_SLIDESHOW => self::TYPE_SLIDESHOW, |
||
156 | self::TYPE_VIDEO => self::TYPE_VIDEO, |
||
157 | ]; |
||
158 | } |
||
159 | |||
160 | /** |
||
161 | * @return string |
||
162 | */ |
||
163 | 1 | public function getTranslatableLocale() |
|
164 | { |
||
165 | 1 | return $this->locale; |
|
166 | } |
||
167 | |||
168 | /** |
||
169 | * @param string $locale |
||
170 | * |
||
171 | * @return Folder |
||
172 | */ |
||
173 | 1 | public function setTranslatableLocale($locale) |
|
174 | { |
||
175 | 1 | $this->locale = $locale; |
|
176 | |||
177 | 1 | return $this; |
|
178 | } |
||
179 | |||
180 | /** |
||
181 | * @return string |
||
182 | */ |
||
183 | 1 | public function getRel() |
|
184 | { |
||
185 | 1 | return $this->rel; |
|
186 | } |
||
187 | |||
188 | /** |
||
189 | * @param string $rel |
||
190 | * |
||
191 | * @return Folder |
||
192 | */ |
||
193 | 1 | public function setRel($rel) |
|
194 | { |
||
195 | 1 | $this->rel = $rel; |
|
196 | |||
197 | 1 | return $this; |
|
198 | } |
||
199 | |||
200 | /** |
||
201 | * Get createdAd |
||
202 | * |
||
203 | * @return \DateTime |
||
204 | */ |
||
205 | 1 | public function getCreatedAt() |
|
206 | { |
||
207 | 1 | return $this->createdAt; |
|
208 | } |
||
209 | |||
210 | /** |
||
211 | * Set createdAd |
||
212 | * |
||
213 | * @param \DateTime $createdAt |
||
214 | * |
||
215 | * @return Folder |
||
216 | */ |
||
217 | 24 | public function setCreatedAt($createdAt) |
|
218 | { |
||
219 | 24 | $this->createdAt = $createdAt; |
|
220 | |||
221 | 24 | return $this; |
|
222 | } |
||
223 | |||
224 | /** |
||
225 | * Get updatedAt |
||
226 | * |
||
227 | * @return \DateTime |
||
228 | */ |
||
229 | 1 | public function getUpdatedAt() |
|
230 | { |
||
231 | 1 | return $this->updatedAt; |
|
232 | } |
||
233 | |||
234 | /** |
||
235 | * Set updatedAt |
||
236 | * |
||
237 | * @param \DateTime $updatedAt |
||
238 | * |
||
239 | * @return Folder |
||
240 | */ |
||
241 | 24 | public function setUpdatedAt($updatedAt) |
|
242 | { |
||
243 | 24 | $this->updatedAt = $updatedAt; |
|
244 | |||
245 | 24 | return $this; |
|
246 | } |
||
247 | |||
248 | /** |
||
249 | * @return Folder[]: |
||
0 ignored issues
–
show
The doc-type
Folder[]: could not be parsed: Expected "|" or "end of type", but got ":" at position 8. (view supported doc-types)
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.
Loading history...
|
|||
250 | */ |
||
251 | 1 | View Code Duplication | public function getParents() |
252 | { |
||
253 | 1 | $parent = $this->getParent(); |
|
254 | 1 | $parents = []; |
|
255 | 1 | while ($parent !== null) { |
|
256 | 1 | $parents[] = $parent; |
|
257 | 1 | $parent = $parent->getParent(); |
|
258 | } |
||
259 | |||
260 | 1 | return array_reverse($parents); |
|
261 | } |
||
262 | |||
263 | /** |
||
264 | * Get parent |
||
265 | * |
||
266 | * @return Folder |
||
267 | */ |
||
268 | 3 | public function getParent() |
|
269 | { |
||
270 | 3 | return $this->parent; |
|
271 | } |
||
272 | |||
273 | /** |
||
274 | * Set parent |
||
275 | * |
||
276 | * @param Folder $parent |
||
277 | * |
||
278 | * @return Folder |
||
279 | */ |
||
280 | 4 | public function setParent(Folder $parent = null) |
|
281 | { |
||
282 | 4 | $this->parent = $parent; |
|
283 | |||
284 | 4 | return $this; |
|
285 | } |
||
286 | |||
287 | /** |
||
288 | * Add a child |
||
289 | * |
||
290 | * @return Folder |
||
291 | */ |
||
292 | 2 | public function addChild(Folder $child) |
|
293 | { |
||
294 | 2 | $this->children[] = $child; |
|
295 | 2 | $child->setParent($this); |
|
296 | |||
297 | 2 | return $this; |
|
298 | } |
||
299 | |||
300 | /** |
||
301 | * Add file |
||
302 | * |
||
303 | * @return Folder |
||
304 | */ |
||
305 | 2 | public function addMedia(Media $media) |
|
306 | { |
||
307 | 2 | $this->media[] = $media; |
|
308 | |||
309 | 2 | return $this; |
|
310 | } |
||
311 | |||
312 | /** |
||
313 | * Get media |
||
314 | * |
||
315 | * @param bool $includeDeleted |
||
316 | * |
||
317 | * @return ArrayCollection |
||
318 | */ |
||
319 | 2 | View Code Duplication | public function getMedia($includeDeleted = false) |
0 ignored issues
–
show
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...
|
|||
320 | { |
||
321 | 2 | if ($includeDeleted) { |
|
322 | 1 | return $this->media; |
|
323 | } |
||
324 | |||
325 | 2 | return $this->media->filter( |
|
326 | function (Media $entry) { |
||
327 | 2 | if ($entry->isDeleted()) { |
|
328 | 1 | return false; |
|
329 | } |
||
330 | |||
331 | 2 | return true; |
|
332 | 2 | } |
|
333 | ); |
||
334 | } |
||
335 | |||
336 | /** |
||
337 | * @param int $id |
||
338 | * |
||
339 | * @return bool |
||
340 | */ |
||
341 | 1 | public function hasActive($id) |
|
342 | { |
||
343 | 1 | foreach ($this->getChildren() as $child) { |
|
344 | 1 | if ($child->hasActive($id) || $child->getId() == $id) { |
|
345 | 1 | return true; |
|
346 | } |
||
347 | } |
||
348 | |||
349 | 1 | return false; |
|
350 | } |
||
351 | |||
352 | /** |
||
353 | * Get child folders |
||
354 | * |
||
355 | * @param bool $includeDeleted |
||
356 | * |
||
357 | * @return ArrayCollection |
||
358 | */ |
||
359 | 3 | View Code Duplication | public function getChildren($includeDeleted = false) |
360 | { |
||
361 | 3 | if ($includeDeleted) { |
|
362 | 1 | return $this->children; |
|
363 | } |
||
364 | |||
365 | 3 | return $this->children->filter( |
|
366 | function (Folder $entry) { |
||
367 | 3 | if ($entry->isDeleted()) { |
|
368 | 1 | return false; |
|
369 | } |
||
370 | |||
371 | 3 | return true; |
|
372 | 3 | } |
|
373 | ); |
||
374 | } |
||
375 | |||
376 | /** |
||
377 | * @param ArrayCollection $children |
||
378 | * |
||
379 | * @return Folder |
||
380 | */ |
||
381 | 1 | public function setChildren($children) |
|
382 | { |
||
383 | 1 | $this->children = $children; |
|
384 | |||
385 | 1 | return $this; |
|
386 | } |
||
387 | |||
388 | /** |
||
389 | * @return bool |
||
390 | */ |
||
391 | 4 | public function isDeleted() |
|
392 | { |
||
393 | 4 | return $this->deleted; |
|
394 | } |
||
395 | |||
396 | /** |
||
397 | * @param bool $deleted |
||
398 | * |
||
399 | * @return Folder |
||
400 | */ |
||
401 | 2 | public function setDeleted($deleted) |
|
402 | { |
||
403 | 2 | $this->deleted = $deleted; |
|
404 | |||
405 | 2 | return $this; |
|
406 | } |
||
407 | |||
408 | /** |
||
409 | * @return string |
||
410 | */ |
||
411 | 1 | public function getInternalName() |
|
412 | { |
||
413 | 1 | return $this->internalName; |
|
414 | } |
||
415 | |||
416 | /** |
||
417 | * @param string $internalName |
||
418 | * |
||
419 | * @return Folder |
||
420 | */ |
||
421 | 1 | public function setInternalName($internalName) |
|
422 | { |
||
423 | 1 | $this->internalName = $internalName; |
|
424 | |||
425 | 1 | return $this; |
|
426 | } |
||
427 | |||
428 | /** |
||
429 | * @return string |
||
430 | */ |
||
431 | 1 | public function __toString() |
|
432 | { |
||
433 | 1 | return $this->getName(); |
|
434 | } |
||
435 | |||
436 | /** |
||
437 | * @return string |
||
438 | */ |
||
439 | 2 | public function getName() |
|
440 | { |
||
441 | 2 | return $this->name; |
|
442 | } |
||
443 | |||
444 | /** |
||
445 | * @param string $name |
||
446 | * |
||
447 | * @return Folder |
||
448 | */ |
||
449 | 2 | public function setName($name) |
|
450 | { |
||
451 | 2 | $this->name = $name; |
|
452 | |||
453 | 2 | return $this; |
|
454 | } |
||
455 | |||
456 | /** |
||
457 | * @param int $lft |
||
458 | * |
||
459 | * @return Folder |
||
460 | */ |
||
461 | 1 | public function setLeft($lft) |
|
462 | { |
||
463 | 1 | $this->lft = $lft; |
|
464 | |||
465 | 1 | return $this; |
|
466 | } |
||
467 | |||
468 | /** |
||
469 | * @return int |
||
470 | */ |
||
471 | 1 | public function getLeft() |
|
472 | { |
||
473 | 1 | return $this->lft; |
|
474 | } |
||
475 | |||
476 | /** |
||
477 | * @param int $lvl |
||
478 | * |
||
479 | * @return Folder |
||
480 | */ |
||
481 | 2 | public function setLevel($lvl) |
|
482 | { |
||
483 | 2 | $this->lvl = $lvl; |
|
484 | |||
485 | 2 | return $this; |
|
486 | } |
||
487 | |||
488 | /** |
||
489 | * @param int $rgt |
||
490 | * |
||
491 | * @return Folder |
||
492 | */ |
||
493 | 1 | public function setRight($rgt) |
|
494 | { |
||
495 | 1 | $this->rgt = $rgt; |
|
496 | |||
497 | 1 | return $this; |
|
498 | } |
||
499 | |||
500 | /** |
||
501 | * @return int |
||
502 | */ |
||
503 | 1 | public function getRight() |
|
504 | { |
||
505 | 1 | return $this->rgt; |
|
506 | } |
||
507 | |||
508 | /** |
||
509 | * @return string |
||
510 | */ |
||
511 | 1 | public function getOptionLabel() |
|
512 | { |
||
513 | 1 | return str_repeat( |
|
514 | 1 | '-', |
|
515 | 1 | $this->getLevel() |
|
516 | 1 | ) . ' ' . $this->getName(); |
|
517 | } |
||
518 | |||
519 | /** |
||
520 | * @return int |
||
521 | */ |
||
522 | 2 | public function getLevel() |
|
523 | { |
||
524 | 2 | return $this->lvl; |
|
525 | } |
||
526 | |||
527 | /** |
||
528 | * @ORM\PreUpdate |
||
529 | */ |
||
530 | 1 | public function preUpdate() |
|
531 | { |
||
532 | 1 | $this->setUpdatedAt(new \DateTime()); |
|
533 | 1 | } |
|
534 | } |
||
535 |
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.