| Total Complexity | 74 |
| Total Lines | 805 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like SequencesController 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 SequencesController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 16 | class SequencesController extends BaseController |
||
| 17 | { |
||
| 18 | public $controller_title = 'strsequences'; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * Default method to render the controller according to the action parameter. |
||
| 22 | */ |
||
| 23 | public function render() |
||
| 107 | } |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Display list of all sequences in the database/schema. |
||
| 111 | * |
||
| 112 | * @param mixed $msg |
||
| 113 | */ |
||
| 114 | public function doDefault($msg = '') |
||
| 206 | } |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Generate XML for the browser tree. |
||
| 210 | */ |
||
| 211 | public function doTree() |
||
| 234 | } |
||
| 235 | |||
| 236 | /** |
||
| 237 | * Display the properties of a sequence. |
||
| 238 | * |
||
| 239 | * @param mixed $msg |
||
| 240 | */ |
||
| 241 | public function doProperties($msg = '') |
||
| 242 | { |
||
| 243 | $data = $this->misc->getDatabaseAccessor(); |
||
| 244 | $this->printTrail('sequence'); |
||
| 245 | $this->printTitle($this->lang['strproperties'], 'pg.sequence'); |
||
| 246 | $this->printMsg($msg); |
||
| 247 | |||
| 248 | // Fetch the sequence information |
||
| 249 | $sequence = $data->getSequence($_REQUEST['sequence']); |
||
| 250 | |||
| 251 | if (is_object($sequence) && $sequence->recordCount() > 0) { |
||
| 252 | $sequence->fields['is_cycled'] = $data->phpBool($sequence->fields['is_cycled']); |
||
| 253 | $sequence->fields['is_called'] = $data->phpBool($sequence->fields['is_called']); |
||
| 254 | |||
| 255 | // Show comment if any |
||
| 256 | if (null !== $sequence->fields['seqcomment']) { |
||
| 257 | echo '<p class="comment">', $this->misc->printVal($sequence->fields['seqcomment']), '</p>'.PHP_EOL; |
||
| 258 | } |
||
| 259 | |||
| 260 | echo '<table border="0">'; |
||
| 261 | echo "<tr><th class=\"data\">{$this->lang['strname']}</th>"; |
||
| 262 | if ($data->hasAlterSequenceStart()) { |
||
| 263 | echo "<th class=\"data\">{$this->lang['strstartvalue']}</th>"; |
||
| 264 | } |
||
| 265 | echo "<th class=\"data\">{$this->lang['strlastvalue']}</th>"; |
||
| 266 | echo "<th class=\"data\">{$this->lang['strincrementby']}</th>"; |
||
| 267 | echo "<th class=\"data\">{$this->lang['strmaxvalue']}</th>"; |
||
| 268 | echo "<th class=\"data\">{$this->lang['strminvalue']}</th>"; |
||
| 269 | echo "<th class=\"data\">{$this->lang['strcachevalue']}</th>"; |
||
| 270 | echo "<th class=\"data\">{$this->lang['strlogcount']}</th>"; |
||
| 271 | echo "<th class=\"data\">{$this->lang['strcancycle']}</th>"; |
||
| 272 | echo "<th class=\"data\">{$this->lang['striscalled']}</th></tr>"; |
||
| 273 | echo '<tr>'; |
||
| 274 | echo '<td class="data1">', $this->misc->printVal($sequence->fields['seqname']), '</td>'; |
||
| 275 | if ($data->hasAlterSequenceStart()) { |
||
| 276 | echo '<td class="data1">', $this->misc->printVal($sequence->fields['start_value']), '</td>'; |
||
| 277 | } |
||
| 278 | echo '<td class="data1">', $this->misc->printVal($sequence->fields['last_value']), '</td>'; |
||
| 279 | echo '<td class="data1">', $this->misc->printVal($sequence->fields['increment_by']), '</td>'; |
||
| 280 | echo '<td class="data1">', $this->misc->printVal($sequence->fields['max_value']), '</td>'; |
||
| 281 | echo '<td class="data1">', $this->misc->printVal($sequence->fields['min_value']), '</td>'; |
||
| 282 | echo '<td class="data1">', $this->misc->printVal($sequence->fields['cache_value']), '</td>'; |
||
| 283 | echo '<td class="data1">', $this->misc->printVal($sequence->fields['log_cnt']), '</td>'; |
||
| 284 | echo '<td class="data1">', ($sequence->fields['is_cycled'] ? $this->lang['stryes'] : $this->lang['strno']), '</td>'; |
||
| 285 | echo '<td class="data1">', ($sequence->fields['is_called'] ? $this->lang['stryes'] : $this->lang['strno']), '</td>'; |
||
| 286 | echo '</tr>'; |
||
| 287 | echo '</table>'; |
||
| 288 | |||
| 289 | $navlinks = [ |
||
| 290 | 'alter' => [ |
||
| 291 | 'attr' => [ |
||
| 292 | 'href' => [ |
||
| 293 | 'url' => 'sequences', |
||
| 294 | 'urlvars' => [ |
||
| 295 | 'action' => 'confirm_alter', |
||
| 296 | 'server' => $_REQUEST['server'], |
||
| 297 | 'database' => $_REQUEST['database'], |
||
| 298 | 'schema' => $_REQUEST['schema'], |
||
| 299 | 'sequence' => $sequence->fields['seqname'], |
||
| 300 | ], |
||
| 301 | ], |
||
| 302 | ], |
||
| 303 | 'content' => $this->lang['stralter'], |
||
| 304 | ], |
||
| 305 | 'setval' => [ |
||
| 306 | 'attr' => [ |
||
| 307 | 'href' => [ |
||
| 308 | 'url' => 'sequences', |
||
| 309 | 'urlvars' => [ |
||
| 310 | 'action' => 'confirm_setval', |
||
| 311 | 'server' => $_REQUEST['server'], |
||
| 312 | 'database' => $_REQUEST['database'], |
||
| 313 | 'schema' => $_REQUEST['schema'], |
||
| 314 | 'sequence' => $sequence->fields['seqname'], |
||
| 315 | ], |
||
| 316 | ], |
||
| 317 | ], |
||
| 318 | 'content' => $this->lang['strsetval'], |
||
| 319 | ], |
||
| 320 | 'nextval' => [ |
||
| 321 | 'attr' => [ |
||
| 322 | 'href' => [ |
||
| 323 | 'url' => 'sequences', |
||
| 324 | 'urlvars' => [ |
||
| 325 | 'action' => 'nextval', |
||
| 326 | 'server' => $_REQUEST['server'], |
||
| 327 | 'database' => $_REQUEST['database'], |
||
| 328 | 'schema' => $_REQUEST['schema'], |
||
| 329 | 'sequence' => $sequence->fields['seqname'], |
||
| 330 | ], |
||
| 331 | ], |
||
| 332 | ], |
||
| 333 | 'content' => $this->lang['strnextval'], |
||
| 334 | ], |
||
| 335 | 'restart' => [ |
||
| 336 | 'attr' => [ |
||
| 337 | 'href' => [ |
||
| 338 | 'url' => 'sequences', |
||
| 339 | 'urlvars' => [ |
||
| 340 | 'action' => 'restart', |
||
| 341 | 'server' => $_REQUEST['server'], |
||
| 342 | 'database' => $_REQUEST['database'], |
||
| 343 | 'schema' => $_REQUEST['schema'], |
||
| 344 | 'sequence' => $sequence->fields['seqname'], |
||
| 345 | ], |
||
| 346 | ], |
||
| 347 | ], |
||
| 348 | 'content' => $this->lang['strrestart'], |
||
| 349 | ], |
||
| 350 | 'reset' => [ |
||
| 351 | 'attr' => [ |
||
| 352 | 'href' => [ |
||
| 353 | 'url' => 'sequences', |
||
| 354 | 'urlvars' => [ |
||
| 355 | 'action' => 'reset', |
||
| 356 | 'server' => $_REQUEST['server'], |
||
| 357 | 'database' => $_REQUEST['database'], |
||
| 358 | 'schema' => $_REQUEST['schema'], |
||
| 359 | 'sequence' => $sequence->fields['seqname'], |
||
| 360 | ], |
||
| 361 | ], |
||
| 362 | ], |
||
| 363 | 'content' => $this->lang['strreset'], |
||
| 364 | ], |
||
| 365 | 'showall' => [ |
||
| 366 | 'attr' => [ |
||
| 367 | 'href' => [ |
||
| 368 | 'url' => 'sequences', |
||
| 369 | 'urlvars' => [ |
||
| 370 | 'server' => $_REQUEST['server'], |
||
| 371 | 'database' => $_REQUEST['database'], |
||
| 372 | 'schema' => $_REQUEST['schema'], |
||
| 373 | ], |
||
| 374 | ], |
||
| 375 | ], |
||
| 376 | 'content' => $this->lang['strshowallsequences'], |
||
| 377 | ], |
||
| 378 | ]; |
||
| 379 | |||
| 380 | if (!$data->hasAlterSequenceStart()) { |
||
| 381 | unset($navlinks['restart']); |
||
| 382 | } |
||
| 383 | |||
| 384 | $this->printNavLinks($navlinks, 'sequences-properties', get_defined_vars()); |
||
| 385 | } else { |
||
| 386 | echo "<p>{$this->lang['strnodata']}</p>".PHP_EOL; |
||
| 387 | } |
||
| 388 | } |
||
| 389 | |||
| 390 | /** |
||
| 391 | * Drop a sequence. |
||
| 392 | * |
||
| 393 | * @param mixed $confirm |
||
| 394 | * @param mixed $msg |
||
| 395 | */ |
||
| 396 | public function doDrop($confirm, $msg = '') |
||
| 460 | } |
||
| 461 | } |
||
| 462 | } |
||
| 463 | } |
||
| 464 | |||
| 465 | /** |
||
| 466 | * Displays a screen where they can enter a new sequence. |
||
| 467 | * |
||
| 468 | * @param mixed $msg |
||
| 469 | */ |
||
| 470 | public function doCreateSequence($msg = '') |
||
| 471 | { |
||
| 472 | $data = $this->misc->getDatabaseAccessor(); |
||
| 473 | |||
| 474 | $this->coalesceArr($_POST, 'formSequenceName', ''); |
||
| 475 | |||
| 476 | $this->coalesceArr($_POST, 'formIncrement', ''); |
||
| 477 | |||
| 478 | $this->coalesceArr($_POST, 'formMinValue', ''); |
||
| 479 | |||
| 480 | $this->coalesceArr($_POST, 'formMaxValue', ''); |
||
| 481 | |||
| 482 | $this->coalesceArr($_POST, 'formStartValue', ''); |
||
| 483 | |||
| 484 | $this->coalesceArr($_POST, 'formCacheValue', ''); |
||
| 485 | |||
| 486 | $this->printTrail('schema'); |
||
| 487 | $this->printTitle($this->lang['strcreatesequence'], 'pg.sequence.create'); |
||
| 488 | $this->printMsg($msg); |
||
| 489 | |||
| 490 | echo '<form action="'.\SUBFOLDER.'/src/views/sequences" method="post">'.PHP_EOL; |
||
| 491 | echo '<table>'.PHP_EOL; |
||
| 492 | |||
| 493 | echo "<tr><th class=\"data left required\">{$this->lang['strname']}</th>".PHP_EOL; |
||
| 494 | echo "<td class=\"data1\"><input name=\"formSequenceName\" size=\"32\" maxlength=\"{$data->_maxNameLen}\" value=\"", |
||
| 495 | htmlspecialchars($_POST['formSequenceName']), '" /></td></tr>'.PHP_EOL; |
||
| 496 | |||
| 497 | echo "<tr><th class=\"data left\">{$this->lang['strincrementby']}</th>".PHP_EOL; |
||
| 498 | echo '<td class="data1"><input name="formIncrement" size="5" value="', |
||
| 499 | htmlspecialchars($_POST['formIncrement']), '" /> </td></tr>'.PHP_EOL; |
||
| 500 | |||
| 501 | echo "<tr><th class=\"data left\">{$this->lang['strminvalue']}</th>".PHP_EOL; |
||
| 502 | echo '<td class="data1"><input name="formMinValue" size="5" value="', |
||
| 503 | htmlspecialchars($_POST['formMinValue']), '" /></td></tr>'.PHP_EOL; |
||
| 504 | |||
| 505 | echo "<tr><th class=\"data left\">{$this->lang['strmaxvalue']}</th>".PHP_EOL; |
||
| 506 | echo '<td class="data1"><input name="formMaxValue" size="5" value="', |
||
| 507 | htmlspecialchars($_POST['formMaxValue']), '" /></td></tr>'.PHP_EOL; |
||
| 508 | |||
| 509 | echo "<tr><th class=\"data left\">{$this->lang['strstartvalue']}</th>".PHP_EOL; |
||
| 510 | echo '<td class="data1"><input name="formStartValue" size="5" value="', |
||
| 511 | htmlspecialchars($_POST['formStartValue']), '" /></td></tr>'.PHP_EOL; |
||
| 512 | |||
| 513 | echo "<tr><th class=\"data left\">{$this->lang['strcachevalue']}</th>".PHP_EOL; |
||
| 514 | echo '<td class="data1"><input name="formCacheValue" size="5" value="', |
||
| 515 | htmlspecialchars($_POST['formCacheValue']), '" /></td></tr>'.PHP_EOL; |
||
| 516 | |||
| 517 | echo "<tr><th class=\"data left\"><label for=\"formCycledValue\">{$this->lang['strcancycle']}</label></th>".PHP_EOL; |
||
| 518 | echo '<td class="data1"><input type="checkbox" id="formCycledValue" name="formCycledValue" ', |
||
| 519 | (isset($_POST['formCycledValue']) ? ' checked="checked"' : ''), ' /></td></tr>'.PHP_EOL; |
||
| 520 | |||
| 521 | echo '</table>'.PHP_EOL; |
||
| 522 | echo '<p><input type="hidden" name="action" value="save_create_sequence" />'.PHP_EOL; |
||
| 523 | echo $this->misc->form; |
||
| 524 | echo "<input type=\"submit\" name=\"create\" value=\"{$this->lang['strcreate']}\" />".PHP_EOL; |
||
| 525 | echo "<input type=\"submit\" name=\"cancel\" value=\"{$this->lang['strcancel']}\" /></p>".PHP_EOL; |
||
| 526 | echo '</form>'.PHP_EOL; |
||
| 527 | } |
||
| 528 | |||
| 529 | /** |
||
| 530 | * Actually creates the new sequence in the database. |
||
| 531 | */ |
||
| 532 | public function doSaveCreateSequence() |
||
| 533 | { |
||
| 534 | $data = $this->misc->getDatabaseAccessor(); |
||
| 535 | |||
| 536 | // Check that they've given a name and at least one column |
||
| 537 | if ('' == $_POST['formSequenceName']) { |
||
| 538 | $this->doCreateSequence($this->lang['strsequenceneedsname']); |
||
| 539 | } else { |
||
| 540 | $status = $data->createSequence( |
||
| 541 | $_POST['formSequenceName'], |
||
| 542 | $_POST['formIncrement'], |
||
| 543 | $_POST['formMinValue'], |
||
| 544 | $_POST['formMaxValue'], |
||
| 545 | $_POST['formStartValue'], |
||
| 546 | $_POST['formCacheValue'], |
||
| 547 | isset($_POST['formCycledValue']) |
||
| 548 | ); |
||
| 549 | if (0 == $status) { |
||
| 550 | $this->doDefault($this->lang['strsequencecreated']); |
||
| 551 | } else { |
||
| 552 | $this->doCreateSequence($this->lang['strsequencecreatedbad']); |
||
| 553 | } |
||
| 554 | } |
||
| 555 | } |
||
| 556 | |||
| 557 | /** |
||
| 558 | * Restarts a sequence. |
||
| 559 | */ |
||
| 560 | public function doRestart() |
||
| 561 | { |
||
| 562 | $data = $this->misc->getDatabaseAccessor(); |
||
| 563 | |||
| 564 | $status = $data->restartSequence($_REQUEST['sequence']); |
||
| 565 | if (0 == $status) { |
||
| 566 | $this->doProperties($this->lang['strsequencerestart']); |
||
| 567 | } else { |
||
| 568 | $this->doProperties($this->lang['strsequencerestartbad']); |
||
| 569 | } |
||
| 570 | } |
||
| 571 | |||
| 572 | /** |
||
| 573 | * Resets a sequence. |
||
| 574 | */ |
||
| 575 | public function doReset() |
||
| 576 | { |
||
| 577 | $data = $this->misc->getDatabaseAccessor(); |
||
| 578 | |||
| 579 | $status = $data->resetSequence($_REQUEST['sequence']); |
||
| 580 | if (0 == $status) { |
||
| 581 | $this->doProperties($this->lang['strsequencereset']); |
||
| 582 | } else { |
||
| 583 | $this->doProperties($this->lang['strsequenceresetbad']); |
||
| 584 | } |
||
| 585 | } |
||
| 586 | |||
| 587 | /** |
||
| 588 | * Set Nextval of a sequence. |
||
| 589 | */ |
||
| 590 | public function doNextval() |
||
| 591 | { |
||
| 592 | $data = $this->misc->getDatabaseAccessor(); |
||
| 593 | |||
| 594 | $status = $data->nextvalSequence($_REQUEST['sequence']); |
||
| 595 | if (0 == $status) { |
||
| 596 | $this->doProperties($this->lang['strsequencenextval']); |
||
| 597 | } else { |
||
| 598 | $this->doProperties($this->lang['strsequencenextvalbad']); |
||
| 599 | } |
||
| 600 | } |
||
| 601 | |||
| 602 | /** |
||
| 603 | * Function to save after 'setval'ing a sequence. |
||
| 604 | */ |
||
| 605 | public function doSaveSetval() |
||
| 606 | { |
||
| 607 | $data = $this->misc->getDatabaseAccessor(); |
||
| 608 | |||
| 609 | $status = $data->setvalSequence($_POST['sequence'], $_POST['nextvalue']); |
||
| 610 | if (0 == $status) { |
||
| 611 | $this->doProperties($this->lang['strsequencesetval']); |
||
| 612 | } else { |
||
| 613 | $this->doProperties($this->lang['strsequencesetvalbad']); |
||
| 614 | } |
||
| 615 | } |
||
| 616 | |||
| 617 | /** |
||
| 618 | * Function to allow 'setval'ing of a sequence. |
||
| 619 | * |
||
| 620 | * @param mixed $msg |
||
| 621 | */ |
||
| 622 | public function doSetval($msg = '') |
||
| 623 | { |
||
| 624 | $data = $this->misc->getDatabaseAccessor(); |
||
| 625 | |||
| 626 | $this->printTrail('sequence'); |
||
| 627 | $this->printTitle($this->lang['strsetval'], 'pg.sequence'); |
||
| 628 | $this->printMsg($msg); |
||
| 629 | |||
| 630 | // Fetch the sequence information |
||
| 631 | $sequence = $data->getSequence($_REQUEST['sequence']); |
||
| 632 | |||
| 633 | if (is_object($sequence) && $sequence->recordCount() > 0) { |
||
| 634 | echo '<form action="'.\SUBFOLDER.'/src/views/sequences" method="post">'.PHP_EOL; |
||
| 635 | echo '<table border="0">'; |
||
| 636 | echo "<tr><th class=\"data left required\">{$this->lang['strlastvalue']}</th>".PHP_EOL; |
||
| 637 | echo '<td class="data1">'; |
||
| 638 | echo "<input name=\"nextvalue\" size=\"32\" maxlength=\"{$data->_maxNameLen}\" value=\"", |
||
| 639 | $this->misc->printVal($sequence->fields['last_value']), '" /></td></tr>'.PHP_EOL; |
||
| 640 | echo '</table>'.PHP_EOL; |
||
| 641 | echo '<p><input type="hidden" name="action" value="setval" />'.PHP_EOL; |
||
| 642 | echo '<input type="hidden" name="sequence" value="', htmlspecialchars($_REQUEST['sequence']), '" />'.PHP_EOL; |
||
| 643 | echo $this->misc->form; |
||
| 644 | echo "<input type=\"submit\" name=\"setval\" value=\"{$this->lang['strsetval']}\" />".PHP_EOL; |
||
| 645 | echo "<input type=\"submit\" name=\"cancel\" value=\"{$this->lang['strcancel']}\" /></p>".PHP_EOL; |
||
| 646 | echo '</form>'.PHP_EOL; |
||
| 647 | } else { |
||
| 648 | echo "<p>{$this->lang['strnodata']}</p>".PHP_EOL; |
||
| 649 | } |
||
| 650 | } |
||
| 651 | |||
| 652 | /** |
||
| 653 | * Function to save after altering a sequence. |
||
| 654 | */ |
||
| 655 | public function doSaveAlter() |
||
| 707 | } |
||
| 708 | } |
||
| 709 | |||
| 710 | /** |
||
| 711 | * Function to allow altering of a sequence. |
||
| 712 | * |
||
| 713 | * @param mixed $msg |
||
| 714 | */ |
||
| 715 | public function doAlter($msg = '') |
||
| 821 | } |
||
| 822 | } |
||
| 823 | } |
||
| 824 |