MenuMenuItem   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 142
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 26
c 3
b 0
f 0
dl 0
loc 142
rs 10
wmc 14

14 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
A getText() 0 3 1
A getSubMenu() 0 3 1
A showItemExtra() 0 3 1
A showsItemExtra() 0 3 1
A canSelect() 0 3 1
A getSelectAction() 0 4 1
A showSubMenu() 0 4 1
A hideItemExtra() 0 3 1
A getStyle() 0 3 1
A setText() 0 3 1
A getRows() 0 3 1
A setStyle() 0 3 1
A propagateStyles() 0 4 1
1
<?php
2
3
namespace PhpSchool\CliMenu\MenuItem;
4
5
use PhpSchool\CliMenu\CliMenu;
6
use PhpSchool\CliMenu\MenuStyle;
7
use PhpSchool\CliMenu\Style\ItemStyle;
8
use PhpSchool\CliMenu\Style\SelectableStyle;
9
10
/**
11
 * @author Michael Woodward <[email protected]>
12
 */
13
class MenuMenuItem implements MenuItemInterface, PropagatesStyles
14
{
15
    /**
16
     * @var string
17
     */
18
    private $text;
19
20
    /**
21
     * @var CliMenu
22
     */
23
    private $subMenu;
24
25
    /**
26
     * @var bool
27
     */
28
    private $showItemExtra = false;
29
30
    /**
31
     * @var bool
32
     */
33
    private $disabled;
34
35
    /**
36
     * @var SelectableStyle
37
     */
38
    private $style;
39
40
    public function __construct(
41
        string $text,
42
        CliMenu $subMenu,
43
        bool $disabled = false
44
    ) {
45
        $this->text = $text;
46
        $this->subMenu = $subMenu;
47
        $this->disabled = $disabled;
48
49
        $this->style = new SelectableStyle();
50
    }
51
52
    /**
53
     * The output text for the item
54
     */
55
    public function getRows(MenuStyle $style, bool $selected = false) : array
56
    {
57
        return (new SelectableItemRenderer())->render($style, $this, $selected, $this->disabled);
58
    }
59
60
    /**
61
     * Return the raw string of text
62
     */
63
    public function getText() : string
64
    {
65
        return $this->text;
66
    }
67
68
    /**
69
     * Set the raw string of text
70
     */
71
    public function setText(string $text) : void
72
    {
73
        $this->text = $text;
74
    }
75
76
    /**
77
     * Execute the items callable if required
78
     */
79
    public function getSelectAction() : ?callable
80
    {
81
        return function (CliMenu $menu) {
82
            $this->showSubMenu($menu);
83
        };
84
    }
85
86
    /**
87
     * Returns the sub menu
88
     */
89
    public function getSubMenu() : CliMenu
90
    {
91
        return $this->subMenu;
92
    }
93
94
    /**
95
     * Display the sub menu
96
     */
97
    public function showSubMenu(CliMenu $parentMenu) : void
98
    {
99
        $parentMenu->closeThis();
100
        $this->subMenu->open();
101
    }
102
103
    /**
104
     * Can the item be selected
105
     */
106
    public function canSelect() : bool
107
    {
108
        return !$this->disabled;
109
    }
110
111
    /**
112
     * Enable showing item extra
113
     */
114
    public function showItemExtra() : void
115
    {
116
        $this->showItemExtra = true;
117
    }
118
119
    /**
120
     * Whether or not we are showing item extra
121
     */
122
    public function showsItemExtra() : bool
123
    {
124
        return $this->showItemExtra;
125
    }
126
127
    /**
128
     * Disable showing item extra
129
     */
130
    public function hideItemExtra() : void
131
    {
132
        $this->showItemExtra = false;
133
    }
134
135
    /**
136
     * @return SelectableStyle
137
     */
138
    public function getStyle() : ItemStyle
139
    {
140
        return $this->style;
141
    }
142
143
    public function setStyle(SelectableStyle $style) : void
144
    {
145
        $this->style = $style;
146
    }
147
148
    /**
149
     * @inheritDoc
150
     */
151
    public function propagateStyles(CliMenu $parent): void
152
    {
153
        $this->getSubMenu()->importStyles($parent);
154
        $this->getSubMenu()->propagateStyles();
155
    }
156
}
157