| Total Complexity | 40 |
| Total Lines | 372 |
| 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 | } |
||
| 36 | |||
| 37 | |||
| 38 | /** |
||
| 39 | * @param callable $condition |
||
| 40 | */ |
||
| 41 | public function hideButtonDetailWhen(callable $condition) { |
||
| 42 | $this->data['hide_button_detail'] = $condition; |
||
| 43 | } |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @param callable $condition |
||
| 47 | */ |
||
| 48 | public function hideButtonEditWhen(callable $condition) { |
||
| 49 | $this->data['hide_button_edit'] = $condition; |
||
| 50 | } |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @param callable $condition |
||
| 54 | */ |
||
| 55 | public function hideButtonDeleteWhen(callable $condition) { |
||
| 56 | $this->data['hide_button_delete'] = $condition; |
||
| 57 | } |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @param $enable boolean |
||
| 61 | */ |
||
| 62 | public function setButtonLimitPage($enable) { |
||
| 63 | $this->data['button_limit_page'] = $enable; |
||
| 64 | } |
||
| 65 | |||
| 66 | public function setPermalink($path) |
||
| 67 | { |
||
| 68 | $this->data['permalink'] = $path; |
||
| 69 | } |
||
| 70 | |||
| 71 | public function disableSoftDelete() |
||
| 72 | { |
||
| 73 | $this->data['disable_soft_delete'] = true; |
||
| 74 | } |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @param Closure $callbackQuery |
||
| 78 | */ |
||
| 79 | public function hookIndexQuery(Closure $callbackQuery) |
||
| 80 | { |
||
| 81 | $this->data['hook_index_query'] = $callbackQuery; |
||
| 82 | } |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @param Closure $callback |
||
| 86 | */ |
||
| 87 | public function hookAfterInsert(Closure $callback) |
||
| 88 | { |
||
| 89 | $this->data['hook_after_insert'] = $callback; |
||
| 90 | } |
||
| 91 | |||
| 92 | /** |
||
| 93 | * @param Closure $callback |
||
| 94 | */ |
||
| 95 | public function hookBeforeInsert(Closure $callback) |
||
| 96 | { |
||
| 97 | $this->data['hook_before_insert'] = $callback; |
||
| 98 | } |
||
| 99 | |||
| 100 | /** |
||
| 101 | * @param Closure $callback |
||
| 102 | */ |
||
| 103 | public function hookBeforeUpdate(Closure $callback) |
||
| 104 | { |
||
| 105 | $this->data['hook_before_update'] = $callback; |
||
| 106 | } |
||
| 107 | |||
| 108 | /** |
||
| 109 | * @param Closure $callback |
||
| 110 | */ |
||
| 111 | public function hookAfterUpdate(Closure $callback) |
||
| 112 | { |
||
| 113 | $this->data['hook_after_update'] = $callback; |
||
| 114 | } |
||
| 115 | |||
| 116 | /** |
||
| 117 | * @param Closure $callbackQuery |
||
| 118 | */ |
||
| 119 | public function hookSearchQuery(Closure $callbackQuery) |
||
| 120 | { |
||
| 121 | $this->data['hook_search_query'] = $callbackQuery; |
||
| 122 | } |
||
| 123 | |||
| 124 | /** |
||
| 125 | * @param string $label |
||
| 126 | * @param string $actionURL |
||
| 127 | * @param string $fontAwesome_icon |
||
| 128 | * @param ButtonColor $color |
||
| 129 | * @param string $attributes |
||
| 130 | */ |
||
| 131 | public function addIndexActionButton($label, $actionURL, $fontAwesome_icon=null, $color=null, $attributes = null) |
||
| 132 | { |
||
| 133 | $color = ($color)?:ButtonColor::DARK_BLUE; |
||
| 134 | |||
| 135 | $model = new IndexActionButtonModel(); |
||
| 136 | $model->setLabel($label); |
||
| 137 | $model->setIcon($fontAwesome_icon); |
||
| 138 | $model->setColor($color); |
||
| 139 | $model->setUrl($actionURL); |
||
| 140 | $model->setAttributes($attributes); |
||
| 141 | |||
| 142 | $this->data['index_action_button'][] = $model; |
||
| 143 | } |
||
| 144 | |||
| 145 | public function setOrderBy($field, $sort = 'desc') |
||
| 146 | { |
||
| 147 | $this->data['order_by'] = [$field, $sort]; |
||
| 148 | return $this; |
||
| 149 | } |
||
| 150 | |||
| 151 | /** |
||
| 152 | * @param boolean $enable |
||
| 153 | * @return $this |
||
| 154 | */ |
||
| 155 | public function setSearchForm($enable) { |
||
| 156 | $this->data['search_form'] = $enable; |
||
| 157 | return $this; |
||
| 158 | } |
||
| 159 | |||
| 160 | /** |
||
| 161 | * @param boolean $button_save |
||
| 162 | * @return ControllerSetting |
||
| 163 | */ |
||
| 164 | public function setButtonSave($button_save) |
||
| 165 | { |
||
| 166 | $this->data['button_save'] = $button_save; |
||
| 167 | return $this; |
||
| 168 | } |
||
| 169 | |||
| 170 | /** |
||
| 171 | * @param boolean $button_cancel |
||
| 172 | * @return $this |
||
| 173 | */ |
||
| 174 | public function setButtonCancel($button_cancel) |
||
| 175 | { |
||
| 176 | $this->data['button_cancel'] = $button_cancel; |
||
| 177 | return $this; |
||
| 178 | } |
||
| 179 | |||
| 180 | |||
| 181 | /** |
||
| 182 | * @param $label |
||
| 183 | * @param callable|string $url_target |
||
| 184 | * @param callable|string $condition_callback |
||
| 185 | * @param $fontAwesome_icon |
||
| 186 | * @param ButtonColor|string $color |
||
| 187 | */ |
||
| 188 | public function addActionButton($label, $url_target, $condition_callback=null, $fontAwesome_icon=null, $color=null, $confirmation = false) |
||
| 189 | { |
||
| 190 | $new = new AddActionButtonModel(); |
||
| 191 | $new->setLabel($label); |
||
| 192 | $new->setIcon($fontAwesome_icon?:"fa fa-bars"); |
||
| 193 | $new->setColor($color?:"primary"); |
||
| 194 | $new->setUrl($url_target); |
||
| 195 | $new->setCondition($condition_callback); |
||
| 196 | $new->setConfirmation($confirmation); |
||
| 197 | $this->data['add_action_button'][] = $new; |
||
| 198 | } |
||
| 199 | /** |
||
| 200 | * @param mixed $button_edit |
||
| 201 | * @param null $condition_callback |
||
| 202 | * @return ControllerSetting |
||
| 203 | */ |
||
| 204 | public function setButtonEdit($button_edit, $condition_callback = null) |
||
| 205 | { |
||
| 206 | $this->data['button_edit'] = $button_edit; |
||
| 207 | $this->data['button_edit_callback'] = $condition_callback; |
||
| 208 | return $this; |
||
| 209 | } |
||
| 210 | |||
| 211 | /** |
||
| 212 | * @param mixed $button_detail |
||
| 213 | * @param null $condition_callback |
||
| 214 | * @return ControllerSetting |
||
| 215 | */ |
||
| 216 | public function setButtonDetail($button_detail, $condition_callback = null) |
||
| 217 | { |
||
| 218 | $this->data['button_detail'] = $button_detail; |
||
| 219 | $this->data['button_detail_callback'] = $condition_callback; |
||
| 220 | return $this; |
||
| 221 | } |
||
| 222 | |||
| 223 | /** |
||
| 224 | * @param mixed $button_delete |
||
| 225 | * @param null $condition_callback |
||
| 226 | * @return ControllerSetting |
||
| 227 | */ |
||
| 228 | public function setButtonDelete($button_delete, $condition_callback = null) |
||
| 229 | { |
||
| 230 | $this->data['button_delete'] = $button_delete; |
||
| 231 | $this->data['button_delete_callback'] = $condition_callback; |
||
| 232 | return $this; |
||
| 233 | } |
||
| 234 | |||
| 235 | |||
| 236 | |||
| 237 | /** |
||
| 238 | * @param string $alert_message |
||
| 239 | * @return ControllerSetting |
||
| 240 | */ |
||
| 241 | public function setAlertMessage($alert_message) |
||
| 242 | { |
||
| 243 | $this->data['alert_message'] = $alert_message; |
||
| 244 | return $this; |
||
| 245 | } |
||
| 246 | |||
| 247 | |||
| 248 | /** |
||
| 249 | * @param string $html_or_view |
||
| 250 | * @return ControllerSetting |
||
| 251 | */ |
||
| 252 | public function setBeforeIndexTable($html_or_view) |
||
| 256 | } |
||
| 257 | |||
| 258 | /** |
||
| 259 | * @param mixed $html_or_view |
||
| 260 | * @return ControllerSetting |
||
| 261 | */ |
||
| 262 | public function setAfterIndexTable($html_or_view) |
||
| 263 | { |
||
| 264 | $this->data['after_index_table'] = $html_or_view; |
||
| 265 | return $this; |
||
| 266 | } |
||
| 267 | |||
| 268 | /** |
||
| 269 | * @param callable|string $html_or_view |
||
| 270 | * @return ControllerSetting |
||
| 271 | */ |
||
| 272 | public function setBeforeDetailForm($html_or_view) |
||
| 273 | { |
||
| 274 | $this->data['before_detail_form'] = $html_or_view; |
||
| 275 | return $this; |
||
| 276 | } |
||
| 277 | |||
| 278 | /** |
||
| 279 | * @param callable|string $html_or_view |
||
| 280 | * @return ControllerSetting |
||
| 281 | */ |
||
| 282 | public function setAfterDetailForm($html_or_view) |
||
| 283 | { |
||
| 284 | $this->data['after_detail_form'] = $html_or_view; |
||
| 285 | return $this; |
||
| 286 | } |
||
| 287 | |||
| 288 | |||
| 289 | /** |
||
| 290 | * @param mixed $page_title |
||
| 291 | * @return ControllerSetting |
||
| 292 | */ |
||
| 293 | public function setPageTitle($page_title) |
||
| 294 | { |
||
| 295 | $this->data['page_title'] = $page_title; |
||
| 296 | return $this; |
||
| 297 | } |
||
| 298 | |||
| 299 | |||
| 300 | /** |
||
| 301 | * @param mixed $icon |
||
| 302 | * @return ControllerSetting |
||
| 303 | */ |
||
| 304 | public function setPageIcon($icon = "fa fa-table") |
||
| 308 | } |
||
| 309 | |||
| 310 | /** |
||
| 311 | * @param mixed $table |
||
| 312 | * @return ControllerSetting |
||
| 313 | */ |
||
| 314 | public function setTable($table) |
||
| 318 | } |
||
| 319 | |||
| 320 | /** |
||
| 321 | * @param int $limit |
||
| 322 | * @return ControllerSetting |
||
| 323 | */ |
||
| 324 | public function setLimit($limit) |
||
| 325 | { |
||
| 326 | $this->data['limit'] = $limit; |
||
| 327 | return $this; |
||
| 328 | } |
||
| 329 | |||
| 330 | /** |
||
| 331 | * @param bool $button_filter |
||
| 332 | * @return ControllerSetting |
||
| 333 | */ |
||
| 334 | public function setButtonFilter($button_filter) |
||
| 335 | { |
||
| 336 | $this->data['button_filter'] = $button_filter; |
||
| 337 | return $this; |
||
| 338 | } |
||
| 339 | |||
| 340 | |||
| 341 | /** |
||
| 342 | * @param bool $button_add_more |
||
| 343 | * @return ControllerSetting |
||
| 344 | */ |
||
| 345 | public function setButtonAddMore($button_add_more) |
||
| 346 | { |
||
| 347 | $this->data['button_add_more'] = $button_add_more; |
||
| 348 | return $this; |
||
| 349 | } |
||
| 350 | |||
| 351 | /** |
||
| 352 | * @param bool $button_add |
||
| 353 | * @return ControllerSetting |
||
| 354 | */ |
||
| 355 | public function setButtonAdd($button_add) |
||
| 356 | { |
||
| 357 | $this->data['button_add'] = $button_add; |
||
| 358 | return $this; |
||
| 359 | } |
||
| 360 | |||
| 361 | /** |
||
| 362 | * @param string $head_html |
||
| 363 | * @return ControllerSetting |
||
| 364 | */ |
||
| 365 | public function setHeadHtml($head_html) |
||
| 366 | { |
||
| 367 | $this->data['head_html'] = $head_html; |
||
| 368 | return $this; |
||
| 369 | } |
||
| 370 | |||
| 371 | /** |
||
| 372 | * @param string $bottom_html |
||
| 373 | * @return ControllerSetting |
||
| 374 | */ |
||
| 375 | public function setBottomHtml($bottom_html) |
||
| 379 | } |
||
| 380 | |||
| 381 | /** |
||
| 382 | * @param SidebarStyle|string $sidebar_style |
||
| 383 | * @return ControllerSetting |
||
| 384 | */ |
||
| 385 | public function setSidebarStyle($sidebar_style) |
||
| 386 | { |
||
| 387 | $this->data['sidebar_style'] = $sidebar_style; |
||
| 388 | return $this; |
||
| 389 | } |
||
| 390 | |||
| 391 | } |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.