| Total Complexity | 42 |
| Total Lines | 520 |
| Duplicated Lines | 0 % |
| Changes | 3 | ||
| Bugs | 1 | Features | 0 |
Complex classes like SchemasController 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 SchemasController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 14 | class SchemasController extends BaseController |
||
| 15 | { |
||
| 16 | use \PHPPgAdmin\Traits\ExportTrait; |
||
|
|
|||
| 17 | |||
| 18 | public $controller_title = 'strschemas'; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * Default method to render the controller according to the action parameter. |
||
| 22 | */ |
||
| 23 | public function render() |
||
| 86 | } |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Show default list of schemas in the database. |
||
| 90 | * |
||
| 91 | * @param mixed $msg |
||
| 92 | */ |
||
| 93 | public function doDefault($msg = ''): void |
||
| 192 | } |
||
| 193 | |||
| 194 | /** |
||
| 195 | * Generate XML for the browser tree. |
||
| 196 | */ |
||
| 197 | public function doTree() |
||
| 198 | { |
||
| 199 | $data = $this->misc->getDatabaseAccessor(); |
||
| 200 | |||
| 201 | $schemas = $data->getSchemas(); |
||
| 202 | |||
| 203 | $reqvars = $this->misc->getRequestVars('schema'); |
||
| 204 | |||
| 205 | $attrs = [ |
||
| 206 | 'text' => Decorator::field('nspname'), |
||
| 207 | 'icon' => 'Schema', |
||
| 208 | 'toolTip' => Decorator::field('nspcomment'), |
||
| 209 | 'action' => Decorator::redirecturl( |
||
| 210 | 'redirect', |
||
| 211 | $reqvars, |
||
| 212 | [ |
||
| 213 | 'subject' => 'schema', |
||
| 214 | 'schema' => Decorator::field('nspname'), |
||
| 215 | ] |
||
| 216 | ), |
||
| 217 | 'branch' => Decorator::url( |
||
| 218 | 'schemas', |
||
| 219 | $reqvars, |
||
| 220 | [ |
||
| 221 | 'action' => 'subtree', |
||
| 222 | 'schema' => Decorator::field('nspname'), |
||
| 223 | ] |
||
| 224 | ), |
||
| 225 | ]; |
||
| 226 | |||
| 227 | return $this->printTree($schemas, $attrs, 'schemas'); |
||
| 228 | } |
||
| 229 | |||
| 230 | public function doSubTree() |
||
| 231 | { |
||
| 232 | $tabs = $this->misc->getNavTabs('schema'); |
||
| 233 | |||
| 234 | $items = $this->adjustTabsForTree($tabs); |
||
| 235 | |||
| 236 | $reqvars = $this->misc->getRequestVars('schema'); |
||
| 237 | |||
| 238 | $attrs = [ |
||
| 239 | 'text' => Decorator::field('title'), |
||
| 240 | 'icon' => Decorator::field('icon'), |
||
| 241 | 'action' => Decorator::actionurl( |
||
| 242 | Decorator::field('url'), |
||
| 243 | $reqvars, |
||
| 244 | Decorator::field('urlvars', []) |
||
| 245 | ), |
||
| 246 | 'branch' => Decorator::url( |
||
| 247 | Decorator::field('url'), |
||
| 248 | $reqvars, |
||
| 249 | Decorator::field('urlvars'), |
||
| 250 | ['action' => 'tree'] |
||
| 251 | ), |
||
| 252 | ]; |
||
| 253 | |||
| 254 | return $this->printTree($items, $attrs, 'schema'); |
||
| 255 | } |
||
| 256 | |||
| 257 | /** |
||
| 258 | * Displays a screen where they can enter a new schema. |
||
| 259 | * |
||
| 260 | * @param mixed $msg |
||
| 261 | */ |
||
| 262 | public function doCreate($msg = ''): void |
||
| 312 | } |
||
| 313 | |||
| 314 | /** |
||
| 315 | * Actually creates the new schema in the database. |
||
| 316 | */ |
||
| 317 | public function doSaveCreate(): void |
||
| 318 | { |
||
| 319 | $data = $this->misc->getDatabaseAccessor(); |
||
| 320 | |||
| 321 | // Check that they've given a name |
||
| 322 | if ('' === $_POST['formName']) { |
||
| 323 | $this->doCreate($this->lang['strschemaneedsname']); |
||
| 324 | } else { |
||
| 325 | $status = $data->createSchema($_POST['formName'], $_POST['formAuth'], $_POST['formComment']); |
||
| 326 | |||
| 327 | if (0 === $status) { |
||
| 328 | $this->view->setReloadBrowser(true); |
||
| 329 | $this->doDefault($this->lang['strschemacreated']); |
||
| 330 | } else { |
||
| 331 | $this->doCreate($this->lang['strschemacreatedbad']); |
||
| 332 | } |
||
| 333 | } |
||
| 334 | } |
||
| 335 | |||
| 336 | /** |
||
| 337 | * Display a form to permit editing schema properies. |
||
| 338 | * TODO: permit changing owner. |
||
| 339 | * |
||
| 340 | * @param mixed $msg |
||
| 341 | */ |
||
| 342 | public function doAlter($msg = ''): void |
||
| 401 | } |
||
| 402 | } |
||
| 403 | |||
| 404 | /** |
||
| 405 | * Save the form submission containing changes to a schema. |
||
| 406 | * |
||
| 407 | * @param mixed $msg |
||
| 408 | */ |
||
| 409 | public function doSaveAlter(): void |
||
| 420 | } |
||
| 421 | } |
||
| 422 | |||
| 423 | /** |
||
| 424 | * Show confirmation of drop and perform actual drop. |
||
| 425 | * |
||
| 426 | * @param mixed $confirm |
||
| 427 | */ |
||
| 428 | public function doDrop($confirm) |
||
| 429 | { |
||
| 430 | $data = $this->misc->getDatabaseAccessor(); |
||
| 431 | |||
| 432 | if (empty($_REQUEST['nsp']) && empty($_REQUEST['ma'])) { |
||
| 433 | return $this->doDefault($this->lang['strspecifyschematodrop']); |
||
| 434 | } |
||
| 435 | |||
| 436 | if ($confirm) { |
||
| 437 | $this->printTrail('schema'); |
||
| 438 | $this->printTitle($this->lang['strdrop'], 'pg.schema.drop'); |
||
| 439 | |||
| 440 | echo '<form action="' . \containerInstance()->subFolder . '/src/views/schemas" method="post">' . \PHP_EOL; |
||
| 441 | //If multi drop |
||
| 442 | if (isset($_REQUEST['ma'])) { |
||
| 443 | foreach ($_REQUEST['ma'] as $v) { |
||
| 444 | $a = \unserialize(\htmlspecialchars_decode($v, \ENT_QUOTES)); |
||
| 445 | echo '<p>', \sprintf($this->lang['strconfdropschema'], $this->misc->printVal($a['nsp'])), '</p>' . \PHP_EOL; |
||
| 446 | echo '<input type="hidden" name="nsp[]" value="', \htmlspecialchars($a['nsp']), '" />' . \PHP_EOL; |
||
| 447 | } |
||
| 448 | } else { |
||
| 449 | echo '<p>', \sprintf($this->lang['strconfdropschema'], $this->misc->printVal($_REQUEST['nsp'])), '</p>' . \PHP_EOL; |
||
| 450 | echo '<input type="hidden" name="nsp" value="', \htmlspecialchars($_REQUEST['nsp']), '" />' . \PHP_EOL; |
||
| 451 | } |
||
| 452 | |||
| 453 | echo "<p><input type=\"checkbox\" id=\"cascade\" name=\"cascade\" /> <label for=\"cascade\">{$this->lang['strcascade']}</label></p>" . \PHP_EOL; |
||
| 454 | echo '<p><input type="hidden" name="action" value="drop" />' . \PHP_EOL; |
||
| 455 | echo '<input type="hidden" name="database" value="', \htmlspecialchars($_REQUEST['database']), '" />' . \PHP_EOL; |
||
| 456 | echo $this->view->form; |
||
| 457 | echo "<input type=\"submit\" name=\"drop\" value=\"{$this->lang['strdrop']}\" />" . \PHP_EOL; |
||
| 458 | echo \sprintf('<input type="submit" name="cancel" value="%s" /></p>%s', $this->lang['strcancel'], \PHP_EOL); |
||
| 459 | echo '</form>' . \PHP_EOL; |
||
| 460 | } else { |
||
| 461 | if (\is_array($_POST['nsp'])) { |
||
| 462 | $msg = ''; |
||
| 463 | $status = $data->beginTransaction(); |
||
| 464 | |||
| 465 | if (0 === $status) { |
||
| 466 | foreach ($_POST['nsp'] as $s) { |
||
| 467 | $status = $data->dropSchema($s, isset($_POST['cascade'])); |
||
| 468 | |||
| 469 | if (0 === $status) { |
||
| 470 | $msg .= \sprintf( |
||
| 471 | '%s: %s<br />', |
||
| 472 | \htmlentities($s, \ENT_QUOTES, 'UTF-8'), |
||
| 473 | $this->lang['strschemadropped'] |
||
| 474 | ); |
||
| 475 | } else { |
||
| 476 | $data->endTransaction(); |
||
| 477 | $this->doDefault(\sprintf( |
||
| 478 | '%s%s: %s<br />', |
||
| 479 | $msg, |
||
| 480 | \htmlentities($s, \ENT_QUOTES, 'UTF-8'), |
||
| 481 | $this->lang['strschemadroppedbad'] |
||
| 482 | )); |
||
| 483 | |||
| 484 | return; |
||
| 485 | } |
||
| 486 | } |
||
| 487 | } |
||
| 488 | |||
| 489 | if (0 === $data->endTransaction()) { |
||
| 490 | // Everything went fine, back to the Default page.... |
||
| 491 | $this->view->setReloadBrowser(true); |
||
| 492 | $this->doDefault($msg); |
||
| 493 | } else { |
||
| 494 | $this->doDefault($this->lang['strschemadroppedbad']); |
||
| 495 | } |
||
| 496 | } else { |
||
| 497 | $status = $data->dropSchema($_POST['nsp'], isset($_POST['cascade'])); |
||
| 498 | |||
| 499 | if (0 === $status) { |
||
| 500 | $this->view->setReloadBrowser(true); |
||
| 501 | $this->doDefault($this->lang['strschemadropped']); |
||
| 502 | } else { |
||
| 503 | $this->doDefault($this->lang['strschemadroppedbad']); |
||
| 504 | } |
||
| 505 | } |
||
| 506 | } |
||
| 507 | } |
||
| 508 | |||
| 509 | /** |
||
| 510 | * Displays options for database download. |
||
| 511 | * |
||
| 512 | * @param mixed $msg |
||
| 513 | */ |
||
| 514 | public function doExport($msg = ''): void |
||
| 534 | } |
||
| 535 | } |
||
| 536 |