ChangeParentMenuItemHandler::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
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\MenuRepository;
19
use LIN3S\CMSKernel\Domain\Model\Translation\Locale;
20
21
/**
22
 * @author Beñat Espiña <[email protected]>
23
 */
24
class ChangeParentMenuItemHandler
25
{
26
    private $repository;
27
28
    public function __construct(MenuRepository $repository)
29
    {
30
        $this->repository = $repository;
31
    }
32
33
    public function __invoke(ChangeParentMenuItemCommand $command)
34
    {
35
        $menu = $this->repository->menuOfId(
36
            MenuId::generate(
37
                $command->menuId()
38
            )
39
        );
40
        if (!$menu instanceof Menu) {
41
            throw new MenuDoesNotExistException();
42
        }
43
        $translation = $menu->{$command->locale()}();
44
45
        $menuItemParent = $translation->item(
46
            null !== $command->menuItemParentId()
47
                ? MenuItemId::generate($command->menuItemParentId())
48
                : null
49
        );
50
51
        $translation->item(
52
            MenuItemId::generate(
53
                $command->menuItemId()
54
            )
55
        )->changeParent(
56
            $menuItemParent->id()
57
        );
58
59
        $menu->removeTranslation(
60
            new Locale(
61
                $command->locale()
62
            )
63
        );
64
        $menu->addTranslation($translation);
65
        $this->repository->persist($menu);
66
    }
67
}
68