RemoveMenuItemHandler::__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 RemoveMenuItemHandler
25
{
26
    private $repository;
27
28
    public function __construct(MenuRepository $repository)
29
    {
30
        $this->repository = $repository;
31
    }
32
33
    public function __invoke(RemoveMenuItemCommand $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
        $locale = new Locale(
44
            $command->locale()
45
        );
46
        $translation = $menu->{$command->locale()}();
47
48
        $translation->removeItem(
49
            MenuItemId::generate(
50
                $command->menuItemId()
51
            )
52
        );
53
        $menu->removeTranslation($locale);
54
        $menu->addTranslation($translation);
55
        $this->repository->persist($menu);
56
    }
57
}
58