Completed
Push — master ( 800225...4b09e1 )
by Beñat
02:30
created

ManageMenuHandler   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 53
Duplicated Lines 35.85 %

Coupling/Cohesion

Components 0
Dependencies 10

Importance

Changes 0
Metric Value
wmc 7
lcom 0
cbo 10
dl 19
loc 53
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A __invoke() 0 23 3
A buildTree() 19 19 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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\MenuId;
16
use LIN3S\CMSKernel\Domain\Model\Menu\MenuItemId;
17
use LIN3S\CMSKernel\Domain\Model\Menu\MenuItemLink;
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
use LIN3S\CMSKernel\Domain\Model\Translation\TranslationDoesNotExistException;
24
25
/**
26
 * @author Beñat Espiña <[email protected]>
27
 */
28
class ManageMenuHandler
29
{
30
    private $repository;
31
32
    public function __construct(MenuRepository $repository)
33
    {
34
        $this->repository = $repository;
35
    }
36
37
    public function __invoke(ManageMenuCommand $command)
38
    {
39
        $menuId = MenuId::generate($command->menuId());
40
        $menuName = new MenuName($command->name());
41
        $locale = new Locale($command->locale());
42
        $menuTranslationId = MenuTranslationId::generate();
43
        $tree = $command->items();
44
45
        $menuTranslation = new MenuTranslation($menuTranslationId, $locale, $menuName);
46
        $this->buildTree($menuTranslation, $tree);
47
48
        $menu = $this->repository->menuOfId($menuId);
49
        if (null === $menu) {
50
            $menu = new Menu($menuId, $menuTranslation);
51
        } else {
52
            try {
53
                $menu->removeTranslation($locale);
54
            } catch (TranslationDoesNotExistException $exception) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
55
            }
56
            $menu->addTranslation($menuTranslation);
57
        }
58
        $this->repository->persist($menu);
59
    }
60
61 View Code Duplication
    private function buildTree(MenuTranslation $menuTranslation, array $items, MenuItemId $parentId = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
62
    {
63
        foreach ($items as $item) {
64
            $menuItemId = MenuItemId::generate();
65
66
            $menuTranslation->addItem(
67
                new MenuItemLink(
68
                    $item['label'],
69
                    $item['url']
70
                ),
71
                $parentId,
72
                $menuItemId
73
            );
74
75
            if (isset($item['children'])) {
76
                $this->buildTree($menuTranslation, $item['children'], $menuItemId);
77
            }
78
        }
79
    }
80
}
81