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