1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the CMS Kernel library. |
5
|
|
|
* |
6
|
|
|
* Copyright (c) 2016 LIN3S <[email protected]> |
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 LIN3S\CMSKernel\Application\Query\Menu; |
13
|
|
|
|
14
|
|
|
use LIN3S\CMSKernel\Application\DataTransformer\Menu\MenuItemDataTransformer; |
15
|
|
|
use LIN3S\CMSKernel\Domain\Model\Menu\Menu; |
16
|
|
|
use LIN3S\CMSKernel\Domain\Model\Menu\MenuDoesNotExistException; |
17
|
|
|
use LIN3S\CMSKernel\Domain\Model\Menu\MenuId; |
18
|
|
|
use LIN3S\CMSKernel\Domain\Model\Menu\MenuRepository; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @author Beñat Espiña <[email protected]> |
22
|
|
|
*/ |
23
|
|
|
class MenuOfIdHandler |
24
|
|
|
{ |
25
|
|
|
private $repository; |
26
|
|
|
private $menuItemDataTransformer; |
27
|
|
|
|
28
|
|
|
public function __construct(MenuRepository $repository, MenuItemDataTransformer $menuItemDataTransformer) |
29
|
|
|
{ |
30
|
|
|
$this->repository = $repository; |
31
|
|
|
$this->menuItemDataTransformer = $menuItemDataTransformer; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
View Code Duplication |
public function __invoke(MenuOfIdQuery $query) |
|
|
|
|
35
|
|
|
{ |
36
|
|
|
$menu = $this->repository->menuOfId( |
37
|
|
|
MenuId::generate( |
38
|
|
|
$query->menuId() |
39
|
|
|
) |
40
|
|
|
); |
41
|
|
|
if (!$menu instanceof Menu) { |
42
|
|
|
throw new MenuDoesNotExistException(); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
$translation = $menu->{$query->locale()}(); |
46
|
|
|
|
47
|
|
|
return $this->tree($translation->tree()); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
private function tree($items) |
51
|
|
|
{ |
52
|
|
|
$tree = []; |
53
|
|
|
|
54
|
|
|
// Create an associative array with each key being the ID of the item |
55
|
|
|
foreach ($items as &$item) { |
56
|
|
|
$this->menuItemDataTransformer->write($item); |
57
|
|
|
$item = $this->menuItemDataTransformer->read(); |
58
|
|
|
|
59
|
|
|
$tree[$item['id']] = &$item; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
// Loop over the array and add each child to their parent |
63
|
|
|
foreach ($tree as &$item) { |
64
|
|
|
if (!isset($item['parent_id'])) { |
65
|
|
|
continue; |
66
|
|
|
} |
67
|
|
|
$tree[$item['parent_id']]['children'][] = &$item; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
// Loop over the array again and remove any items that don't have a parent of 0; |
71
|
|
|
foreach ($tree as $key => &$item) { |
72
|
|
|
if (!isset($item['parent_id'])) { |
73
|
|
|
continue; |
74
|
|
|
} |
75
|
|
|
unset($tree[$key]); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
// Sanitize the first level items |
79
|
|
|
$result = []; |
80
|
|
|
foreach ($tree as &$item) { |
81
|
|
|
$result[] = &$item['children'][0]; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return $result; |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
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.