AddMenuTranslationHandler::__invoke()   B
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 24
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 24
rs 8.9713
c 0
b 0
f 0
cc 2
eloc 14
nc 2
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\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 AddMenuTranslationHandler
27
{
28
    private $repository;
29
30
    public function __construct(MenuRepository $repository)
31
    {
32
        $this->repository = $repository;
33
    }
34
35
    public function __invoke(AddMenuTranslationCommand $command)
36
    {
37
        $menu = $this->repository->menuOfId(
38
            MenuId::generate(
39
                $command->menuId()
40
            )
41
        );
42
        if (!$menu instanceof Menu) {
43
            throw new MenuDoesNotExistException();
44
        }
45
46
        $menu->addTranslation(
47
            new MenuTranslation(
48
                MenuTranslationId::generate(),
49
                new Locale(
50
                    $command->locale()
51
                ),
52
                new MenuName(
53
                    $command->name()
54
                )
55
            )
56
        );
57
        $this->repository->persist($menu);
58
    }
59
}
60