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\MenuDoesNotExistException; |
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 ManageMenuTranslationHandler |
31
|
|
|
{ |
32
|
|
|
private $repository; |
33
|
|
|
|
34
|
|
|
public function __construct(MenuRepository $repository) |
35
|
|
|
{ |
36
|
|
|
$this->repository = $repository; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function __invoke(ManageMenuTranslationCommand $command) |
40
|
|
|
{ |
41
|
|
|
$menuId = MenuId::generate($command->menuId()); |
42
|
|
|
$menuName = new MenuName($command->name()); |
43
|
|
|
$locale = new Locale($command->locale()); |
44
|
|
|
$menuTranslationId = MenuTranslationId::generate(); |
45
|
|
|
$tree = $command->items(); |
46
|
|
|
|
47
|
|
|
$menu = $this->repository->menuOfId($menuId); |
48
|
|
|
$this->checkMenuExists($menu); |
49
|
|
|
|
50
|
|
|
$menuTranslation = new MenuTranslation($menuTranslationId, $locale, $menuName); |
51
|
|
|
$this->buildTree($menuTranslation, $tree); |
52
|
|
|
|
53
|
|
|
try { |
54
|
|
|
$menu->removeTranslation($locale); |
55
|
|
|
} catch (TranslationDoesNotExistException $exception) { |
|
|
|
|
56
|
|
|
} |
57
|
|
|
$menu->addTranslation($menuTranslation); |
58
|
|
|
$this->repository->persist($menu); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
private function checkMenuExists(Menu $menu = null) |
62
|
|
|
{ |
63
|
|
|
if (!$menu instanceof Menu) { |
64
|
|
|
throw new MenuDoesNotExistException(); |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
private function buildTree(MenuTranslation $menuTranslation, array $items, MenuItemId $parentId = null) |
69
|
|
|
{ |
70
|
|
|
foreach ($items as $order => $item) { |
71
|
|
|
++$order; |
72
|
|
|
$menuItemId = MenuItemId::generate(); |
73
|
|
|
|
74
|
|
|
$menuTranslation->addItem( |
75
|
|
|
new MenuItemLink( |
76
|
|
|
$item['label'], |
77
|
|
|
$item['url'] |
78
|
|
|
), |
79
|
|
|
new MenuItemOrder( |
80
|
|
|
$order |
81
|
|
|
), |
82
|
|
|
$parentId, |
83
|
|
|
$menuItemId |
84
|
|
|
); |
85
|
|
|
|
86
|
|
|
if (isset($item['children'])) { |
87
|
|
|
$this->buildTree($menuTranslation, $item['children'], $menuItemId); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|