|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the CMS Kernel package. |
|
5
|
|
|
* |
|
6
|
|
|
* Copyright (c) 2016-present 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\Command\Menu; |
|
13
|
|
|
|
|
14
|
|
|
use LIN3S\CMSKernel\Domain\Model\Menu\Menu; |
|
15
|
|
|
use LIN3S\CMSKernel\Domain\Model\Menu\MenuCode; |
|
16
|
|
|
use LIN3S\CMSKernel\Domain\Model\Menu\MenuId; |
|
17
|
|
|
use LIN3S\CMSKernel\Domain\Model\Menu\MenuItemId; |
|
18
|
|
|
use LIN3S\CMSKernel\Domain\Model\Menu\MenuItemLink; |
|
19
|
|
|
use LIN3S\CMSKernel\Domain\Model\Menu\MenuItemOrder; |
|
20
|
|
|
use LIN3S\CMSKernel\Domain\Model\Menu\MenuName; |
|
21
|
|
|
use LIN3S\CMSKernel\Domain\Model\Menu\MenuRepository; |
|
22
|
|
|
use LIN3S\CMSKernel\Domain\Model\Menu\MenuTranslation; |
|
23
|
|
|
use LIN3S\CMSKernel\Domain\Model\Menu\MenuTranslationId; |
|
24
|
|
|
use LIN3S\CMSKernel\Domain\Model\Translation\Locale; |
|
25
|
|
|
use LIN3S\CMSKernel\Domain\Model\Translation\TranslationDoesNotExistException; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @author Beñat Espiña <[email protected]> |
|
29
|
|
|
*/ |
|
30
|
|
|
class ManageMenuHandler |
|
31
|
|
|
{ |
|
32
|
|
|
private $repository; |
|
33
|
|
|
|
|
34
|
|
|
public function __construct(MenuRepository $repository) |
|
35
|
|
|
{ |
|
36
|
|
|
$this->repository = $repository; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
public function __invoke(ManageMenuCommand $command) |
|
40
|
|
|
{ |
|
41
|
|
|
$menuId = MenuId::generate($command->id()); |
|
42
|
|
|
$menuCode = new MenuCode($command->code()); |
|
43
|
|
|
$menuName = new MenuName($command->name()); |
|
44
|
|
|
$locale = new Locale($command->locale()); |
|
45
|
|
|
$menuTranslationId = MenuTranslationId::generate(); |
|
46
|
|
|
$tree = $command->items(); |
|
47
|
|
|
|
|
48
|
|
|
$menuTranslation = new MenuTranslation($menuTranslationId, $locale, $menuName); |
|
49
|
|
|
$this->buildTree($menuTranslation, $tree); |
|
50
|
|
|
|
|
51
|
|
|
$menu = $this->repository->menuOfId($menuId); |
|
52
|
|
|
if (null === $menu) { |
|
53
|
|
|
$menu = new Menu($menuId, $menuCode, $menuTranslation); |
|
54
|
|
|
} else { |
|
55
|
|
|
try { |
|
56
|
|
|
$menu->removeTranslation($locale); |
|
57
|
|
|
} catch (TranslationDoesNotExistException $exception) { |
|
|
|
|
|
|
58
|
|
|
} |
|
59
|
|
|
$menu->addTranslation($menuTranslation); |
|
60
|
|
|
} |
|
61
|
|
|
$this->repository->persist($menu); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
private function buildTree(MenuTranslation $menuTranslation, array $items, MenuItemId $parentId = null) |
|
65
|
|
|
{ |
|
66
|
|
|
foreach ($items as $order => $item) { |
|
67
|
|
|
++$order; |
|
68
|
|
|
$menuItemId = MenuItemId::generate(); |
|
69
|
|
|
|
|
70
|
|
|
$menuTranslation->addItem( |
|
71
|
|
|
new MenuItemLink( |
|
72
|
|
|
$item['label'], |
|
73
|
|
|
$item['url'] |
|
74
|
|
|
), |
|
75
|
|
|
new MenuItemOrder( |
|
76
|
|
|
$order |
|
77
|
|
|
), |
|
78
|
|
|
$parentId, |
|
79
|
|
|
$menuItemId |
|
80
|
|
|
); |
|
81
|
|
|
|
|
82
|
|
|
if (isset($item['children']) && count($item['children']) > 0) { |
|
83
|
|
|
$this->buildTree($menuTranslation, $item['children'], $menuItemId); |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|