Passed
Pull Request — develop (#92)
by Felipe
04:47
created

ServersController::doDefault()   C

Complexity

Conditions 7
Paths 8

Size

Total Lines 77
Code Lines 51

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 51
nc 8
nop 1
dl 0
loc 77
rs 6.5755
c 0
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
0 ignored issues
show
Coding Style introduced by
You must use "/**" style comments for a file comment
Loading history...
3
/*
4
 * PHPPgAdmin v6.0.0-beta.30
5
 */
6
7
namespace PHPPgAdmin\Controller;
8
9
use PHPPgAdmin\Decorators\Decorator;
10
11
/**
12
 * Base controller class.
13
 */
5 ignored issues
show
Coding Style introduced by
Missing @category tag in class comment
Loading history...
Coding Style introduced by
Missing @package tag in class comment
Loading history...
Coding Style introduced by
Missing @author tag in class comment
Loading history...
Coding Style introduced by
Missing @license tag in class comment
Loading history...
Coding Style introduced by
Missing @link tag in class comment
Loading history...
14
class ServersController extends BaseController
15
{
16
    public $controller_name = 'ServersController';
17
    public $table_place     = 'servers-servers';
18
    public $section         = 'servers';
19
    public $query           = '';
20
    public $subject         = '';
21
    public $start_time;
22
    public $duration;
23
24
    /**
25
     * Default method to render the controller according to the action parameter.
26
     */
27
    public function render()
28
    {
29
        $conf = $this->conf;
0 ignored issues
show
Unused Code introduced by
The assignment to $conf is dead and can be removed.
Loading history...
30
31
        $lang = $this->lang;
0 ignored issues
show
Unused Code introduced by
The assignment to $lang is dead and can be removed.
Loading history...
32
33
        $action = $this->action;
34
35
        if ('tree' == $action) {
36
            return $this->doTree();
37
        }
38
39
        $msg = $this->msg;
40
41
        $server_html = $this->printHeader($this->lang['strservers'], null, false);
42
        $server_html .= $this->printBody(false);
43
        $server_html .= $this->printTrail('root', false);
44
45
        ob_start();
46
        switch ($action) {
47
            case 'logout':
48
                $this->doLogout();
49
50
                break;
51
            default:
52
                $this->doDefault($msg);
53
54
                break;
55
        }
56
57
        $server_html .= ob_get_clean();
58
59
        $server_html .= $this->printFooter(false);
60
61
        if (null === $this->container->requestobj->getAttribute('route')) {
62
            echo $server_html;
63
        } else {
64
            $body = $this->container->responseobj->getBody();
65
            $body->write($server_html);
66
67
            return $this->container->responseobj;
68
        }
69
    }
70
71
    public function doDefault($msg = '')
1 ignored issue
show
Coding Style introduced by
Missing function doc comment
Loading history...
72
    {
73
        $lang = $this->lang;
74
        $conf = $this->conf;
75
76
        $data = $this->misc->getDatabaseAccessor();
0 ignored issues
show
Unused Code introduced by
The assignment to $data is dead and can be removed.
Loading history...
77
78
        $this->printTabs('root', 'servers');
79
        $this->printMsg($msg);
80
        $group = isset($_GET['group']) ? $_GET['group'] : false;
81
82
        $groups  = $this->misc->getServersGroups(true, $group);
83
        $columns = [
84
            'group' => [
85
                'title' => $lang['strgroup'],
86
                'field' => Decorator::field('desc'),
87
                'url'   => 'servers.php?',
88
                'vars'  => ['group' => 'id'],
89
            ],
90
        ];
91
        $actions = [];
92
        if ((false !== $group) && (isset($conf['srv_groups'][$group])) && ($groups->recordCount() > 0)) {
93
            $this->printTitle(sprintf($lang['strgroupgroups'], htmlentities($conf['srv_groups'][$group]['desc'], ENT_QUOTES, 'UTF-8')));
94
        }
95
        $this->printTable($groups, $columns, $actions, $this->table_place);
96
        $servers = $this->misc->getServers(true, $group);
97
98
        $columns = [
99
            'server' => [
100
                'title' => $lang['strserver'],
101
                'field' => Decorator::field('desc'),
102
                'url'   => \SUBFOLDER.'/redirect/server?',
103
                'vars'  => ['server' => 'id'],
104
            ],
105
            'host' => [
106
                'title' => $lang['strhost'],
107
                'field' => Decorator::field('host'),
108
            ],
109
            'port' => [
110
                'title' => $lang['strport'],
111
                'field' => Decorator::field('port'),
112
            ],
113
            'username' => [
114
                'title' => $lang['strusername'],
115
                'field' => Decorator::field('username'),
116
            ],
117
            'actions' => [
118
                'title' => $lang['stractions'],
119
            ],
120
        ];
121
122
        $actions = [
123
            'logout' => [
124
                'content' => $lang['strlogout'],
125
                'attr'    => [
126
                    'href' => [
127
                        'url'     => 'servers.php',
128
                        'urlvars' => [
129
                            'action'       => 'logout',
130
                            'logoutServer' => Decorator::field('id'),
131
                        ],
132
                    ],
133
                ],
134
            ],
135
        ];
136
137
        $svPre = function (&$rowdata) use ($actions) {
138
            $actions['logout']['disable'] = empty($rowdata->fields['username']);
139
140
            return $actions;
141
        };
142
143
        if ((false !== $group) and isset($conf['srv_groups'][$group])) {
144
            $this->printTitle(sprintf($lang['strgroupservers'], htmlentities($conf['srv_groups'][$group]['desc'], ENT_QUOTES, 'UTF-8')), null);
145
            $actions['logout']['attr']['href']['urlvars']['group'] = $group;
146
        }
147
        echo $this->printTable($servers, $columns, $actions, $this->table_place, $lang['strnoobjects'], $svPre);
148
    }
149
150
    public function doTree()
1 ignored issue
show
Coding Style introduced by
Missing function doc comment
Loading history...
151
    {
152
        $conf = $this->conf;
153
154
        $nodes    = [];
155
        $group_id = isset($_GET['group']) ? $_GET['group'] : false;
156
157
        // root with srv_groups
158
        if (isset($conf['srv_groups']) and count($conf['srv_groups']) > 0
159
            and false === $group_id) {
1 ignored issue
show
Coding Style introduced by
Closing parenthesis of a multi-line IF statement must be on a new line
Loading history...
160
            $nodes = $this->misc->getServersGroups(true);
161
        } elseif (isset($conf['srv_groups']) and false !== $group_id) {
162
            // group subtree
163
            if ('all' !== $group_id) {
164
                $nodes = $this->misc->getServersGroups(false, $group_id);
165
            }
166
167
            $nodes = array_merge($nodes, $this->misc->getServers(false, $group_id));
168
            $nodes = new \PHPPgAdmin\ArrayRecordSet($nodes);
169
        } else {
170
            // no srv_group
171
            $nodes = $this->misc->getServers(true, false);
172
        }
173
174
        $reqvars = $this->misc->getRequestVars('server');
0 ignored issues
show
Unused Code introduced by
The assignment to $reqvars is dead and can be removed.
Loading history...
175
176
        //$this->prtrace($reqvars);
177
178
        $attrs = [
179
            'text' => Decorator::field('desc'),
180
            // Show different icons for logged in/out
181
            'icon'    => Decorator::field('icon'),
182
            'toolTip' => Decorator::field('id'),
183
            'action'  => Decorator::field('action'),
184
            // Only create a branch url if the user has
185
            // logged into the server.
186
            'branch' => Decorator::field('branch'),
187
        ];
188
        /*$this->prtrace([
189
        'nodes'   => $nodes,
190
        'attrs'   => $attrs,
191
        'section' => $this->section,
192
        ]);*/
193
        return $this->printTree($nodes, $attrs, $this->section);
194
    }
195
196
    public function doLogout()
1 ignored issue
show
Coding Style introduced by
Missing function doc comment
Loading history...
197
    {
198
        $plugin_manager = $this->plugin_manager;
199
        $lang           = $this->lang;
200
        $this->misc     = $this->misc;
201
        $conf           = $this->conf;
0 ignored issues
show
Unused Code introduced by
The assignment to $conf is dead and can be removed.
Loading history...
202
        $data           = $this->misc->getDatabaseAccessor();
0 ignored issues
show
Unused Code introduced by
The assignment to $data is dead and can be removed.
Loading history...
203
204
        $plugin_manager->do_hook('logout', $_REQUEST['logoutServer']);
205
206
        $server_info = $this->misc->getServerInfo($_REQUEST['logoutServer']);
207
        $this->misc->setServerInfo(null, null, $_REQUEST['logoutServer']);
208
209
        unset($_SESSION['sharedUsername'], $_SESSION['sharedPassword']);
210
211
        $this->misc->setReloadBrowser(true);
212
213
        echo sprintf($lang['strlogoutmsg'], $server_info['desc']);
214
    }
215
}
216