MenuTranslation   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 6
dl 0
loc 72
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A addItem() 0 16 2
A removeItem() 0 4 1
A id() 0 4 1
A name() 0 4 1
A tree() 0 4 1
A item() 0 15 4
A __toString() 0 4 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\Domain\Model\Menu;
13
14
use LIN3S\CMSKernel\Domain\Model\Translation\Locale;
15
use LIN3S\CMSKernel\Domain\Model\Translation\Translation;
16
17
/**
18
 * @author Beñat Espiña <[email protected]>
19
 */
20
class MenuTranslation extends Translation
21
{
22
    private $id;
23
    private $name;
24
    private $tree;
25
26
    public function __construct(MenuTranslationId $id, Locale $locale, MenuName $name)
27
    {
28
        parent::__construct($locale);
29
        $this->id = $id;
30
        $this->name = $name;
31
        $this->tree = new MenuItemCollection();
32
    }
33
34
    public function addItem(
35
        MenuItemLink $link,
36
        MenuItemOrder $order,
37
        MenuItemId $parentId = null,
38
        MenuItemId $menuItemId = null
39
    ) {
40
        $menuItemId = null === $menuItemId ? MenuItemId::generate() : $menuItemId;
41
42
        $menuItem = new MenuItem($menuItemId, $link, $order, $parentId);
43
        $menuItemReflection = new \ReflectionClass($menuItem);
44
        $menuTranslation = $menuItemReflection->getProperty('menuTranslation');
45
        $menuTranslation->setAccessible(true);
46
        $menuTranslation->setValue($menuItem, $this);
47
48
        $this->tree->add($menuItem);
49
    }
50
51
    public function removeItem(MenuItemId $id)
52
    {
53
        $this->tree->removeById($id);
54
    }
55
56
    public function id()
57
    {
58
        return $this->id;
59
    }
60
61
    public function name()
62
    {
63
        return $this->name;
64
    }
65
66
    public function tree()
67
    {
68
        return $this->tree;
69
    }
70
71
    public function item(MenuItemId $id)
72
    {
73
        $menuItem = null;
74
        foreach ($this->tree() as $item) {
75
            if ($id->equals($item->id())) {
76
                $menuItem = $item;
77
                break;
78
            }
79
        }
80
        if (!$menuItem instanceof MenuItem) {
81
            throw new MenuItemDoesNotExistException();
82
        }
83
84
        return $menuItem;
85
    }
86
87
    public function __toString()
88
    {
89
        return (string) $this->id->id();
90
    }
91
}
92