Passed
Push — develop ( 51bd2c...501708 )
by Felipe
05:51
created

TablespacesController   B

Complexity

Total Complexity 41

Size/Duplication

Total Lines 360
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 41
dl 0
loc 360
rs 8.2769
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
B doAlter() 0 56 9
C render() 0 49 10
A doSaveAlter() 0 18 4
B doCreate() 0 56 8
B doSaveCreate() 0 20 5
A doDrop() 0 23 3
B doDefault() 0 88 2

How to fix   Complexity   

Complex Class

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
2
3
/**
4
 * PHPPgAdmin v6.0.0-beta.40
5
 */
6
7
namespace PHPPgAdmin\Controller;
8
9
/**
10
 * Base controller class.
11
 *
12
 * @package PHPPgAdmin
13
 */
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
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
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
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
165
    }
166
167
    /**
168
     * Function to allow altering of a tablespace.
169
     *
170
     * @param mixed $msg
171
     */
172
    public function doAlter($msg = '')
173
    {
174
        $data = $this->misc->getDatabaseAccessor();
175
176
        $this->printTrail('tablespace');
177
        $this->printTitle($this->lang['stralter'], 'pg.tablespace.alter');
178
        $this->printMsg($msg);
179
180
        // Fetch tablespace info
181
        $tablespace = $data->getTablespace($_REQUEST['tablespace']);
182
        // Fetch all users
183
        $users = $data->getUsers();
184
185
        if ($tablespace->recordCount() > 0) {
186
            if (!isset($_POST['name'])) {
187
                $_POST['name'] = $tablespace->fields['spcname'];
188
            }
189
190
            if (!isset($_POST['owner'])) {
191
                $_POST['owner'] = $tablespace->fields['spcowner'];
192
            }
193
194
            if (!isset($_POST['comment'])) {
195
                $_POST['comment'] = ($data->hasSharedComments()) ? $tablespace->fields['spccomment'] : '';
196
            }
197
198
            echo '<form action="'.\SUBFOLDER."/src/views/tablespaces\" method=\"post\">\n";
199
            echo $this->misc->form;
200
            echo "<table>\n";
201
            echo "<tr><th class=\"data left required\">{$this->lang['strname']}</th>\n";
202
            echo '<td class="data1">';
203
            echo "<input name=\"name\" size=\"32\" maxlength=\"{$data->_maxNameLen}\" value=\"",
204
            htmlspecialchars($_POST['name']), "\" /></td></tr>\n";
205
            echo "<tr><th class=\"data left required\">{$this->lang['strowner']}</th>\n";
206
            echo '<td class="data1"><select name="owner">';
207
            while (!$users->EOF) {
208
                $uname = $users->fields['usename'];
209
                echo '<option value="', htmlspecialchars($uname), '"',
210
                ($uname == $_POST['owner']) ? ' selected="selected"' : '', '>', htmlspecialchars($uname), "</option>\n";
211
                $users->moveNext();
212
            }
213
            echo "</select></td></tr>\n";
214
            if ($data->hasSharedComments()) {
215
                echo "<tr><th class=\"data left\">{$this->lang['strcomment']}</th>\n";
216
                echo '<td class="data1">';
217
                echo '<textarea rows="3" cols="32" name="comment">',
218
                htmlspecialchars($_POST['comment']), "</textarea></td></tr>\n";
219
            }
220
            echo "</table>\n";
221
            echo "<p><input type=\"hidden\" name=\"action\" value=\"save_edit\" />\n";
222
            echo '<input type="hidden" name="tablespace" value="', htmlspecialchars($_REQUEST['tablespace']), "\" />\n";
223
            echo "<input type=\"submit\" name=\"alter\" value=\"{$this->lang['stralter']}\" />\n";
224
            echo "<input type=\"submit\" name=\"cancel\" value=\"{$this->lang['strcancel']}\" /></p>\n";
225
            echo "</form>\n";
226
        } else {
227
            echo "<p>{$this->lang['strnodata']}</p>\n";
228
        }
229
    }
230
231
    /**
232
     * Function to save after altering a tablespace.
233
     */
234
    public function doSaveAlter()
235
    {
236
        $data = $this->misc->getDatabaseAccessor();
237
238
        // Check data
239
        if ('' == trim($_POST['name'])) {
240
            $this->doAlter($this->lang['strtablespaceneedsname']);
241
        } else {
242
            $status = $data->alterTablespace($_POST['tablespace'], $_POST['name'], $_POST['owner'], $_POST['comment']);
243
            if (0 == $status) {
244
                // If tablespace has been renamed, need to change to the new name
245
                if ($_POST['tablespace'] != $_POST['name']) {
246
                    // Jump them to the new table name
247
                    $_REQUEST['tablespace'] = $_POST['name'];
248
                }
249
                $this->doDefault($this->lang['strtablespacealtered']);
250
            } else {
251
                $this->doAlter($this->lang['strtablespacealteredbad']);
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 = '')
294
    {
295
        $data = $this->misc->getDatabaseAccessor();
296
297
        $server_info = $this->misc->getServerInfo();
298
299
        if (!isset($_POST['formSpcname'])) {
300
            $_POST['formSpcname'] = '';
301
        }
302
303
        if (!isset($_POST['formOwner'])) {
304
            $_POST['formOwner'] = $server_info['username'];
305
        }
306
307
        if (!isset($_POST['formLoc'])) {
308
            $_POST['formLoc'] = '';
309
        }
310
311
        if (!isset($_POST['formComment'])) {
312
            $_POST['formComment'] = '';
313
        }
314
315
        // Fetch all users
316
        $users = $data->getUsers();
317
318
        $this->printTrail('server');
319
        $this->printTitle($this->lang['strcreatetablespace'], 'pg.tablespace.create');
320
        $this->printMsg($msg);
321
322
        echo '<form action="'.\SUBFOLDER."/src/views/tablespaces\" method=\"post\">\n";
323
        echo $this->misc->form;
324
        echo "<table>\n";
325
        echo "\t<tr>\n\t\t<th class=\"data left required\">{$this->lang['strname']}</th>\n";
326
        echo "\t\t<td class=\"data1\"><input size=\"32\" name=\"formSpcname\" maxlength=\"{$data->_maxNameLen}\" value=\"", htmlspecialchars($_POST['formSpcname']), "\" /></td>\n\t</tr>\n";
327
        echo "\t<tr>\n\t\t<th class=\"data left required\">{$this->lang['strowner']}</th>\n";
328
        echo "\t\t<td class=\"data1\"><select name=\"formOwner\">\n";
329
        while (!$users->EOF) {
330
            $uname = $users->fields['usename'];
331
            echo "\t\t\t<option value=\"", htmlspecialchars($uname), '"',
332
            ($uname == $_POST['formOwner']) ? ' selected="selected"' : '', '>', htmlspecialchars($uname), "</option>\n";
333
            $users->moveNext();
334
        }
335
        echo "\t\t</select></td>\n\t</tr>\n";
336
        echo "\t<tr>\n\t\t<th class=\"data left required\">{$this->lang['strlocation']}</th>\n";
337
        echo "\t\t<td class=\"data1\"><input size=\"32\" name=\"formLoc\" value=\"", htmlspecialchars($_POST['formLoc']), "\" /></td>\n\t</tr>\n";
338
        // Comments (if available)
339
        if ($data->hasSharedComments()) {
340
            echo "\t<tr>\n\t\t<th class=\"data left\">{$this->lang['strcomment']}</th>\n";
341
            echo "\t\t<td><textarea name=\"formComment\" rows=\"3\" cols=\"32\">",
342
            htmlspecialchars($_POST['formComment']), "</textarea></td>\n\t</tr>\n";
343
        }
344
        echo "</table>\n";
345
        echo "<p><input type=\"hidden\" name=\"action\" value=\"save_create\" />\n";
346
        echo "<input type=\"submit\" value=\"{$this->lang['strcreate']}\" />\n";
347
        echo "<input type=\"submit\" name=\"cancel\" value=\"{$this->lang['strcancel']}\" /></p>\n";
348
        echo "</form>\n";
349
    }
350
351
    /**
352
     * Actually creates the new tablespace in the cluster.
353
     */
354
    public function doSaveCreate()
355
    {
356
        $data = $this->misc->getDatabaseAccessor();
357
358
        // Check data
359
        if ('' == trim($_POST['formSpcname'])) {
360
            $this->doCreate($this->lang['strtablespaceneedsname']);
361
        } elseif ('' == trim($_POST['formLoc'])) {
362
            $this->doCreate($this->lang['strtablespaceneedsloc']);
363
        } else {
364
            // Default comment to blank if it isn't set
365
            if (!isset($_POST['formComment'])) {
366
                $_POST['formComment'] = null;
367
            }
368
369
            $status = $data->createTablespace($_POST['formSpcname'], $_POST['formOwner'], $_POST['formLoc'], $_POST['formComment']);
370
            if (0 == $status) {
371
                $this->doDefault($this->lang['strtablespacecreated']);
372
            } else {
373
                $this->doCreate($this->lang['strtablespacecreatedbad']);
374
            }
375
        }
376
    }
377
}
378