Item   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 165
Duplicated Lines 0 %

Coupling/Cohesion

Components 5
Dependencies 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 15
c 1
b 0
f 0
lcom 5
cbo 3
dl 0
loc 165
rs 10

13 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A to() 0 6 1
A title() 0 6 1
A badge() 0 6 1
A subMenu() 0 9 1
A active() 0 11 2
A getLink() 0 4 1
A getTitle() 0 4 1
A getBadge() 0 4 1
A getSubMenu() 0 4 1
A hasSubMenu() 0 4 2
A hasBadge() 0 4 1
A isActive() 0 4 1
1
<?php
2
3
namespace Roboc\Menu;
4
5
use Closure;
6
use Roboc\Support\Interfaces\MenuItemInterface;
7
8
/**
9
 * Class Item
10
 * @package Roboc\Menu
11
 */
12
class Item implements MenuItemInterface
13
{
14
    /**
15
     * @var string
16
     */
17
    protected $title;
18
19
    /**
20
     * @var string
21
     */
22
    protected $link;
23
24
    /**
25
     * @var bool
26
     */
27
    protected $active = false;
28
29
    /**
30
     * @var Badge
31
     */
32
    protected $badge;
33
34
    /**
35
     * Parent Menu instance
36
     *
37
     * @var Menu
38
     */
39
    protected $menu;
40
41
    /**
42
     * Sub menu instance
43
     *
44
     * @var Menu
45
     */
46
    protected $subMenu;
47
48
    /**
49
     * Item constructor.
50
     * @param Menu $menu
51
     */
52
    public function __construct( Menu $menu )
53
    {
54
        $this->menu = $menu;
55
    }
56
57
    /**
58
     * @param string $link
59
     * @return $this
60
     */
61
    public function to( $link )
62
    {
63
        $this->link = $link;
64
65
        return $this;
66
    }
67
68
    /**
69
     * @param string $title
70
     * @return $this
71
     */
72
    public function title( $title )
73
    {
74
        $this->title = $title;
75
76
        return $this;
77
    }
78
79
    /**
80
     * @param string $value
81
     * @param array $attributes
82
     * @return $this
83
     */
84
    public function badge( $value, array $attributes = [ ] )
85
    {
86
        $this->badge = new Badge( $value, $attributes );
87
88
        return $this;
89
    }
90
91
    /**
92
     * @param Closure $callback
93
     * @return $this
94
     */
95
    public function subMenu( Closure $callback )
96
    {
97
        $this->subMenu = new Menu( null );
98
        $this->subMenu->setParent( $this );
99
100
        $callback( $this->subMenu );
101
102
        return $this;
103
    }
104
105
    /**
106
     * @param $isActive
107
     * @return $this
108
     */
109
    public function active( $isActive )
110
    {
111
        $this->active = $isActive;
112
113
        if( $this->menu->hasParent() )
114
        {
115
            $this->menu->parent()->active( true );
116
        }
117
118
        return $this;
119
    }
120
121
    /**
122
     * @return string
123
     */
124
    public function getLink()
125
    {
126
        return $this->link;
127
    }
128
129
    /**
130
     * @return string
131
     */
132
    public function getTitle()
133
    {
134
        return $this->title;
135
    }
136
137
    /**
138
     * @return Badge|null
139
     */
140
    public function getBadge()
141
    {
142
        return $this->badge;
143
    }
144
145
    /**
146
     * @return Menu
147
     */
148
    public function getSubMenu()
149
    {
150
        return $this->subMenu;
151
    }
152
153
    /**
154
     * @return bool
155
     */
156
    public function hasSubMenu()
157
    {
158
        return $this->subMenu !== null && $this->subMenu->hasItems();
159
    }
160
161
    /**
162
     * @return bool
163
     */
164
    public function hasBadge()
165
    {
166
        return $this->badge !== null;
167
    }
168
169
    /**
170
     * @return bool
171
     */
172
    public function isActive()
173
    {
174
        return (bool) $this->active;
175
    }
176
}
177