ManageMenuTranslationHandler   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 11

Importance

Changes 0
Metric Value
wmc 8
lcom 0
cbo 11
dl 0
loc 62
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A __invoke() 0 21 2
A checkMenuExists() 0 6 2
A buildTree() 0 23 3
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) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
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