Completed
Push — master ( 0e9bc5...7ef25f )
by Ryan
15:00
created

Module::addSection()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 13
Ratio 100 %

Importance

Changes 0
Metric Value
cc 2
eloc 7
nc 2
nop 3
dl 13
loc 13
rs 9.4285
c 0
b 0
f 0
1
<?php namespace Anomaly\Streams\Platform\Addon\Module;
2
3
use Anomaly\Streams\Platform\Addon\Addon;
4
5
/**
6
 * Class Module
7
 *
8
 * @link    http://pyrocms.com/
9
 * @author  PyroCMS, Inc. <[email protected]>
10
 * @author  Ryan Thompson <[email protected]>
11
 */
12
class Module extends Addon
13
{
14
15
    /**
16
     * The module's sections.
17
     *
18
     * @var string|array
19
     */
20
    protected $sections = [];
21
22
    /**
23
     * The module's menu.
24
     *
25
     * @var string|array
26
     */
27
    protected $menu = [];
28
29
    /**
30
     * The module's icon.
31
     *
32
     * @var string
33
     */
34
    protected $icon = 'fa fa-puzzle-piece';
35
36
    /**
37
     * The navigation flag.
38
     *
39
     * @var bool
40
     */
41
    protected $navigation = true;
42
43
    /**
44
     * The installed flag.
45
     *
46
     * @var bool
47
     */
48
    protected $installed = false;
49
50
    /**
51
     * The enabled flag.
52
     *
53
     * @var bool
54
     */
55
    protected $enabled = false;
56
57
    /**
58
     * The active flag.
59
     *
60
     * @var bool
61
     */
62
    protected $active = false;
63
64
    /**
65
     * Get the module's tag class.
66
     *
67
     * @var string
68
     */
69
    protected $tag = 'Anomaly\Streams\Platform\Addon\Module\ModuleTag';
70
71
    /**
72
     * Get the module's sections.
73
     *
74
     * @return array
75
     */
76
    public function getSections()
77
    {
78
        return $this->sections;
79
    }
80
81
    /**
82
     * Set the sections.
83
     *
84
     * @param array $sections
85
     * @return $this
86
     */
87
    public function setSections($sections)
88
    {
89
        $this->sections = $sections;
90
91
        return $this;
92
    }
93
94
    /**
95
     * Add a section.
96
     *
97
     * @param        $slug
98
     * @param  array $section
99
     * @param null   $position
100
     * @return $this
101
     */
102 View Code Duplication
    public function addSection($slug, array $section, $position = null)
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...
103
    {
104
        if ($position === null) {
105
            $position = count($this->sections) + 1;
106
        }
107
108
        $front = array_slice($this->sections, 0, $position, true);
109
        $back  = array_slice($this->sections, $position, count($this->sections) - $position, true);
110
111
        $this->sections = $front + [$slug => $section] + $back;
112
113
        return $this;
114
    }
115
116
    /**
117
     * Add a section button.
118
     *
119
     * @param        $section
120
     * @param        $slug
121
     * @param  array $button
122
     * @param null   $position
123
     * @return $this
124
     */
125 View Code Duplication
    public function addSectionButton($section, $slug, array $button, $position = null)
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...
126
    {
127
        $buttons = (array)array_get($this->sections, "{$section}.buttons");
128
129
        if ($position === null) {
130
            $position = count($buttons) + 1;
131
        }
132
133
        $front = array_slice($buttons, 0, $position, true);
134
        $back  = array_slice($buttons, $position, count($buttons) - $position, true);
135
136
        $buttons = $front + [$slug => $button] + $back;
137
138
        array_set($this->sections, "{$section}.buttons", $buttons);
0 ignored issues
show
Bug introduced by
It seems like $this->sections can also be of type string; however, array_set() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
139
140
        return $this;
141
    }
142
143
    /**
144
     * Get the module's menu.
145
     *
146
     * @return array|string
147
     */
148
    public function getMenu()
149
    {
150
        return $this->menu;
151
    }
152
153
    /**
154
     * Get the module's icon.
155
     *
156
     * @return string|null|false
157
     */
158
    public function getIcon()
159
    {
160
        return $this->icon;
161
    }
162
163
    /**
164
     * Get the navigation flag.
165
     *
166
     * @return bool
167
     */
168
    public function getNavigation()
169
    {
170
        return $this->navigation;
171
    }
172
173
    /**
174
     * Set the navigation flag.
175
     *
176
     * @param $navigation
177
     * @return $this
178
     */
179
    public function setNavigation($navigation)
180
    {
181
        $this->navigation = $navigation;
182
183
        return $this;
184
    }
185
186
    /**
187
     * Set the installed flag.
188
     *
189
     * @param  $installed
190
     * @return $this
191
     */
192
    public function setInstalled($installed)
193
    {
194
        $this->installed = $installed;
195
196
        return $this;
197
    }
198
199
    /**
200
     * Get the installed flag.
201
     *
202
     * @return bool
203
     */
204
    public function isInstalled()
205
    {
206
        return $this->installed;
207
    }
208
209
    /**
210
     * Set the enabled flag.
211
     *
212
     * @param  $enabled
213
     * @return $this
214
     */
215
    public function setEnabled($enabled)
216
    {
217
        $this->enabled = $enabled;
218
219
        return $this;
220
    }
221
222
    /**
223
     * Get the enabled flag.
224
     *
225
     * @return bool
226
     */
227
    public function isEnabled()
228
    {
229
        return $this->enabled && $this->installed;
230
    }
231
232
    /**
233
     * Set the active flag.
234
     *
235
     * @param  $active
236
     * @return $this
237
     */
238
    public function setActive($active)
239
    {
240
        $this->active = $active;
241
242
        return $this;
243
    }
244
245
    /**
246
     * Get the active flag.
247
     *
248
     * @return bool
249
     */
250
    public function isActive()
251
    {
252
        return $this->active;
253
    }
254
255
    /**
256
     * Get the module's presenter.
257
     *
258
     * @return ModulePresenter
259
     */
260
    public function getPresenter()
261
    {
262
        return app()->make('Anomaly\Streams\Platform\Addon\Module\ModulePresenter', ['object' => $this]);
263
    }
264
}
265