AddMenuItemHandler   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 9

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
lcom 0
cbo 9
dl 0
loc 46
rs 10
c 1
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B __invoke() 0 36 4
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\MenuRepository;
21
use LIN3S\CMSKernel\Domain\Model\Translation\Locale;
22
use LIN3S\CMSKernel\Domain\Model\Translation\TranslationDoesNotExistException;
23
24
/**
25
 * @author Beñat Espiña <[email protected]>
26
 */
27
class AddMenuItemHandler
28
{
29
    private $repository;
30
31
    public function __construct(MenuRepository $repository)
32
    {
33
        $this->repository = $repository;
34
    }
35
36
    public function __invoke(AddMenuItemCommand $command)
37
    {
38
        $menu = $this->repository->menuOfId(
39
            MenuId::generate(
40
                $command->menuId()
41
            )
42
        );
43
        if (!$menu instanceof Menu) {
44
            throw new MenuDoesNotExistException();
45
        }
46
        $locale = new Locale(
47
            $command->locale()
48
        );
49
        $translation = $menu->{$command->locale()}();
50
51
        $translation->addItem(
52
            new MenuItemLink(
53
                $command->menuItemLabel(),
54
                $command->menuItemUrl()
55
            ),
56
            new MenuItemOrder(
57
                $command->menuItemOrder()
58
            ),
59
            null !== $command->menuItemParentId()
60
                ? MenuItemId::generate($command->menuItemParentId())
61
                : null
62
        );
63
64
        try {
65
            $menu->removeTranslation($locale);
66
        } catch (TranslationDoesNotExistException $exception) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
67
        }
68
69
        $menu->addTranslation($translation);
70
        $this->repository->persist($menu);
71
    }
72
}
73