Total Complexity | 42 |
Total Lines | 588 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like TriggersController 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 TriggersController, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
16 | class TriggersController extends BaseController |
||
17 | { |
||
18 | public $controller_title = 'strtables'; |
||
19 | |||
20 | /** |
||
21 | * Default method to render the controller according to the action parameter. |
||
22 | */ |
||
23 | public function render() |
||
101 | } |
||
102 | |||
103 | /** |
||
104 | * List all the triggers on the table. |
||
105 | * |
||
106 | * @param mixed $msg |
||
107 | */ |
||
108 | public function doDefault($msg = ''): void |
||
232 | } |
||
233 | |||
234 | /** |
||
235 | * @return \Slim\Http\Response|string |
||
236 | */ |
||
237 | public function doTree() |
||
238 | { |
||
239 | $data = $this->misc->getDatabaseAccessor(); |
||
240 | |||
241 | $triggers = $data->getTriggers($_REQUEST['table']); |
||
242 | |||
243 | $attrs = [ |
||
244 | 'text' => Decorator::field('tgname'), |
||
245 | 'icon' => 'Trigger', |
||
246 | ]; |
||
247 | |||
248 | return $this->printTree($triggers, $attrs, 'triggers'); |
||
249 | } |
||
250 | |||
251 | /** |
||
252 | * Function to save after altering a trigger. |
||
253 | */ |
||
254 | public function doSaveAlter(): void |
||
255 | { |
||
256 | $data = $this->misc->getDatabaseAccessor(); |
||
257 | |||
258 | $status = $data->alterTrigger($_POST['table'], $_POST['trigger'], $_POST['name']); |
||
259 | |||
260 | if (0 === $status) { |
||
261 | $this->doDefault($this->lang['strtriggeraltered']); |
||
262 | } else { |
||
263 | $this->doAlter($this->lang['strtriggeralteredbad']); |
||
264 | } |
||
265 | } |
||
266 | |||
267 | /** |
||
268 | * Function to allow altering of a trigger. |
||
269 | * |
||
270 | * @param mixed $msg |
||
271 | */ |
||
272 | public function doAlter($msg = ''): void |
||
321 | } |
||
322 | } |
||
323 | |||
324 | /** |
||
325 | * Show confirmation of drop and perform actual drop. |
||
326 | * |
||
327 | * @param mixed $confirm |
||
328 | */ |
||
329 | public function doDrop($confirm): void |
||
330 | { |
||
331 | $data = $this->misc->getDatabaseAccessor(); |
||
332 | |||
333 | if ($confirm) { |
||
334 | $this->printTrail('trigger'); |
||
335 | $this->printTitle($this->lang['strdrop'], 'pg.trigger.drop'); |
||
336 | |||
337 | echo '<p>', \sprintf( |
||
338 | $this->lang['strconfdroptrigger'], |
||
339 | $this->misc->printVal($_REQUEST['trigger']), |
||
340 | $this->misc->printVal($_REQUEST['table']) |
||
341 | ), '</p>' . \PHP_EOL; |
||
342 | |||
343 | echo '<form action="' . \containerInstance()->subFolder . '/src/views/triggers" method="post">' . \PHP_EOL; |
||
344 | echo '<input type="hidden" name="action" value="drop" />' . \PHP_EOL; |
||
345 | echo \sprintf( |
||
346 | '<input type="hidden" name="table" value="%s" />%s', |
||
347 | \htmlspecialchars($_REQUEST['table']), |
||
348 | \PHP_EOL |
||
349 | ); |
||
350 | echo '<input type="hidden" name="trigger" value="', \htmlspecialchars($_REQUEST['trigger']), '" />' . \PHP_EOL; |
||
351 | echo $this->view->form; |
||
352 | echo \sprintf( |
||
353 | '<p><input type="checkbox" id="cascade" name="cascade" /> <label for="cascade">%s</label></p>', |
||
354 | $this->lang['strcascade'] |
||
355 | ) . \PHP_EOL; |
||
356 | echo \sprintf( |
||
357 | '<input type="submit" name="yes" value="%s" />', |
||
358 | $this->lang['stryes'] |
||
359 | ) . \PHP_EOL; |
||
360 | echo \sprintf( |
||
361 | '<input type="submit" name="no" value="%s" />', |
||
362 | $this->lang['strno'] |
||
363 | ) . \PHP_EOL; |
||
364 | echo '</form>' . \PHP_EOL; |
||
365 | } else { |
||
366 | $status = $data->dropTrigger($_POST['trigger'], $_POST['table'], isset($_POST['cascade'])); |
||
367 | |||
368 | if (0 === $status) { |
||
369 | $this->doDefault($this->lang['strtriggerdropped']); |
||
370 | } else { |
||
371 | $this->doDefault($this->lang['strtriggerdroppedbad']); |
||
372 | } |
||
373 | } |
||
374 | } |
||
375 | |||
376 | /** |
||
377 | * Show confirmation of enable trigger and perform enabling the trigger. |
||
378 | * |
||
379 | * @param mixed $confirm |
||
380 | */ |
||
381 | public function doEnable($confirm): void |
||
382 | { |
||
383 | $data = $this->misc->getDatabaseAccessor(); |
||
384 | |||
385 | if ($confirm) { |
||
386 | $this->printTrail('trigger'); |
||
387 | $this->printTitle($this->lang['strenable'], 'pg.table.alter'); |
||
388 | |||
389 | echo '<p>', \sprintf( |
||
390 | $this->lang['strconfenabletrigger'], |
||
391 | $this->misc->printVal($_REQUEST['trigger']), |
||
392 | $this->misc->printVal($_REQUEST['table']) |
||
393 | ), '</p>' . \PHP_EOL; |
||
394 | |||
395 | echo '<form action="' . \containerInstance()->subFolder . '/src/views/triggers" method="post">' . \PHP_EOL; |
||
396 | echo '<input type="hidden" name="action" value="enable" />' . \PHP_EOL; |
||
397 | echo \sprintf( |
||
398 | '<input type="hidden" name="table" value="%s" />%s', |
||
399 | \htmlspecialchars($_REQUEST['table']), |
||
400 | \PHP_EOL |
||
401 | ); |
||
402 | echo '<input type="hidden" name="trigger" value="', \htmlspecialchars($_REQUEST['trigger']), '" />' . \PHP_EOL; |
||
403 | echo $this->view->form; |
||
404 | echo \sprintf( |
||
405 | '<input type="submit" name="yes" value="%s" />', |
||
406 | $this->lang['stryes'] |
||
407 | ) . \PHP_EOL; |
||
408 | echo \sprintf( |
||
409 | '<input type="submit" name="no" value="%s" />', |
||
410 | $this->lang['strno'] |
||
411 | ) . \PHP_EOL; |
||
412 | echo '</form>' . \PHP_EOL; |
||
413 | } else { |
||
414 | $status = $data->enableTrigger($_POST['trigger'], $_POST['table']); |
||
415 | |||
416 | if (0 === $status) { |
||
417 | $this->doDefault($this->lang['strtriggerenabled']); |
||
418 | } else { |
||
419 | $this->doDefault($this->lang['strtriggerenabledbad']); |
||
420 | } |
||
421 | } |
||
422 | } |
||
423 | |||
424 | /** |
||
425 | * Show confirmation of disable trigger and perform disabling the trigger. |
||
426 | * |
||
427 | * @param mixed $confirm |
||
428 | */ |
||
429 | public function doDisable($confirm): void |
||
430 | { |
||
431 | $data = $this->misc->getDatabaseAccessor(); |
||
432 | |||
433 | if ($confirm) { |
||
434 | $this->printTrail('trigger'); |
||
435 | $this->printTitle($this->lang['strdisable'], 'pg.table.alter'); |
||
436 | |||
437 | echo '<p>', \sprintf( |
||
438 | $this->lang['strconfdisabletrigger'], |
||
439 | $this->misc->printVal($_REQUEST['trigger']), |
||
440 | $this->misc->printVal($_REQUEST['table']) |
||
441 | ), '</p>' . \PHP_EOL; |
||
442 | |||
443 | echo '<form action="' . \containerInstance()->subFolder . '/src/views/triggers" method="post">' . \PHP_EOL; |
||
444 | echo '<input type="hidden" name="action" value="disable" />' . \PHP_EOL; |
||
445 | echo \sprintf( |
||
446 | '<input type="hidden" name="table" value="%s" />%s', |
||
447 | \htmlspecialchars($_REQUEST['table']), |
||
448 | \PHP_EOL |
||
449 | ); |
||
450 | echo '<input type="hidden" name="trigger" value="', \htmlspecialchars($_REQUEST['trigger']), '" />' . \PHP_EOL; |
||
451 | echo $this->view->form; |
||
452 | echo \sprintf( |
||
453 | '<input type="submit" name="yes" value="%s" />', |
||
454 | $this->lang['stryes'] |
||
455 | ) . \PHP_EOL; |
||
456 | echo \sprintf( |
||
457 | '<input type="submit" name="no" value="%s" />', |
||
458 | $this->lang['strno'] |
||
459 | ) . \PHP_EOL; |
||
460 | echo '</form>' . \PHP_EOL; |
||
461 | } else { |
||
462 | $status = $data->disableTrigger($_POST['trigger'], $_POST['table']); |
||
463 | |||
464 | if (0 === $status) { |
||
465 | $this->doDefault($this->lang['strtriggerdisabled']); |
||
466 | } else { |
||
467 | $this->doDefault($this->lang['strtriggerdisabledbad']); |
||
468 | } |
||
469 | } |
||
470 | } |
||
471 | |||
472 | /** |
||
473 | * Let them create s.th. |
||
474 | * |
||
475 | * @param mixed $msg |
||
476 | */ |
||
477 | public function doCreate($msg = ''): void |
||
572 | } |
||
573 | |||
574 | /** |
||
575 | * Actually creates the new trigger in the database. |
||
576 | */ |
||
577 | public function doSaveCreate(): void |
||
604 | } |
||
605 | } |
||
606 | } |
||
607 | } |
||
608 |