Completed
Push — master ( 838197...9b1ada )
by Beñat
03:21
created

ManageMenuHandler::__invoke()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 24
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 24
rs 8.9713
c 0
b 0
f 0
cc 3
eloc 18
nc 3
nop 1
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) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
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