Total Complexity | 42 |
Total Lines | 381 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like ControllerSetting often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ControllerSetting, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
17 | trait ControllerSetting |
||
18 | { |
||
19 | |||
20 | private $data = []; |
||
21 | |||
22 | private function defaultData() { |
||
23 | $this->setSearchForm(true); |
||
24 | $this->setButtonDelete(true); |
||
25 | $this->setButtonEdit(true); |
||
26 | $this->setButtonAdd(true); |
||
27 | $this->setButtonCancel(true); |
||
28 | $this->setButtonAddMore(true); |
||
29 | $this->setButtonDetail(true); |
||
30 | $this->setButtonSave(true); |
||
31 | $this->setButtonLimitPage(true); |
||
32 | $this->hideButtonDeleteWhen(function ($row) { return false; }); |
||
|
|||
33 | $this->hideButtonDetailWhen(function ($row) { return false; }); |
||
34 | $this->hideButtonEditWhen(function ($row) { return false; }); |
||
35 | $this->style(function () { return null; }); |
||
36 | $this->javascript(function () { return null; }); |
||
37 | } |
||
38 | |||
39 | public function style(callable $style) { |
||
40 | $this->data['style'] = $style; |
||
41 | } |
||
42 | |||
43 | public function javascript(callable $javascript) { |
||
44 | $this->data['javascript'] = $javascript; |
||
45 | } |
||
46 | |||
47 | /** |
||
48 | * @param callable $condition |
||
49 | */ |
||
50 | public function hideButtonDetailWhen(callable $condition) { |
||
51 | $this->data['hide_button_detail'] = $condition; |
||
52 | } |
||
53 | |||
54 | /** |
||
55 | * @param callable $condition |
||
56 | */ |
||
57 | public function hideButtonEditWhen(callable $condition) { |
||
58 | $this->data['hide_button_edit'] = $condition; |
||
59 | } |
||
60 | |||
61 | /** |
||
62 | * @param callable $condition |
||
63 | */ |
||
64 | public function hideButtonDeleteWhen(callable $condition) { |
||
65 | $this->data['hide_button_delete'] = $condition; |
||
66 | } |
||
67 | |||
68 | /** |
||
69 | * @param $enable boolean |
||
70 | */ |
||
71 | public function setButtonLimitPage($enable) { |
||
72 | $this->data['button_limit_page'] = $enable; |
||
73 | } |
||
74 | |||
75 | public function setPermalink($path) |
||
76 | { |
||
77 | $this->data['permalink'] = $path; |
||
78 | } |
||
79 | |||
80 | public function disableSoftDelete() |
||
81 | { |
||
82 | $this->data['disable_soft_delete'] = true; |
||
83 | } |
||
84 | |||
85 | /** |
||
86 | * @param Closure $callbackQuery |
||
87 | */ |
||
88 | public function hookIndexQuery(Closure $callbackQuery) |
||
89 | { |
||
90 | $this->data['hook_index_query'] = $callbackQuery; |
||
91 | } |
||
92 | |||
93 | /** |
||
94 | * @param Closure $callback |
||
95 | */ |
||
96 | public function hookAfterInsert(Closure $callback) |
||
97 | { |
||
98 | $this->data['hook_after_insert'] = $callback; |
||
99 | } |
||
100 | |||
101 | /** |
||
102 | * @param Closure $callback |
||
103 | */ |
||
104 | public function hookBeforeInsert(Closure $callback) |
||
105 | { |
||
106 | $this->data['hook_before_insert'] = $callback; |
||
107 | } |
||
108 | |||
109 | /** |
||
110 | * @param Closure $callback |
||
111 | */ |
||
112 | public function hookBeforeUpdate(Closure $callback) |
||
113 | { |
||
114 | $this->data['hook_before_update'] = $callback; |
||
115 | } |
||
116 | |||
117 | /** |
||
118 | * @param Closure $callback |
||
119 | */ |
||
120 | public function hookAfterUpdate(Closure $callback) |
||
121 | { |
||
122 | $this->data['hook_after_update'] = $callback; |
||
123 | } |
||
124 | |||
125 | /** |
||
126 | * @param Closure $callbackQuery |
||
127 | */ |
||
128 | public function hookSearchQuery(Closure $callbackQuery) |
||
129 | { |
||
130 | $this->data['hook_search_query'] = $callbackQuery; |
||
131 | } |
||
132 | |||
133 | /** |
||
134 | * @param string $label |
||
135 | * @param string $actionURL |
||
136 | * @param string $fontAwesome_icon |
||
137 | * @param ButtonColor $color |
||
138 | * @param string $attributes |
||
139 | */ |
||
140 | public function addIndexActionButton($label, $actionURL, $fontAwesome_icon=null, $color=null, $attributes = null) |
||
141 | { |
||
142 | $color = ($color)?:ButtonColor::DARK_BLUE; |
||
143 | |||
144 | $model = new IndexActionButtonModel(); |
||
145 | $model->setLabel($label); |
||
146 | $model->setIcon($fontAwesome_icon); |
||
147 | $model->setColor($color); |
||
148 | $model->setUrl($actionURL); |
||
149 | $model->setAttributes($attributes); |
||
150 | |||
151 | $this->data['index_action_button'][] = $model; |
||
152 | } |
||
153 | |||
154 | public function setOrderBy($field, $sort = 'desc') |
||
155 | { |
||
156 | $this->data['order_by'] = [$field, $sort]; |
||
157 | return $this; |
||
158 | } |
||
159 | |||
160 | /** |
||
161 | * @param boolean $enable |
||
162 | * @return $this |
||
163 | */ |
||
164 | public function setSearchForm($enable) { |
||
165 | $this->data['search_form'] = $enable; |
||
166 | return $this; |
||
167 | } |
||
168 | |||
169 | /** |
||
170 | * @param boolean $button_save |
||
171 | * @return ControllerSetting |
||
172 | */ |
||
173 | public function setButtonSave($button_save) |
||
174 | { |
||
175 | $this->data['button_save'] = $button_save; |
||
176 | return $this; |
||
177 | } |
||
178 | |||
179 | /** |
||
180 | * @param boolean $button_cancel |
||
181 | * @return $this |
||
182 | */ |
||
183 | public function setButtonCancel($button_cancel) |
||
184 | { |
||
185 | $this->data['button_cancel'] = $button_cancel; |
||
186 | return $this; |
||
187 | } |
||
188 | |||
189 | |||
190 | /** |
||
191 | * @param $label |
||
192 | * @param callable|string $url_target |
||
193 | * @param callable|string $condition_callback |
||
194 | * @param $fontAwesome_icon |
||
195 | * @param ButtonColor|string $color |
||
196 | */ |
||
197 | public function addActionButton($label, $url_target, $condition_callback=null, $fontAwesome_icon=null, $color=null, $confirmation = false) |
||
198 | { |
||
199 | $new = new AddActionButtonModel(); |
||
200 | $new->setLabel($label); |
||
201 | $new->setIcon($fontAwesome_icon?:"fa fa-bars"); |
||
202 | $new->setColor($color?:"primary"); |
||
203 | $new->setUrl($url_target); |
||
204 | $new->setCondition($condition_callback); |
||
205 | $new->setConfirmation($confirmation); |
||
206 | $this->data['add_action_button'][] = $new; |
||
207 | } |
||
208 | /** |
||
209 | * @param mixed $button_edit |
||
210 | * @param null $condition_callback |
||
211 | * @return ControllerSetting |
||
212 | */ |
||
213 | public function setButtonEdit($button_edit, $condition_callback = null) |
||
214 | { |
||
215 | $this->data['button_edit'] = $button_edit; |
||
216 | $this->data['button_edit_callback'] = $condition_callback; |
||
217 | return $this; |
||
218 | } |
||
219 | |||
220 | /** |
||
221 | * @param mixed $button_detail |
||
222 | * @param null $condition_callback |
||
223 | * @return ControllerSetting |
||
224 | */ |
||
225 | public function setButtonDetail($button_detail, $condition_callback = null) |
||
226 | { |
||
227 | $this->data['button_detail'] = $button_detail; |
||
228 | $this->data['button_detail_callback'] = $condition_callback; |
||
229 | return $this; |
||
230 | } |
||
231 | |||
232 | /** |
||
233 | * @param mixed $button_delete |
||
234 | * @param null $condition_callback |
||
235 | * @return ControllerSetting |
||
236 | */ |
||
237 | public function setButtonDelete($button_delete, $condition_callback = null) |
||
238 | { |
||
239 | $this->data['button_delete'] = $button_delete; |
||
240 | $this->data['button_delete_callback'] = $condition_callback; |
||
241 | return $this; |
||
242 | } |
||
243 | |||
244 | |||
245 | |||
246 | /** |
||
247 | * @param string $alert_message |
||
248 | * @return ControllerSetting |
||
249 | */ |
||
250 | public function setAlertMessage($alert_message) |
||
251 | { |
||
252 | $this->data['alert_message'] = $alert_message; |
||
253 | return $this; |
||
254 | } |
||
255 | |||
256 | |||
257 | /** |
||
258 | * @param string $html_or_view |
||
259 | * @return ControllerSetting |
||
260 | */ |
||
261 | public function setBeforeIndexTable($html_or_view) |
||
265 | } |
||
266 | |||
267 | /** |
||
268 | * @param mixed $html_or_view |
||
269 | * @return ControllerSetting |
||
270 | */ |
||
271 | public function setAfterIndexTable($html_or_view) |
||
272 | { |
||
273 | $this->data['after_index_table'] = $html_or_view; |
||
274 | return $this; |
||
275 | } |
||
276 | |||
277 | /** |
||
278 | * @param callable|string $html_or_view |
||
279 | * @return ControllerSetting |
||
280 | */ |
||
281 | public function setBeforeDetailForm($html_or_view) |
||
282 | { |
||
283 | $this->data['before_detail_form'] = $html_or_view; |
||
284 | return $this; |
||
285 | } |
||
286 | |||
287 | /** |
||
288 | * @param callable|string $html_or_view |
||
289 | * @return ControllerSetting |
||
290 | */ |
||
291 | public function setAfterDetailForm($html_or_view) |
||
292 | { |
||
293 | $this->data['after_detail_form'] = $html_or_view; |
||
294 | return $this; |
||
295 | } |
||
296 | |||
297 | |||
298 | /** |
||
299 | * @param mixed $page_title |
||
300 | * @return ControllerSetting |
||
301 | */ |
||
302 | public function setPageTitle($page_title) |
||
303 | { |
||
304 | $this->data['page_title'] = $page_title; |
||
305 | return $this; |
||
306 | } |
||
307 | |||
308 | |||
309 | /** |
||
310 | * @param mixed $icon |
||
311 | * @return ControllerSetting |
||
312 | */ |
||
313 | public function setPageIcon($icon = "fa fa-table") |
||
317 | } |
||
318 | |||
319 | /** |
||
320 | * @param mixed $table |
||
321 | * @return ControllerSetting |
||
322 | */ |
||
323 | public function setTable($table) |
||
327 | } |
||
328 | |||
329 | /** |
||
330 | * @param int $limit |
||
331 | * @return ControllerSetting |
||
332 | */ |
||
333 | public function setLimit($limit) |
||
334 | { |
||
335 | $this->data['limit'] = $limit; |
||
336 | return $this; |
||
337 | } |
||
338 | |||
339 | /** |
||
340 | * @param bool $button_filter |
||
341 | * @return ControllerSetting |
||
342 | */ |
||
343 | public function setButtonFilter($button_filter) |
||
344 | { |
||
345 | $this->data['button_filter'] = $button_filter; |
||
346 | return $this; |
||
347 | } |
||
348 | |||
349 | |||
350 | /** |
||
351 | * @param bool $button_add_more |
||
352 | * @return ControllerSetting |
||
353 | */ |
||
354 | public function setButtonAddMore($button_add_more) |
||
355 | { |
||
356 | $this->data['button_add_more'] = $button_add_more; |
||
357 | return $this; |
||
358 | } |
||
359 | |||
360 | /** |
||
361 | * @param bool $button_add |
||
362 | * @return ControllerSetting |
||
363 | */ |
||
364 | public function setButtonAdd($button_add) |
||
368 | } |
||
369 | |||
370 | /** |
||
371 | * @param string $head_script |
||
372 | * @return ControllerSetting |
||
373 | */ |
||
374 | public function setHeadScript($head_script) |
||
375 | { |
||
376 | $this->data['head_script'] = $head_script; |
||
377 | return $this; |
||
378 | } |
||
379 | |||
380 | /** |
||
381 | * @param string $bottom_view |
||
382 | * @return ControllerSetting |
||
383 | */ |
||
384 | public function setBottomView($bottom_view) |
||
385 | { |
||
386 | $this->data['bottom_view'] = $bottom_view; |
||
387 | return $this; |
||
388 | } |
||
389 | |||
390 | /** |
||
391 | * @param SidebarStyle|string $sidebar_style |
||
392 | * @return ControllerSetting |
||
393 | */ |
||
394 | public function setSidebarStyle($sidebar_style) |
||
398 | } |
||
399 | |||
400 | } |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.