Completed
Push — master ( 800225...4b09e1 )
by Beñat
02:30
created

ManageMenuTranslationHandler::__invoke()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 13
nc 1
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\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\MenuName;
20
use LIN3S\CMSKernel\Domain\Model\Menu\MenuRepository;
21
use LIN3S\CMSKernel\Domain\Model\Menu\MenuTranslation;
22
use LIN3S\CMSKernel\Domain\Model\Menu\MenuTranslationId;
23
use LIN3S\CMSKernel\Domain\Model\Translation\Locale;
24
25
/**
26
 * @author Beñat Espiña <[email protected]>
27
 */
28
class ManageMenuTranslationHandler
29
{
30
    private $repository;
31
32
    public function __construct(MenuRepository $repository)
33
    {
34
        $this->repository = $repository;
35
    }
36
37
    public function __invoke(ManageMenuTranslationCommand $command)
38
    {
39
        $menuId = MenuId::generate($command->menuId());
40
        $menuName = new MenuName($command->name());
41
        $locale = new Locale($command->locale());
42
        $menuTranslationId = MenuTranslationId::generate();
43
        $tree = $command->items();
44
45
        $menu = $this->repository->menuOfId($menuId);
46
        $this->checkMenuExists($menu);
47
48
        $menuTranslation = new MenuTranslation($menuTranslationId, $locale, $menuName);
49
        $this->buildTree($menuTranslation, $tree);
50
51
        $menu->removeTranslation($locale);
52
        $menu->addTranslation($menuTranslation);
53
        $this->repository->persist($menu);
54
    }
55
56
    private function checkMenuExists(Menu $menu = null)
57
    {
58
        if (!$menu instanceof Menu) {
59
            throw new MenuDoesNotExistException();
60
        }
61
    }
62
63 View Code Duplication
    private function buildTree(MenuTranslation $menuTranslation, array $items, MenuItemId $parentId = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
64
    {
65
        foreach ($items as $item) {
66
            $menuItemId = MenuItemId::generate();
67
68
            $menuTranslation->addItem(
69
                new MenuItemLink(
70
                    $item['label'],
71
                    $item['url']
72
                ),
73
                $parentId,
74
                $menuItemId
75
            );
76
77
            if (isset($item['children'])) {
78
                $this->buildTree($menuTranslation, $item['children'], $menuItemId);
79
            }
80
        }
81
    }
82
}
83