Passed
Push — develop ( e54c4a...a6767a )
by Felipe
11:32 queued 05:47
created

MaterializedviewpropertiesController   B

Complexity

Total Complexity 49

Size/Duplication

Total Lines 363
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 49
dl 0
loc 363
rs 8.5454
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
B doEdit() 0 33 3
C doProperties() 0 88 7
C render() 0 75 16
A doSaveEdit() 0 9 2
D doAlter() 0 107 19
A doRefresh() 0 11 2

How to fix   Complexity   

Complex Class

Complex classes like MaterializedviewpropertiesController 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 MaterializedviewpropertiesController, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
/**
4
 * PHPPgAdmin v6.0.0-beta.45
5
 */
6
7
namespace PHPPgAdmin\Controller;
8
9
/**
10
 * Base controller class.
11
 *
12
 * @package PHPPgAdmin
13
 */
14
class MaterializedviewpropertiesController extends BaseController
15
{
16
    use \PHPPgAdmin\Traits\ExportTrait;
17
    use \PHPPgAdmin\Traits\ViewsAndMaterializedViewsTrait;
18
    public $controller_title = 'strviews';
19
    public $subject          = 'matview';
20
21
    /**
22
     * Default method to render the controller according to the action parameter.
23
     */
24
    public function render()
25
    {
26
        if ('tree' == $this->action) {
27
            return $this->doTree();
28
        }
29
30
        $this->printHeader($this->headerTitle('', '', $_REQUEST[$this->subject]));
31
        $this->printBody();
32
33
        switch ($this->action) {
34
            case 'save_edit':
35
                if (isset($_POST['cancel'])) {
36
                    $this->doDefinition();
37
                } else {
38
                    $this->doSaveEdit();
39
                }
40
41
                break;
42
            case 'edit':
43
                $this->doEdit();
44
45
                break;
46
            case 'export':
47
                $this->doExport();
48
49
                break;
50
51
            case 'refresh':
52
                $this->doRefresh();
53
54
                break;
55
56
            case 'definition':
57
                $this->doDefinition();
58
59
                break;
60
            case 'properties':
61
                if (isset($_POST['cancel'])) {
62
                    $this->doDefault();
63
                } else {
64
                    $this->doProperties();
65
                }
66
67
                break;
68
            case 'alter':
69
                if (isset($_POST['alter'])) {
70
                    $this->doAlter(false);
71
                } else {
72
                    $this->doDefault();
73
                }
74
75
                break;
76
            case 'confirm_alter':
77
                $this->doAlter(true);
78
79
                break;
80
            case 'drop':
81
                if (isset($_POST['drop'])) {
82
                    $this->doDrop(false);
0 ignored issues
show
Bug introduced by
The method doDrop() does not exist on PHPPgAdmin\Controller\Ma...iewpropertiesController. ( Ignorable by Annotation )

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

82
                    $this->/** @scrutinizer ignore-call */ 
83
                           doDrop(false);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
83
                } else {
84
                    $this->doDefault();
85
                }
86
87
                break;
88
            case 'confirm_drop':
89
                $this->doDrop(true);
90
91
                break;
92
            default:
93
                $this->doDefault();
94
95
                break;
96
        }
97
98
        $this->printFooter();
99
    }
100
101
    /**
102
     * Function to save after editing a matview.
103
     */
104
    public function doSaveEdit()
105
    {
106
        $data = $this->misc->getDatabaseAccessor();
107
108
        $status = $data->setView($_POST[$this->subject], $_POST['formDefinition'], $_POST['formComment']);
109
        if (0 == $status) {
110
            $this->doDefinition($this->lang['strviewupdated']);
111
        } else {
112
            $this->doEdit($this->lang['strviewupdatedbad']);
113
        }
114
    }
115
116
    /**
117
     * Function to refresh a matview.
118
     */
119
    public function doRefresh()
120
    {
121
        $data = $this->misc->getDatabaseAccessor();
122
        $sql  = 'REFRESH MATERIALIZED VIEW ' . $_REQUEST[$this->subject];
123
        $this->prtrace($sql);
124
        $status = $data->execute($sql);
125
126
        if (0 == $status) {
127
            $this->doDefault($this->lang['strviewupdated']);
128
        } else {
129
            $this->doDefault($this->lang['strviewupdatedbad']);
130
        }
131
    }
132
133
    /**
134
     * Function to allow editing of a matview.
135
     *
136
     * @param mixed $msg
137
     */
138
    public function doEdit($msg = '')
139
    {
140
        $data = $this->misc->getDatabaseAccessor();
141
142
        $this->printTrail($this->subject);
143
        $this->printTitle($this->lang['stredit'], 'pg.matview.alter');
144
        $this->printMsg($msg);
145
146
        $viewdata = $data->getView($_REQUEST[$this->subject]);
147
148
        if ($viewdata->recordCount() > 0) {
149
            if (!isset($_POST['formDefinition'])) {
150
                $_POST['formDefinition'] = $viewdata->fields['vwdefinition'];
151
                $_POST['formComment']    = $viewdata->fields['relcomment'];
152
            }
153
154
            echo '<form action="' . \SUBFOLDER . "/src/views/materializedviewproperties\" method=\"post\">\n";
155
            echo "<table style=\"width: 100%\">\n";
156
            echo "\t<tr>\n\t\t<th class=\"data left required\">{$this->lang['strdefinition']}</th>\n";
157
            echo "\t\t<td class=\"data1\"><textarea style=\"width: 100%;\" rows=\"20\" cols=\"50\" name=\"formDefinition\">",
158
            htmlspecialchars($_POST['formDefinition']), "</textarea></td>\n\t</tr>\n";
159
            echo "\t<tr>\n\t\t<th class=\"data left\">{$this->lang['strcomment']}</th>\n";
160
            echo "\t\t<td class=\"data1\"><textarea rows=\"3\" cols=\"32\" name=\"formComment\">",
161
            htmlspecialchars($_POST['formComment']), "</textarea></td>\n\t</tr>\n";
162
            echo "</table>\n";
163
            echo "<p><input type=\"hidden\" name=\"action\" value=\"save_edit\" />\n";
164
            echo '<input type="hidden" name="matview" value="', htmlspecialchars($_REQUEST[$this->subject]), "\" />\n";
165
            echo $this->misc->form;
166
            echo "<input type=\"submit\" value=\"{$this->lang['stralter']}\" />\n";
167
            echo "<input type=\"submit\" name=\"cancel\" value=\"{$this->lang['strcancel']}\" /></p>\n";
168
            echo "</form>\n";
169
        } else {
170
            echo "<p>{$this->lang['strnodata']}</p>\n";
171
        }
172
    }
173
174
    /**
175
     * Displays a screen where they can alter a column in a matview.
176
     *
177
     * @param mixed $msg
178
     */
179
    public function doProperties($msg = '')
180
    {
181
        $data = $this->misc->getDatabaseAccessor();
182
183
        if (!isset($_REQUEST['stage'])) {
184
            $_REQUEST['stage'] = 1;
185
        }
186
187
        switch ($_REQUEST['stage']) {
188
            case 1:
189
190
                $this->printTrail('column');
191
                $this->printTitle($this->lang['stralter'], 'pg.column.alter');
192
                $this->printMsg($msg);
193
194
                echo '<form action="' . \SUBFOLDER . "/src/views/materializedviewproperties\" method=\"post\">\n";
195
196
                // Output matview header
197
                echo "<table>\n";
198
                echo "<tr><th class=\"data required\">{$this->lang['strname']}</th><th class=\"data required\">{$this->lang['strtype']}</th>";
199
                echo "<th class=\"data\">{$this->lang['strdefault']}</th><th class=\"data\">{$this->lang['strcomment']}</th></tr>";
200
201
                $column = $data->getTableAttributes($_REQUEST[$this->subject], $_REQUEST['column']);
202
203
                if (!isset($_REQUEST['default'])) {
204
                    $_REQUEST['field']   = $column->fields['attname'];
205
                    $_REQUEST['default'] = $_REQUEST['olddefault'] = $column->fields['adsrc'];
206
                    $_REQUEST['comment'] = $column->fields['comment'];
207
                }
208
209
                echo '<tr><td><input name="field" size="32" value="',
210
                htmlspecialchars($_REQUEST['field']), '" /></td>';
211
212
                echo '<td>', $this->misc->printVal($data->formatType($column->fields['type'], $column->fields['atttypmod'])), '</td>';
213
                echo '<td><input name="default" size="20" value="',
214
                htmlspecialchars($_REQUEST['default']), '" /></td>';
215
                echo '<td><input name="comment" size="32" value="',
216
                htmlspecialchars($_REQUEST['comment']), '" /></td>';
217
218
                echo "</table>\n";
219
                echo "<p><input type=\"hidden\" name=\"action\" value=\"properties\" />\n";
220
                echo "<input type=\"hidden\" name=\"stage\" value=\"2\" />\n";
221
                echo $this->misc->form;
222
                echo '<input type="hidden" name="matview" value="', htmlspecialchars($_REQUEST[$this->subject]), "\" />\n";
223
                echo '<input type="hidden" name="column" value="', htmlspecialchars($_REQUEST['column']), "\" />\n";
224
                echo '<input type="hidden" name="olddefault" value="', htmlspecialchars($_REQUEST['olddefault']), "\" />\n";
225
                echo "<input type=\"submit\" value=\"{$this->lang['stralter']}\" />\n";
226
                echo "<input type=\"submit\" name=\"cancel\" value=\"{$this->lang['strcancel']}\" /></p>\n";
227
                echo "</form>\n";
228
229
                break;
230
            case 2:
231
232
                // Check inputs
233
                if ('' == trim($_REQUEST['field'])) {
234
                    $_REQUEST['stage'] = 1;
235
                    $this->doProperties($this->lang['strcolneedsname']);
236
237
                    return;
238
                }
239
240
                // Alter the matview column
241
                list($status, $sql) = $data->alterColumn(
242
                    $_REQUEST[$this->subject],
243
                    $_REQUEST['column'],
244
                    $_REQUEST['field'],
245
                    false,
246
                    false,
247
                    $_REQUEST['default'],
248
                    $_REQUEST['olddefault'],
249
                    '',
250
                    '',
251
                    '',
252
                    '',
253
                    $_REQUEST['comment']
254
                );
255
                if (0 == $status) {
256
                    $this->doDefault($this->lang['strcolumnaltered']);
257
                } else {
258
                    $_REQUEST['stage'] = 1;
259
                    $this->doProperties($this->lang['strcolumnalteredbad']);
260
261
                    return;
262
                }
263
264
                break;
265
            default:
266
                echo "<p>{$this->lang['strinvalidparam']}</p>\n";
267
        }
268
    }
269
270
    public function doAlter($confirm = false, $msg = '')
271
    {
272
        $data = $this->misc->getDatabaseAccessor();
273
274
        if ($confirm) {
275
            $this->printTrail($this->subject);
276
            $this->printTitle($this->lang['stralter'], 'pg.matview.alter');
277
            $this->printMsg($msg);
278
279
            // Fetch matview info
280
            $matview = $data->getView($_REQUEST[$this->subject]);
281
282
            if ($matview->recordCount() > 0) {
283
                if (!isset($_POST['name'])) {
284
                    $_POST['name'] = $matview->fields['relname'];
285
                }
286
287
                if (!isset($_POST['owner'])) {
288
                    $_POST['owner'] = $matview->fields['relowner'];
289
                }
290
291
                if (!isset($_POST['newschema'])) {
292
                    $_POST['newschema'] = $matview->fields['nspname'];
293
                }
294
295
                if (!isset($_POST['comment'])) {
296
                    $_POST['comment'] = $matview->fields['relcomment'];
297
                }
298
299
                echo '<form action="' . \SUBFOLDER . "/src/views/materializedviewproperties\" method=\"post\">\n";
300
                echo "<table>\n";
301
                echo "<tr><th class=\"data left required\">{$this->lang['strname']}</th>\n";
302
                echo '<td class="data1">';
303
                echo "<input name=\"name\" size=\"32\" maxlength=\"{$data->_maxNameLen}\" value=\"",
304
                htmlspecialchars($_POST['name']), "\" /></td></tr>\n";
305
306
                if ($data->isSuperUser()) {
307
                    // Fetch all users
308
                    $users = $data->getUsers();
309
310
                    echo "<tr><th class=\"data left required\">{$this->lang['strowner']}</th>\n";
311
                    echo '<td class="data1"><select name="owner">';
312
                    while (!$users->EOF) {
313
                        $uname = $users->fields['usename'];
314
                        echo '<option value="', htmlspecialchars($uname), '"',
315
                        ($uname == $_POST['owner']) ? ' selected="selected"' : '', '>', htmlspecialchars($uname), "</option>\n";
316
                        $users->moveNext();
317
                    }
318
                    echo "</select></td></tr>\n";
319
                }
320
321
                if ($data->hasAlterTableSchema()) {
322
                    $schemas = $data->getSchemas();
323
                    echo "<tr><th class=\"data left required\">{$this->lang['strschema']}</th>\n";
324
                    echo '<td class="data1"><select name="newschema">';
325
                    while (!$schemas->EOF) {
326
                        $schema = $schemas->fields['nspname'];
327
                        echo '<option value="', htmlspecialchars($schema), '"',
328
                        ($schema == $_POST['newschema']) ? ' selected="selected"' : '', '>', htmlspecialchars($schema), "</option>\n";
329
                        $schemas->moveNext();
330
                    }
331
                    echo "</select></td></tr>\n";
332
                }
333
334
                echo "<tr><th class=\"data left\">{$this->lang['strcomment']}</th>\n";
335
                echo '<td class="data1">';
336
                echo '<textarea rows="3" cols="32" name="comment">';
337
                echo htmlspecialchars($_POST['comment']), "</textarea></td></tr>\n";
338
                echo "</table>\n";
339
                echo "<input type=\"hidden\" name=\"action\" value=\"alter\" />\n";
340
                echo '<input type="hidden" name="matview" value="', htmlspecialchars($_REQUEST[$this->subject]), "\" />\n";
341
                echo $this->misc->form;
342
                echo "<p><input type=\"submit\" name=\"alter\" value=\"{$this->lang['stralter']}\" />\n";
343
                echo "<input type=\"submit\" name=\"cancel\" value=\"{$this->lang['strcancel']}\" /></p>\n";
344
                echo "</form>\n";
345
            } else {
346
                echo "<p>{$this->lang['strnodata']}</p>\n";
347
            }
348
        } else {
349
            // For databases that don't allow owner change
350
            if (!isset($_POST['owner'])) {
351
                $_POST['owner'] = '';
352
            }
353
354
            if (!isset($_POST['newschema'])) {
355
                $_POST['newschema'] = null;
356
            }
357
358
            $status = $data->alterView($_POST[$this->subject], $_POST['name'], $_POST['owner'], $_POST['newschema'], $_POST['comment']);
359
            if (0 == $status) {
360
                // If matview has been renamed, need to change to the new name and
361
                // reload the browser frame.
362
                if ($_POST[$this->subject] != $_POST['name']) {
363
                    // Jump them to the new matview name
364
                    $_REQUEST[$this->subject] = $_POST['name'];
365
                    // Force a browser reload
366
                    $this->misc->setReloadBrowser(true);
367
                }
368
                // If schema has changed, need to change to the new schema and reload the browser
369
                if (!empty($_POST['newschema']) && ($_POST['newschema'] != $data->_schema)) {
370
                    // Jump them to the new sequence schema
371
                    $this->misc->setCurrentSchema($_POST['newschema']);
372
                    $this->misc->setReloadBrowser(true);
373
                }
374
                $this->doDefault($this->lang['strviewaltered']);
375
            } else {
376
                $this->doAlter(true, $this->lang['strviewalteredbad']);
377
            }
378
        }
379
    }
380
}
381