| Total Complexity | 41 |
| Total Lines | 360 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like TablespacesController 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 TablespacesController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 14 | class TablespacesController extends BaseController |
||
| 15 | { |
||
| 16 | public $controller_name = 'TablespacesController'; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * Default method to render the controller according to the action parameter. |
||
| 20 | */ |
||
| 21 | public function render() |
||
| 22 | { |
||
| 23 | $this->printHeader($this->lang['strtablespaces']); |
||
| 24 | $this->printBody(); |
||
| 25 | |||
| 26 | switch ($this->action) { |
||
| 27 | case 'save_create': |
||
| 28 | if (isset($_REQUEST['cancel'])) { |
||
| 29 | $this->doDefault(); |
||
| 30 | } else { |
||
| 31 | $this->doSaveCreate(); |
||
| 32 | } |
||
| 33 | |||
| 34 | break; |
||
| 35 | case 'create': |
||
| 36 | $this->doCreate(); |
||
| 37 | |||
| 38 | break; |
||
| 39 | case 'drop': |
||
| 40 | if (isset($_REQUEST['cancel'])) { |
||
| 41 | $this->doDefault(); |
||
| 42 | } else { |
||
| 43 | $this->doDrop(false); |
||
| 44 | } |
||
| 45 | |||
| 46 | break; |
||
| 47 | case 'confirm_drop': |
||
| 48 | $this->doDrop(true); |
||
| 49 | |||
| 50 | break; |
||
| 51 | case 'save_edit': |
||
| 52 | if (isset($_REQUEST['cancel'])) { |
||
| 53 | $this->doDefault(); |
||
| 54 | } else { |
||
| 55 | $this->doSaveAlter(); |
||
| 56 | } |
||
| 57 | |||
| 58 | break; |
||
| 59 | case 'edit': |
||
| 60 | $this->doAlter(); |
||
| 61 | |||
| 62 | break; |
||
| 63 | default: |
||
| 64 | $this->doDefault(); |
||
| 65 | |||
| 66 | break; |
||
| 67 | } |
||
| 68 | |||
| 69 | $this->printFooter(); |
||
| 70 | } |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Show default list of tablespaces in the cluster. |
||
| 74 | * |
||
| 75 | * @param mixed $msg |
||
| 76 | */ |
||
| 77 | public function doDefault($msg = '') |
||
| 78 | { |
||
| 79 | $data = $this->misc->getDatabaseAccessor(); |
||
| 80 | |||
| 81 | $this->printTrail('server'); |
||
| 82 | $this->printTabs('server', 'tablespaces'); |
||
| 83 | $this->printMsg($msg); |
||
| 84 | |||
| 85 | $tablespaces = $data->getTablespaces(); |
||
| 86 | |||
| 87 | $columns = [ |
||
| 88 | 'database' => [ |
||
| 89 | 'title' => $this->lang['strname'], |
||
| 90 | 'field' => \PHPPgAdmin\Decorators\Decorator::field('spcname'), |
||
| 91 | ], |
||
| 92 | 'owner' => [ |
||
| 93 | 'title' => $this->lang['strowner'], |
||
| 94 | 'field' => \PHPPgAdmin\Decorators\Decorator::field('spcowner'), |
||
| 95 | ], |
||
| 96 | 'location' => [ |
||
| 97 | 'title' => $this->lang['strlocation'], |
||
| 98 | 'field' => \PHPPgAdmin\Decorators\Decorator::field('spclocation'), |
||
| 99 | ], |
||
| 100 | 'actions' => [ |
||
| 101 | 'title' => $this->lang['stractions'], |
||
| 102 | ], |
||
| 103 | ]; |
||
| 104 | |||
| 105 | if ($data->hasSharedComments()) { |
||
| 106 | $columns['comment'] = [ |
||
| 107 | 'title' => $this->lang['strcomment'], |
||
| 108 | 'field' => \PHPPgAdmin\Decorators\Decorator::field('spccomment'), |
||
| 109 | ]; |
||
| 110 | } |
||
| 111 | |||
| 112 | $actions = [ |
||
| 113 | 'alter' => [ |
||
| 114 | 'content' => $this->lang['stralter'], |
||
| 115 | 'attr' => [ |
||
| 116 | 'href' => [ |
||
| 117 | 'url' => 'tablespaces', |
||
| 118 | 'urlvars' => [ |
||
| 119 | 'action' => 'edit', |
||
| 120 | 'tablespace' => \PHPPgAdmin\Decorators\Decorator::field('spcname'), |
||
| 121 | ], |
||
| 122 | ], |
||
| 123 | ], |
||
| 124 | ], |
||
| 125 | 'drop' => [ |
||
| 126 | 'content' => $this->lang['strdrop'], |
||
| 127 | 'attr' => [ |
||
| 128 | 'href' => [ |
||
| 129 | 'url' => 'tablespaces', |
||
| 130 | 'urlvars' => [ |
||
| 131 | 'action' => 'confirm_drop', |
||
| 132 | 'tablespace' => \PHPPgAdmin\Decorators\Decorator::field('spcname'), |
||
| 133 | ], |
||
| 134 | ], |
||
| 135 | ], |
||
| 136 | ], |
||
| 137 | 'privileges' => [ |
||
| 138 | 'content' => $this->lang['strprivileges'], |
||
| 139 | 'attr' => [ |
||
| 140 | 'href' => [ |
||
| 141 | 'url' => 'privileges', |
||
| 142 | 'urlvars' => [ |
||
| 143 | 'subject' => 'tablespace', |
||
| 144 | 'tablespace' => \PHPPgAdmin\Decorators\Decorator::field('spcname'), |
||
| 145 | ], |
||
| 146 | ], |
||
| 147 | ], |
||
| 148 | ], |
||
| 149 | ]; |
||
| 150 | |||
| 151 | echo $this->printTable($tablespaces, $columns, $actions, 'tablespaces-tablespaces', $this->lang['strnotablespaces']); |
||
| 152 | |||
| 153 | $this->printNavLinks(['create' => [ |
||
|
1 ignored issue
–
show
|
|||
| 154 | 'attr' => [ |
||
| 155 | 'href' => [ |
||
| 156 | 'url' => 'tablespaces', |
||
| 157 | 'urlvars' => [ |
||
| 158 | 'action' => 'create', |
||
| 159 | 'server' => $_REQUEST['server'], |
||
| 160 | ], |
||
| 161 | ], |
||
| 162 | ], |
||
| 163 | 'content' => $this->lang['strcreatetablespace'], |
||
| 164 | ]], 'tablespaces-tablespaces', get_defined_vars()); |
||
|
1 ignored issue
–
show
|
|||
| 165 | } |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Function to allow altering of a tablespace. |
||
| 169 | * |
||
| 170 | * @param mixed $msg |
||
| 171 | */ |
||
| 172 | public function doAlter($msg = '') |
||
| 228 | } |
||
| 229 | } |
||
| 230 | |||
| 231 | /** |
||
| 232 | * Function to save after altering a tablespace. |
||
| 233 | */ |
||
| 234 | public function doSaveAlter() |
||
| 252 | } |
||
| 253 | } |
||
| 254 | } |
||
| 255 | |||
| 256 | /** |
||
| 257 | * Show confirmation of drop and perform actual drop. |
||
| 258 | * |
||
| 259 | * @param mixed $confirm |
||
| 260 | */ |
||
| 261 | public function doDrop($confirm) |
||
| 262 | { |
||
| 263 | $data = $this->misc->getDatabaseAccessor(); |
||
| 264 | |||
| 265 | if ($confirm) { |
||
| 266 | $this->printTrail('tablespace'); |
||
| 267 | $this->printTitle($this->lang['strdrop'], 'pg.tablespace.drop'); |
||
| 268 | |||
| 269 | echo '<p>', sprintf($this->lang['strconfdroptablespace'], $this->misc->printVal($_REQUEST['tablespace'])), "</p>\n"; |
||
| 270 | |||
| 271 | echo '<form action="'.\SUBFOLDER."/src/views/tablespaces\" method=\"post\">\n"; |
||
| 272 | echo $this->misc->form; |
||
| 273 | echo "<input type=\"hidden\" name=\"action\" value=\"drop\" />\n"; |
||
| 274 | echo '<input type="hidden" name="tablespace" value="', htmlspecialchars($_REQUEST['tablespace']), "\" />\n"; |
||
| 275 | echo "<input type=\"submit\" name=\"drop\" value=\"{$this->lang['strdrop']}\" />\n"; |
||
| 276 | echo "<input type=\"submit\" name=\"cancel\" value=\"{$this->lang['strcancel']}\" />\n"; |
||
| 277 | echo "</form>\n"; |
||
| 278 | } else { |
||
| 279 | $status = $data->droptablespace($_REQUEST['tablespace']); |
||
| 280 | if (0 == $status) { |
||
| 281 | $this->doDefault($this->lang['strtablespacedropped']); |
||
| 282 | } else { |
||
| 283 | $this->doDefault($this->lang['strtablespacedroppedbad']); |
||
| 284 | } |
||
| 285 | } |
||
| 286 | } |
||
| 287 | |||
| 288 | /** |
||
| 289 | * Displays a screen where they can enter a new tablespace. |
||
| 290 | * |
||
| 291 | * @param mixed $msg |
||
| 292 | */ |
||
| 293 | public function doCreate($msg = '') |
||
| 349 | } |
||
| 350 | |||
| 351 | /** |
||
| 352 | * Actually creates the new tablespace in the cluster. |
||
| 353 | */ |
||
| 354 | public function doSaveCreate() |
||
| 374 | } |
||
| 375 | } |
||
| 376 | } |
||
| 378 |