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

MenuOfIdHandler   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 64
Duplicated Lines 23.44 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 5
dl 15
loc 64
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A __invoke() 15 15 2
C tree() 0 36 7

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 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\Query\Menu;
13
14
use LIN3S\CMSKernel\Application\DataTransformer\Menu\MenuItemDataTransformer;
15
use LIN3S\CMSKernel\Domain\Model\Menu\Menu;
16
use LIN3S\CMSKernel\Domain\Model\Menu\MenuDoesNotExistException;
17
use LIN3S\CMSKernel\Domain\Model\Menu\MenuId;
18
use LIN3S\CMSKernel\Domain\Model\Menu\MenuRepository;
19
20
/**
21
 * @author Beñat Espiña <[email protected]>
22
 */
23
class MenuOfIdHandler
24
{
25
    private $repository;
26
    private $menuItemDataTransformer;
27
28
    public function __construct(MenuRepository $repository, MenuItemDataTransformer $menuItemDataTransformer)
29
    {
30
        $this->repository = $repository;
31
        $this->menuItemDataTransformer = $menuItemDataTransformer;
32
    }
33
34 View Code Duplication
    public function __invoke(MenuOfIdQuery $query)
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...
35
    {
36
        $menu = $this->repository->menuOfId(
37
            MenuId::generate(
38
                $query->menuId()
39
            )
40
        );
41
        if (!$menu instanceof Menu) {
42
            throw new MenuDoesNotExistException();
43
        }
44
45
        $translation = $menu->{$query->locale()}();
46
47
        return $this->tree($translation->tree());
48
    }
49
50
    private function tree($items)
51
    {
52
        $tree = [];
53
54
        // Create an associative array with each key being the ID of the item
55
        foreach ($items as &$item) {
56
            $this->menuItemDataTransformer->write($item);
57
            $item = $this->menuItemDataTransformer->read();
58
59
            $tree[$item['id']] = &$item;
60
        }
61
62
        // Loop over the array and add each child to their parent
63
        foreach ($tree as &$item) {
64
            if (!isset($item['parent_id'])) {
65
                continue;
66
            }
67
            $tree[$item['parent_id']]['children'][] = &$item;
68
        }
69
70
        // Loop over the array again and remove any items that don't have a parent of 0;
71
        foreach ($tree as $key => &$item) {
72
            if (!isset($item['parent_id'])) {
73
                continue;
74
            }
75
            unset($tree[$key]);
76
        }
77
78
        // Sanitize the first level items
79
        $result = [];
80
        foreach ($tree as &$item) {
81
            $result[] = &$item['children'][0];
82
        }
83
84
        return $result;
85
    }
86
}
87