| Total Complexity | 49 |
| Total Lines | 511 |
| Duplicated Lines | 0 % |
| Changes | 4 | ||
| Bugs | 1 | Features | 0 |
Complex classes like MaintenanceController 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 MaintenanceController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 22 | class MaintenanceController extends Controller |
||
| 23 | { |
||
| 24 | /** |
||
| 25 | * Date |
||
| 26 | * @var string |
||
| 27 | */ |
||
| 28 | public $date; |
||
| 29 | /** |
||
| 30 | * Title |
||
| 31 | * @var string |
||
| 32 | */ |
||
| 33 | public $title; |
||
| 34 | /** |
||
| 35 | * Content |
||
| 36 | * @var string |
||
| 37 | */ |
||
| 38 | public $content; |
||
| 39 | /** |
||
| 40 | * @var string |
||
| 41 | */ |
||
| 42 | public $subscribe; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var string |
||
| 46 | */ |
||
| 47 | public $timer; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var StateInterface |
||
| 51 | */ |
||
| 52 | protected $state; |
||
| 53 | /** |
||
| 54 | * @var mixed string |
||
| 55 | */ |
||
| 56 | protected $exampleData; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @var FileStateForm |
||
| 60 | */ |
||
| 61 | protected $stateForm; |
||
| 62 | /** |
||
| 63 | * @var SubscribeForm |
||
| 64 | */ |
||
| 65 | protected $subscribeForm; |
||
| 66 | /** |
||
| 67 | * @var string |
||
| 68 | */ |
||
| 69 | protected $enabled; |
||
| 70 | /** |
||
| 71 | * @var string |
||
| 72 | */ |
||
| 73 | protected $disabled; |
||
| 74 | /** |
||
| 75 | * @var string |
||
| 76 | */ |
||
| 77 | protected $enabledMode; |
||
| 78 | /** |
||
| 79 | * @var string |
||
| 80 | */ |
||
| 81 | protected $updatedMode; |
||
| 82 | protected $notUpdatedMode; |
||
| 83 | /** |
||
| 84 | * @var string |
||
| 85 | */ |
||
| 86 | protected $disabledMode; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * MaintenanceController constructor. |
||
| 90 | * |
||
| 91 | * @param string $id |
||
| 92 | * @param Module $module |
||
| 93 | * @param StateInterface $state |
||
| 94 | * @param array $config |
||
| 95 | */ |
||
| 96 | public function __construct($id, Module $module, StateInterface $state, array $config = []) |
||
| 97 | { |
||
| 98 | $this->state = $state; |
||
| 99 | $this->exampleData = $this->exampleDateFormat(); |
||
| 100 | $this->stateForm = new FileStateForm(); |
||
| 101 | $this->subscribeForm = new SubscribeForm(); |
||
| 102 | $this->enabledMode = $this->ansiFormat(Maintenance::t('app', 'ENABLED'), Console::FG_RED); |
||
| 103 | $this->updatedMode = $this->ansiFormat(Maintenance::t('app', 'UPDATED'), Console::FG_GREEN); |
||
| 104 | $this->notUpdatedMode = $this->ansiFormat(Maintenance::t('app', 'NOT UPDATED'), Console::FG_YELLOW); |
||
| 105 | $this->disabledMode = $this->ansiFormat(Maintenance::t('app', 'DISABLED'), Console::FG_GREEN); |
||
| 106 | $this->enabled = $this->ansiFormat(Maintenance::t('app', 'ENABLED'), Console::FG_GREEN); |
||
| 107 | $this->disabled = $this->ansiFormat(Maintenance::t('app', 'DISABLED'), Console::FG_RED); |
||
| 108 | parent::__construct($id, $module, $config); |
||
| 109 | } |
||
| 110 | |||
| 111 | |||
| 112 | /** |
||
| 113 | * Options |
||
| 114 | * |
||
| 115 | * @param string $actionId |
||
| 116 | * @return array|string[] |
||
| 117 | */ |
||
| 118 | public function options($actionId) |
||
| 119 | { |
||
| 120 | return [ |
||
| 121 | 'date', |
||
| 122 | 'title', |
||
| 123 | 'content', |
||
| 124 | 'subscribe', |
||
| 125 | 'timer' |
||
| 126 | ]; |
||
| 127 | } |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Aliases |
||
| 131 | * |
||
| 132 | * @return array |
||
| 133 | */ |
||
| 134 | public function optionAliases() |
||
| 135 | { |
||
| 136 | return [ |
||
| 137 | 'd' => 'date', |
||
| 138 | 't' => 'title', |
||
| 139 | 'c' => 'content', |
||
| 140 | 's' => 'subscribe', |
||
| 141 | 'tm' => 'timer' |
||
| 142 | ]; |
||
| 143 | } |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Maintenance status |
||
| 147 | */ |
||
| 148 | public function actionIndex() |
||
| 149 | { |
||
| 150 | if ($this->state->isEnabled()) { |
||
| 151 | $this->renderGroupEnabled(); |
||
| 152 | } else { |
||
| 153 | $this->renderGroupDisabled(); |
||
| 154 | } |
||
| 155 | } |
||
| 156 | |||
| 157 | /** |
||
| 158 | * Enable maintenance mode |
||
| 159 | */ |
||
| 160 | public function actionEnable() |
||
| 161 | { |
||
| 162 | $stateForm = new FileStateForm(); |
||
| 163 | if (!$this->stateForm->isEnabled()) { |
||
| 164 | $stateForm->mode = Maintenance::STATUS_CODE_MAINTENANCE; |
||
| 165 | $stateForm = $this->setFileStateForm($stateForm); |
||
| 166 | $this->setDefaultValue($stateForm); |
||
| 167 | if ($stateForm->validate()) { |
||
| 168 | $stateForm->save(); |
||
| 169 | } |
||
| 170 | } |
||
| 171 | $this->renderGroupEnabled(); |
||
| 172 | } |
||
| 173 | |||
| 174 | /** |
||
| 175 | * Disable maintenance mode and send notify |
||
| 176 | */ |
||
| 177 | public function actionDisable() |
||
| 178 | { |
||
| 179 | $status = $status = $this->disabledMode; |
||
|
|
|||
| 180 | $this->renderMaintenanceModeHasBeenStatus($status); |
||
| 181 | $this->stdout(PHP_EOL); |
||
| 182 | if ($this->stateForm->isEnabled()) { |
||
| 183 | $this->stateForm->disable(); |
||
| 184 | $result = $this->subscribeForm->send(); |
||
| 185 | if ($result || $result === 0) { |
||
| 186 | $this->renderNotifiedSubscribers($result); |
||
| 187 | $this->stdout(PHP_EOL); |
||
| 188 | } |
||
| 189 | } |
||
| 190 | $this->renderEnableMaintenanceMode(); |
||
| 191 | $this->stdout(PHP_EOL); |
||
| 192 | $this->renderOptionsTable(); |
||
| 193 | } |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Update date and time maintenance mode |
||
| 197 | */ |
||
| 198 | public function actionUpdate() |
||
| 199 | { |
||
| 200 | if ($this->state->isEnabled()) { |
||
| 201 | $this->stateForm->mode = Maintenance::STATUS_CODE_MAINTENANCE; |
||
| 202 | $stateForm = $this->setFileStateForm($this->stateForm); |
||
| 203 | |||
| 204 | if ($stateForm->validate()) { |
||
| 205 | $stateForm->save(); |
||
| 206 | |||
| 207 | $status = $this->updatedMode; |
||
| 208 | $this->renderMaintenanceModeHasBeenStatus($status); |
||
| 209 | $this->stdout(PHP_EOL); |
||
| 210 | |||
| 211 | $this->renderOnUntilDateTime(); |
||
| 212 | $this->stdout(PHP_EOL); |
||
| 213 | $this->stdout(PHP_EOL); |
||
| 214 | |||
| 215 | $this->renderCountDownStatus(); |
||
| 216 | $this->stdout(PHP_EOL); |
||
| 217 | $this->renderSubscriptionFormStatus(); |
||
| 218 | $this->stdout(PHP_EOL); |
||
| 219 | |||
| 220 | $this->renderUpdateMaintenanceMode(); |
||
| 221 | $this->stdout(PHP_EOL); |
||
| 222 | |||
| 223 | $this->renderOptionsTable(); |
||
| 224 | } else { |
||
| 225 | $status = $this->notUpdatedMode; |
||
| 226 | $this->renderMaintenanceModeHasBeenStatus($status); |
||
| 227 | $this->stdout(PHP_EOL); |
||
| 228 | |||
| 229 | $this->renderUpdateMaintenanceMode(); |
||
| 230 | $this->stdout(PHP_EOL); |
||
| 231 | |||
| 232 | $this->renderOptionsTable(); |
||
| 233 | } |
||
| 234 | } else { |
||
| 235 | $this->renderGroupDisabled(); |
||
| 236 | } |
||
| 237 | } |
||
| 238 | |||
| 239 | /** |
||
| 240 | * Show subscribers to whom messages |
||
| 241 | */ |
||
| 242 | public function actionSubscribers() |
||
| 243 | { |
||
| 244 | if (!$this->stateForm->isEnabled()) { |
||
| 245 | $this->renderGroupDisabled(); |
||
| 246 | } else if ($emails = $this->subscribeForm->getEmails()) { |
||
| 247 | $status = $this->enabledMode; |
||
| 248 | $this->renderMaintenanceModeHasBeenStatus($status); |
||
| 249 | $this->stdout(PHP_EOL); |
||
| 250 | |||
| 251 | $this->renderOnUntilDateTime(); |
||
| 252 | $this->stdout(PHP_EOL); |
||
| 253 | |||
| 254 | $this->renderSubscriptionInfo(); |
||
| 255 | $this->stdout(PHP_EOL); |
||
| 256 | $this->stdout(PHP_EOL); |
||
| 257 | |||
| 258 | foreach ($emails as $email) { |
||
| 259 | $this->stdout($email . PHP_EOL); |
||
| 260 | } |
||
| 261 | } else { |
||
| 262 | $status = $this->enabledMode; |
||
| 263 | $this->renderMaintenanceModeHasBeenStatus($status); |
||
| 264 | $this->stdout(PHP_EOL); |
||
| 265 | $this->renderOnUntilDateTime(); |
||
| 266 | $this->stdout(PHP_EOL); |
||
| 267 | |||
| 268 | $this->stdout(Maintenance::t('app', 'No subscribers')); |
||
| 269 | $this->stdout(PHP_EOL); |
||
| 270 | } |
||
| 271 | } |
||
| 272 | |||
| 273 | /** |
||
| 274 | * Options |
||
| 275 | */ |
||
| 276 | public function actionOptions() |
||
| 277 | { |
||
| 278 | $this->renderOptionsTable(); |
||
| 279 | } |
||
| 280 | |||
| 281 | /** |
||
| 282 | * lcfirst() |
||
| 283 | * @param $str string |
||
| 284 | * @return string |
||
| 285 | */ |
||
| 286 | protected function mb_lcfirst($str = '') |
||
| 287 | { |
||
| 288 | if (is_string($str) && !empty($str)) { |
||
| 289 | $charset = Yii::$app->charset; |
||
| 290 | $first = mb_substr($str, 0, 1, $charset); |
||
| 291 | $last = mb_substr($str, 1); |
||
| 292 | $first = mb_strtolower($first, $charset); |
||
| 293 | $last = mb_strtolower($last, $charset); |
||
| 294 | return $first . $last; |
||
| 295 | } |
||
| 296 | return $str; |
||
| 297 | } |
||
| 298 | |||
| 299 | /** |
||
| 300 | * Render is Disabled |
||
| 301 | */ |
||
| 302 | public function renderGroupDisabled() |
||
| 303 | { |
||
| 304 | $status = $this->disabledMode; |
||
| 305 | $this->renderMaintenanceModeHasBeenStatus($status); |
||
| 306 | $this->stdout(PHP_EOL); |
||
| 307 | |||
| 308 | $this->renderEnableMaintenanceMode(); |
||
| 309 | $this->stdout(PHP_EOL); |
||
| 310 | |||
| 311 | $this->renderOptionsTable(); |
||
| 312 | } |
||
| 313 | |||
| 314 | /** |
||
| 315 | * Render is Enabled |
||
| 316 | */ |
||
| 317 | public function renderGroupEnabled() |
||
| 318 | { |
||
| 319 | $status = $this->enabledMode; |
||
| 320 | $this->renderMaintenanceModeHasBeenStatus($status); |
||
| 321 | $this->stdout(PHP_EOL); |
||
| 322 | |||
| 323 | $this->renderOnUntilDateTime(); |
||
| 324 | $this->stdout(PHP_EOL); |
||
| 325 | |||
| 326 | $this->renderSubscriptionInfo(); |
||
| 327 | $this->stdout(PHP_EOL); |
||
| 328 | $this->stdout(PHP_EOL); |
||
| 329 | |||
| 330 | $this->renderCountDownStatus(); |
||
| 331 | $this->stdout(PHP_EOL); |
||
| 332 | |||
| 333 | $this->renderSubscriptionFormStatus(); |
||
| 334 | $this->stdout(PHP_EOL); |
||
| 335 | |||
| 336 | $this->renderDisableAndSubscribe(); |
||
| 337 | $this->stdout(PHP_EOL); |
||
| 338 | |||
| 339 | $this->renderUpdateMaintenanceMode(); |
||
| 340 | $this->stdout(PHP_EOL); |
||
| 341 | |||
| 342 | $this->renderOptionsTable(); |
||
| 343 | } |
||
| 344 | |||
| 345 | /** |
||
| 346 | * Notified 2 subscribers. |
||
| 347 | * |
||
| 348 | * @param $count int |
||
| 349 | */ |
||
| 350 | public function renderNotifiedSubscribers($count = 0) |
||
| 351 | { |
||
| 352 | $this->stdout(Maintenance::t('app', '{n, plural, =0{No subscribers} =1{Notified one subscriber} other{Notified # subscribers}}.', [ |
||
| 353 | 'n' => $count |
||
| 354 | ])); |
||
| 355 | } |
||
| 356 | |||
| 357 | /** |
||
| 358 | * Maintenance Mode has been ENABLED/DISABLED |
||
| 359 | * |
||
| 360 | * @param $status string |
||
| 361 | */ |
||
| 362 | public function renderMaintenanceModeHasBeenStatus($status) |
||
| 363 | { |
||
| 364 | $message = Maintenance::t('app', 'Maintenance Mode has been {:status}', [ |
||
| 365 | ':status' => $status, |
||
| 366 | ]); |
||
| 367 | $this->stdout($message); |
||
| 368 | } |
||
| 369 | |||
| 370 | /** |
||
| 371 | * on until 09-03-2020 11:15:04 |
||
| 372 | */ |
||
| 373 | public function renderOnUntilDateTime() |
||
| 374 | { |
||
| 375 | $datetime = $this->stateForm->getDateTime(); |
||
| 376 | $message = Maintenance::t('app', 'on until {:datetime}', [ |
||
| 377 | ':datetime' => $datetime |
||
| 378 | ]); |
||
| 379 | $this->stdout($message); |
||
| 380 | } |
||
| 381 | |||
| 382 | /** |
||
| 383 | * Total 2 followers. |
||
| 384 | */ |
||
| 385 | public function renderSubscriptionInfo() |
||
| 391 | } |
||
| 392 | |||
| 393 | /** |
||
| 394 | * Count Down: ENABLED |
||
| 395 | */ |
||
| 396 | public function renderCountDownStatus() |
||
| 397 | { |
||
| 398 | $status = $this->stateForm->isTimer() ? $this->enabled : $this->disabled; |
||
| 399 | $message = Maintenance::t('app', 'Count Down: {:status}', [ |
||
| 400 | ':status' => $status |
||
| 401 | ]); |
||
| 402 | $this->stdout($message); |
||
| 403 | } |
||
| 404 | |||
| 405 | /** |
||
| 406 | * Subscription form: ENABLED |
||
| 407 | */ |
||
| 408 | public function renderSubscriptionFormStatus() |
||
| 409 | { |
||
| 410 | $status = $this->stateForm->isSubscribe() ? $this->enabled : $this->disabled; |
||
| 411 | $message = Maintenance::t('app', 'Subscription form: {:status}', [ |
||
| 412 | ':status' => $status |
||
| 413 | ]); |
||
| 414 | $this->stdout($message); |
||
| 415 | } |
||
| 416 | |||
| 417 | /** |
||
| 418 | * To turn off and notify subscribers, |
||
| 419 | * use: |
||
| 420 | * php yii maintenance/disable |
||
| 421 | */ |
||
| 422 | public function renderDisableAndSubscribe() |
||
| 423 | { |
||
| 424 | $message = Maintenance::t('app', "To turn off and notify subscribers,\nuse:"); |
||
| 425 | $this->stdout(PHP_EOL . $message . PHP_EOL); |
||
| 426 | $message = 'php yii maintenance/disable'; |
||
| 427 | $this->stdout($message); |
||
| 428 | } |
||
| 429 | |||
| 430 | /** |
||
| 431 | * To enable the maintenance mode, |
||
| 432 | * use: |
||
| 433 | * php yii maintenance/enable --option='value' |
||
| 434 | */ |
||
| 435 | public function renderEnableMaintenanceMode() |
||
| 436 | { |
||
| 437 | $message = Maintenance::t('app', "To enable the maintenance mode,\nuse:"); |
||
| 438 | $this->stdout(PHP_EOL . $message . PHP_EOL); |
||
| 439 | |||
| 440 | $option = Maintenance::t('app', 'Option'); |
||
| 441 | $value = Maintenance::t('app', 'Value'); |
||
| 442 | $message = Maintenance::t('app', "php yii maintenance/enable --{:option}1='{:value}1' --{:option}2='{:value}2' ...", [ |
||
| 443 | ':option' => $this->mb_lcfirst(trim($option)), |
||
| 444 | ':value' => $this->mb_lcfirst(trim($value)) |
||
| 445 | ]); |
||
| 446 | $this->stdout($message); |
||
| 447 | } |
||
| 448 | |||
| 449 | /** |
||
| 450 | * To update the maintenance mode, |
||
| 451 | * use: |
||
| 452 | * php yii maintenance/update --option='value' |
||
| 453 | */ |
||
| 454 | public function renderUpdateMaintenanceMode() |
||
| 455 | { |
||
| 456 | $message = Maintenance::t('app', "To update the maintenance mode,\nuse:"); |
||
| 457 | $this->stdout(PHP_EOL . $message . PHP_EOL); |
||
| 458 | |||
| 459 | $option = Maintenance::t('app', 'Option'); |
||
| 460 | $value = Maintenance::t('app', 'Value'); |
||
| 461 | $message = Maintenance::t('app', "php yii maintenance/update --{:option}1='{:value}1' --{:option}2='{:value}2' ...", [ |
||
| 462 | ':option' => $this->mb_lcfirst(trim($option)), |
||
| 463 | ':value' => $this->mb_lcfirst(trim($value)) |
||
| 464 | ]); |
||
| 465 | $this->stdout($message); |
||
| 466 | } |
||
| 467 | |||
| 468 | /** |
||
| 469 | * Options and aliases |
||
| 470 | */ |
||
| 471 | public function renderOptionsTable() |
||
| 487 | } |
||
| 488 | |||
| 489 | /** |
||
| 490 | * @param FileStateForm $stateForm |
||
| 491 | * @return FileStateForm |
||
| 492 | */ |
||
| 493 | protected function setFileStateForm(FileStateForm $stateForm) |
||
| 511 | } |
||
| 512 | |||
| 513 | /** |
||
| 514 | * @param FileStateForm $stateForm |
||
| 515 | */ |
||
| 516 | protected function setDefaultValue(FileStateForm $stateForm) |
||
| 517 | { |
||
| 518 | if ($stateForm->title === null && $this->title === null) { |
||
| 519 | $stateForm->title = BackendMaintenance::t('app', $stateForm->getDefaultTitle()); |
||
| 520 | } |
||
| 521 | if ($stateForm->text === null && $this->content === null) { |
||
| 522 | $stateForm->text = BackendMaintenance::t('app', $stateForm->getDefaultText()); |
||
| 523 | } |
||
| 524 | } |
||
| 525 | |||
| 526 | /** |
||
| 527 | * Example format date time |
||
| 528 | * @return mixed |
||
| 529 | */ |
||
| 530 | protected function exampleDateFormat() |
||
| 533 | } |
||
| 534 | } |
||
| 535 | |||
| 536 |