AddMenuHandler   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 10

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 10
dl 0
loc 42
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B __invoke() 0 32 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\MenuCode;
16
use LIN3S\CMSKernel\Domain\Model\Menu\MenuId;
17
use LIN3S\CMSKernel\Domain\Model\Menu\MenuIsAlreadyExistsException;
18
use LIN3S\CMSKernel\Domain\Model\Menu\MenuName;
19
use LIN3S\CMSKernel\Domain\Model\Menu\MenuRepository;
20
use LIN3S\CMSKernel\Domain\Model\Menu\MenuTranslation;
21
use LIN3S\CMSKernel\Domain\Model\Menu\MenuTranslationId;
22
use LIN3S\CMSKernel\Domain\Model\Translation\Locale;
23
24
/**
25
 * @author Beñat Espiña <[email protected]>
26
 */
27
class AddMenuHandler
28
{
29
    private $repository;
30
31
    public function __construct(MenuRepository $repository)
32
    {
33
        $this->repository = $repository;
34
    }
35
36
    public function __invoke(AddMenuCommand $command)
37
    {
38
        if (null !== $menuId = $command->menuId()) {
39
            $menu = $this->repository->menuOfId(
40
                MenuId::generate(
41
                    $menuId
42
                )
43
            );
44
            if ($menu instanceof Menu) {
45
                throw new MenuIsAlreadyExistsException();
46
            }
47
        }
48
49
        $menu = new Menu(
50
            MenuId::generate(
51
                $command->menuId()
52
            ),
53
            new MenuCode(
54
                $command->code()
55
            ),
56
            new MenuTranslation(
57
                MenuTranslationId::generate(),
58
                new Locale(
59
                    $command->locale()
60
                ),
61
                new MenuName(
62
                    $command->name()
63
                )
64
            )
65
        );
66
        $this->repository->persist($menu);
67
    }
68
}
69