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

MenuTranslation::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 3
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\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(MenuItemLink $link, MenuItemId $parentId = null)
35
    {
36
        $menuItem = new MenuItem(MenuItemId::generate(), $link, $parentId);
37
        $menuItemReflection = new \ReflectionClass($menuItem);
38
        $menuTranslation = $menuItemReflection->getProperty('menuTranslation');
39
        $menuTranslation->setAccessible(true);
40
        $menuTranslation->setValue($menuItem, $this);
41
42
        $this->tree->add($menuItem);
43
    }
44
45
    public function removeItem(MenuItemId $id)
46
    {
47
        $this->tree->removeById($id);
48
    }
49
50
    public function id()
51
    {
52
        return $this->id;
53
    }
54
55
    public function name()
56
    {
57
        return $this->name;
58
    }
59
60
    public function tree()
61
    {
62
        return $this->tree;
63
    }
64
65
    public function item(MenuItemId $id)
66
    {
67
        $menuItem = null;
68
        foreach ($this->tree() as $item) {
69
            if ($id->equals($item->id())) {
70
                $menuItem = $item;
71
                break;
72
            }
73
        }
74
        if (!$menuItem instanceof MenuItem) {
75
            throw new MenuItemDoesNotExistException();
76
        }
77
78
        return $menuItem;
79
    }
80
81
    public function __toString()
82
    {
83
        return (string) $this->id->id();
84
    }
85
}
86