Completed
Push — master ( 1f36be...d8b059 )
by Beñat
03:59
created

AddMenuHandler::__construct()   A

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