Test Failed
Pull Request — develop (#380)
by Felipe
04:40
created

TablespacesController::doCreate()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 75
Code Lines 59

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 59
nc 4
nop 1
dl 0
loc 75
rs 8.8945
c 1
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * PHPPgAdmin 6.1.3
5
 */
6
7
namespace PHPPgAdmin\Controller;
8
9
use PHPPgAdmin\Decorators\Decorator;
10
11
/**
12
 * Base controller class.
13
 */
14
class TablespacesController extends BaseController
15
{
16
    public $controller_title = 'strtablespaces';
17
18
    /**
19
     * Default method to render the controller according to the action parameter.
20
     */
21
    public function render(): void
22
    {
23
        $this->printHeader();
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
64
            default:
65
                $this->doDefault();
66
67
                break;
68
        }
69
70
        $this->printFooter();
71
    }
72
73
    /**
74
     * Show default list of tablespaces in the cluster.
75
     *
76
     * @param mixed $msg
77
     */
78
    public function doDefault($msg = ''): void
79
    {
80
        $data = $this->misc->getDatabaseAccessor();
81
82
        $this->printTrail('server');
83
        $this->printTabs('server', 'tablespaces');
84
        $this->printMsg($msg);
85
86
        $tablespaces = $data->getTablespaces();
87
88
        $columns = [
89
            'database' => [
90
                'title' => $this->lang['strname'],
91
                'field' => Decorator::field('spcname'),
92
            ],
93
            'owner' => [
94
                'title' => $this->lang['strowner'],
95
                'field' => Decorator::field('spcowner'),
96
            ],
97
            'location' => [
98
                'title' => $this->lang['strlocation'],
99
                'field' => Decorator::field('spclocation'),
100
            ],
101
            'actions' => [
102
                'title' => $this->lang['stractions'],
103
            ],
104
        ];
105
106
        if ($data->hasSharedComments()) {
107
            $columns['comment'] = [
108
                'title' => $this->lang['strcomment'],
109
                'field' => Decorator::field('spccomment'),
110
            ];
111
        }
112
113
        $actions = [
114
            'alter' => [
115
                'content' => $this->lang['stralter'],
116
                'attr' => [
117
                    'href' => [
118
                        'url' => 'tablespaces',
119
                        'urlvars' => [
120
                            'action' => 'edit',
121
                            'tablespace' => Decorator::field('spcname'),
122
                        ],
123
                    ],
124
                ],
125
            ],
126
            'drop' => [
127
                'content' => $this->lang['strdrop'],
128
                'attr' => [
129
                    'href' => [
130
                        'url' => 'tablespaces',
131
                        'urlvars' => [
132
                            'action' => 'confirm_drop',
133
                            'tablespace' => Decorator::field('spcname'),
134
                        ],
135
                    ],
136
                ],
137
            ],
138
            'privileges' => [
139
                'content' => $this->lang['strprivileges'],
140
                'attr' => [
141
                    'href' => [
142
                        'url' => 'privileges',
143
                        'urlvars' => [
144
                            'subject' => 'tablespace',
145
                            'tablespace' => Decorator::field('spcname'),
146
                        ],
147
                    ],
148
                ],
149
            ],
150
        ];
151
152
        echo $this->printTable($tablespaces, $columns, $actions, 'tablespaces-tablespaces', $this->lang['strnotablespaces']);
0 ignored issues
show
Bug introduced by
It seems like $tablespaces can also be of type integer; however, parameter $tabledata of PHPPgAdmin\Controller\BaseController::printTable() does only seem to accept PHPPgAdmin\ADORecordSet|PHPPgAdmin\ArrayRecordSet, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

152
        echo $this->printTable(/** @scrutinizer ignore-type */ $tablespaces, $columns, $actions, 'tablespaces-tablespaces', $this->lang['strnotablespaces']);
Loading history...
153
154
        $this->printNavLinks(['create' => [
155
            'attr' => [
156
                'href' => [
157
                    'url' => 'tablespaces',
158
                    'urlvars' => [
159
                        'action' => 'create',
160
                        'server' => $_REQUEST['server'],
161
                    ],
162
                ],
163
            ],
164
            'content' => $this->lang['strcreatetablespace'],
165
        ]], 'tablespaces-tablespaces', \get_defined_vars());
166
    }
167
168
    /**
169
     * Function to allow altering of a tablespace.
170
     *
171
     * @param mixed $msg
172
     */
173
    public function doAlter($msg = ''): void
174
    {
175
        $data = $this->misc->getDatabaseAccessor();
176
177
        $this->printTrail('tablespace');
178
        $this->printTitle($this->lang['stralter'], 'pg.tablespace.alter');
179
        $this->printMsg($msg);
180
181
        // Fetch tablespace info
182
        $tablespace = $data->getTablespace($_REQUEST['tablespace']);
183
        // Fetch all users
184
        $users = $data->getUsers();
185
186
        if (0 < $tablespace->recordCount()) {
187
            $this->coalesceArr($_POST, 'name', $tablespace->fields['spcname']);
188
189
            $this->coalesceArr($_POST, 'owner', $tablespace->fields['spcowner']);
190
191
            $this->coalesceArr($_POST, 'comment', ($data->hasSharedComments()) ? $tablespace->fields['spccomment'] : '');
192
193
            echo '<form action="' . \containerInstance()->subFolder . '/src/views/tablespaces" method="post">' . \PHP_EOL;
194
            echo $this->view->form;
195
            echo '<table>' . \PHP_EOL;
196
            echo \sprintf(
197
                '<tr><th class="data left required">%s</th>',
198
                $this->lang['strname']
199
            ) . \PHP_EOL;
200
            echo '<td class="data1">';
201
            echo \sprintf(
202
                '<input name="name" size="32" maxlength="%s" value="',
203
                $data->_maxNameLen
204
            ),
205
            \htmlspecialchars($_POST['name']), '" /></td></tr>' . \PHP_EOL;
206
            echo \sprintf(
207
                '<tr><th class="data left required">%s</th>',
208
                $this->lang['strowner']
209
            ) . \PHP_EOL;
210
            echo '<td class="data1"><select name="owner">';
211
212
            while (!$users->EOF) {
213
                $uname = $users->fields['usename'];
214
                echo '<option value="', \htmlspecialchars($uname), '"',
215
                ($uname === $_POST['owner']) ? ' selected="selected"' : '', '>', \htmlspecialchars($uname), '</option>' . \PHP_EOL;
216
                $users->moveNext();
217
            }
218
            echo '</select></td></tr>' . \PHP_EOL;
219
220
            if ($data->hasSharedComments()) {
221
                echo \sprintf(
222
                    '<tr><th class="data left">%s</th>',
223
                    $this->lang['strcomment']
224
                ) . \PHP_EOL;
225
                echo '<td class="data1">';
226
                echo '<textarea rows="3" cols="32" name="comment">',
227
                \htmlspecialchars($_POST['comment']), '</textarea></td></tr>' . \PHP_EOL;
228
            }
229
            echo '</table>' . \PHP_EOL;
230
            echo '<p><input type="hidden" name="action" value="save_edit" />' . \PHP_EOL;
231
            echo '<input type="hidden" name="tablespace" value="', \htmlspecialchars($_REQUEST['tablespace']), '" />' . \PHP_EOL;
232
            echo \sprintf(
233
                '<input type="submit" name="alter" value="%s" />',
234
                $this->lang['stralter']
235
            ) . \PHP_EOL;
236
            echo \sprintf(
237
                '<input type="submit" name="cancel" value="%s"  /></p>%s',
238
                $this->lang['strcancel'],
239
                \PHP_EOL
240
            );
241
            echo '</form>' . \PHP_EOL;
242
        } else {
243
            echo \sprintf(
244
                '<p>%s</p>',
245
                $this->lang['strnodata']
246
            ) . \PHP_EOL;
247
        }
248
    }
249
250
    /**
251
     * Function to save after altering a tablespace.
252
     */
253
    public function doSaveAlter(): void
254
    {
255
        $data = $this->misc->getDatabaseAccessor();
256
257
        // Check data
258
        if ('' === \trim($_POST['name'])) {
259
            $this->doAlter($this->lang['strtablespaceneedsname']);
260
        } else {
261
            $status = $data->alterTablespace($_POST['tablespace'], $_POST['name'], $_POST['owner'], $_POST['comment']);
262
263
            if (0 === $status) {
264
                // If tablespace has been renamed, need to change to the new name
265
                if ($_POST['tablespace'] !== $_POST['name']) {
266
                    // Jump them to the new table name
267
                    $_REQUEST['tablespace'] = $_POST['name'];
268
                }
269
                $this->doDefault($this->lang['strtablespacealtered']);
270
            } else {
271
                $this->doAlter($this->lang['strtablespacealteredbad']);
272
            }
273
        }
274
    }
275
276
    /**
277
     * Show confirmation of drop and perform actual drop.
278
     *
279
     * @param mixed $confirm
280
     */
281
    public function doDrop($confirm): void
282
    {
283
        $data = $this->misc->getDatabaseAccessor();
284
285
        if ($confirm) {
286
            $this->printTrail('tablespace');
287
            $this->printTitle($this->lang['strdrop'], 'pg.tablespace.drop');
288
289
            echo '<p>', \sprintf(
290
                $this->lang['strconfdroptablespace'],
291
                $this->misc->printVal($_REQUEST['tablespace'])
292
            ), '</p>' . \PHP_EOL;
293
294
            echo '<form action="' . \containerInstance()->subFolder . '/src/views/tablespaces" method="post">' . \PHP_EOL;
295
            echo $this->view->form;
296
            echo '<input type="hidden" name="action" value="drop" />' . \PHP_EOL;
297
            echo '<input type="hidden" name="tablespace" value="', \htmlspecialchars($_REQUEST['tablespace']), '" />' . \PHP_EOL;
298
            echo \sprintf(
299
                '<input type="submit" name="drop" value="%s" />',
300
                $this->lang['strdrop']
301
            ) . \PHP_EOL;
302
            echo \sprintf(
303
                '<input type="submit" name="cancel" value="%s" />',
304
                $this->lang['strcancel']
305
            ) . \PHP_EOL;
306
            echo '</form>' . \PHP_EOL;
307
        } else {
308
            $status = $data->droptablespace($_REQUEST['tablespace']);
309
310
            if (0 === $status) {
311
                $this->doDefault($this->lang['strtablespacedropped']);
312
            } else {
313
                $this->doDefault($this->lang['strtablespacedroppedbad']);
314
            }
315
        }
316
    }
317
318
    /**
319
     * Displays a screen where they can enter a new tablespace.
320
     *
321
     * @param mixed $msg
322
     */
323
    public function doCreate($msg = ''): void
324
    {
325
        $data = $this->misc->getDatabaseAccessor();
326
327
        $server_info = $this->misc->getServerInfo();
328
329
        $this->coalesceArr($_POST, 'formSpcname', '');
330
331
        $this->coalesceArr($_POST, 'formOwner', $server_info['username']);
332
333
        $this->coalesceArr($_POST, 'formLoc', '');
334
335
        $this->coalesceArr($_POST, 'formComment', '');
336
337
        // Fetch all users
338
        $users = $data->getUsers();
339
340
        $this->printTrail('server');
341
        $this->printTitle($this->lang['strcreatetablespace'], 'pg.tablespace.create');
342
        $this->printMsg($msg);
343
344
        echo '<form action="' . \containerInstance()->subFolder . '/src/views/tablespaces" method="post">' . \PHP_EOL;
345
        echo $this->view->form;
346
        echo '<table>' . \PHP_EOL;
347
        echo \sprintf(
348
            '	<tr>
349
		<th class="data left required">%s</th>',
350
            $this->lang['strname']
351
        ) . \PHP_EOL;
352
        echo \sprintf(
353
            '		<td class="data1"><input size="32" name="formSpcname" maxlength="%s" value="',
354
            $data->_maxNameLen
355
        ), \htmlspecialchars($_POST['formSpcname']), "\" /></td>\n\t</tr>" . \PHP_EOL;
356
        echo \sprintf(
357
            '	<tr>
358
		<th class="data left required">%s</th>',
359
            $this->lang['strowner']
360
        ) . \PHP_EOL;
361
        echo "\t\t<td class=\"data1\"><select name=\"formOwner\">" . \PHP_EOL;
362
363
        while (!$users->EOF) {
364
            $uname = $users->fields['usename'];
365
            echo "\t\t\t<option value=\"", \htmlspecialchars($uname), '"',
366
            ($uname === $_POST['formOwner']) ? ' selected="selected"' : '', '>', \htmlspecialchars($uname), '</option>' . \PHP_EOL;
367
            $users->moveNext();
368
        }
369
        echo "\t\t</select></td>\n\t</tr>" . \PHP_EOL;
370
        echo \sprintf(
371
            '	<tr>
372
		<th class="data left required">%s</th>',
373
            $this->lang['strlocation']
374
        ) . \PHP_EOL;
375
        echo "\t\t<td class=\"data1\"><input size=\"32\" name=\"formLoc\" value=\"", \htmlspecialchars($_POST['formLoc']), "\" /></td>\n\t</tr>" . \PHP_EOL;
376
        // Comments (if available)
377
        if ($data->hasSharedComments()) {
378
            echo \sprintf(
379
                '	<tr>
380
		<th class="data left">%s</th>',
381
                $this->lang['strcomment']
382
            ) . \PHP_EOL;
383
            echo "\t\t<td><textarea name=\"formComment\" rows=\"3\" cols=\"32\">",
384
            \htmlspecialchars($_POST['formComment']), "</textarea></td>\n\t</tr>" . \PHP_EOL;
385
        }
386
        echo '</table>' . \PHP_EOL;
387
        echo '<p><input type="hidden" name="action" value="save_create" />' . \PHP_EOL;
388
        echo \sprintf(
389
            '<input type="submit" value="%s" />',
390
            $this->lang['strcreate']
391
        ) . \PHP_EOL;
392
        echo \sprintf(
393
            '<input type="submit" name="cancel" value="%s"  /></p>%s',
394
            $this->lang['strcancel'],
395
            \PHP_EOL
396
        );
397
        echo '</form>' . \PHP_EOL;
398
    }
399
400
    /**
401
     * Actually creates the new tablespace in the cluster.
402
     */
403
    public function doSaveCreate(): void
404
    {
405
        $data = $this->misc->getDatabaseAccessor();
406
407
        // Check data
408
        if ('' === \trim($_POST['formSpcname'])) {
409
            $this->doCreate($this->lang['strtablespaceneedsname']);
410
        } elseif ('' === \trim($_POST['formLoc'])) {
411
            $this->doCreate($this->lang['strtablespaceneedsloc']);
412
        } else {
413
            // Default comment to blank if it isn't set
414
            $this->coalesceArr($_POST, 'formComment', null);
415
416
            $status = $data->createTablespace($_POST['formSpcname'], $_POST['formOwner'], $_POST['formLoc'], $_POST['formComment']);
417
418
            if (0 === $status) {
419
                $this->doDefault($this->lang['strtablespacecreated']);
420
            } else {
421
                $this->doCreate($this->lang['strtablespacecreatedbad']);
422
            }
423
        }
424
    }
425
}
426