| Total Complexity | 43 |
| Total Lines | 360 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like BaseController 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 BaseController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 14 | class BaseController |
||
| 15 | { |
||
| 16 | use \PHPPgAdmin\HelperTrait; |
||
|
1 ignored issue
–
show
|
|||
| 17 | |||
| 18 | protected $container; |
||
| 19 | protected $_connection; |
||
|
1 ignored issue
–
show
|
|||
| 20 | protected $app; |
||
| 21 | protected $data; |
||
| 22 | protected $database; |
||
| 23 | protected $server_id; |
||
| 24 | public $appLangFiles = []; |
||
| 25 | public $appThemes = []; |
||
| 26 | public $appName = ''; |
||
| 27 | public $appVersion = ''; |
||
| 28 | public $form = ''; |
||
| 29 | public $href = ''; |
||
| 30 | public $lang = []; |
||
| 31 | public $action = ''; |
||
| 32 | public $controller_name = 'BaseController'; |
||
| 33 | public $controller_title = 'base'; |
||
| 34 | protected $table_controller; |
||
| 35 | protected $trail_controller; |
||
| 36 | protected $tree_controller; |
||
| 37 | protected $footer_controller; |
||
| 38 | protected $header_controller; |
||
| 39 | protected $scripts = ''; |
||
| 40 | public $msg = ''; |
||
| 41 | public $view; |
||
| 42 | public $plugin_manager; |
||
| 43 | public $misc; |
||
| 44 | public $conf; |
||
| 45 | public $phpMinVer; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Constructs the base controller (common for almost all controllers). |
||
| 49 | * |
||
| 50 | * @param \Slim\Container $container the $app container |
||
| 51 | * @param bool $no_db_connection [optional] if true, sets $this->misc->setNoDBConnection(true); |
||
| 52 | */ |
||
| 53 | public function __construct(\Slim\Container $container, $no_db_connection = false) |
||
| 54 | { |
||
| 55 | $this->container = $container; |
||
| 56 | $this->lang = $container->get('lang'); |
||
| 57 | |||
| 58 | $this->view = $container->get('view'); |
||
| 59 | $this->plugin_manager = $container->get('plugin_manager'); |
||
| 60 | $this->msg = $container->get('msg'); |
||
| 61 | $this->appLangFiles = $container->get('appLangFiles'); |
||
| 62 | |||
| 63 | $this->misc = $container->get('misc'); |
||
| 64 | $this->conf = $this->misc->getConf(); |
||
| 65 | |||
| 66 | $this->appThemes = $container->get('appThemes'); |
||
| 67 | $this->action = $container->get('action'); |
||
| 68 | |||
| 69 | $this->appName = $container->get('settings')['appName']; |
||
| 70 | $this->appVersion = $container->get('settings')['appVersion']; |
||
| 71 | $this->postgresqlMinVer = $container->get('settings')['postgresqlMinVer']; |
||
| 72 | $this->phpMinVer = $container->get('settings')['phpMinVer']; |
||
| 73 | |||
| 74 | $msg = $container->get('msg'); |
||
|
1 ignored issue
–
show
|
|||
| 75 | |||
| 76 | if (true === $no_db_connection) { |
||
| 77 | $this->misc->setNoDBConnection(true); |
||
| 78 | } |
||
| 79 | |||
| 80 | if (false === $this->misc->getNoDBConnection()) { |
||
| 81 | if (null === $this->misc->getServerId()) { |
||
| 82 | $servers_controller = new \PHPPgAdmin\Controller\ServersController($container, true); |
||
| 83 | |||
| 84 | return $servers_controller->render(); |
||
| 85 | } |
||
| 86 | $_server_info = $this->misc->getServerInfo(); |
||
| 87 | // Redirect to the login form if not logged in |
||
| 88 | if (!isset($_server_info['username'])) { |
||
| 89 | $msg = sprintf($this->lang['strlogoutmsg'], $_server_info['desc']); |
||
| 90 | |||
| 91 | $servers_controller = new \PHPPgAdmin\Controller\ServersController($container, true); |
||
| 92 | |||
| 93 | return $servers_controller->render(); |
||
| 94 | } |
||
| 95 | } |
||
| 96 | |||
| 97 | //\PC::debug(['name' => $this->controller_name, 'no_db_connection' => $this->misc->getNoDBConnection()], 'instanced controller'); |
||
| 98 | } |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Default method to render the controller according to the action parameter. |
||
| 102 | */ |
||
| 103 | public function render() |
||
| 104 | { |
||
| 105 | $this->misc = $this->misc; |
||
| 106 | $lang = $this->lang; |
||
| 107 | $action = $this->action; |
||
| 108 | |||
| 109 | $this->printHeader($lang[$this->controller_title]); |
||
| 110 | $this->printBody(); |
||
| 111 | |||
| 112 | switch ($action) { |
||
| 113 | default: |
||
| 114 | $this->doDefault(); |
||
| 115 | |||
| 116 | break; |
||
| 117 | } |
||
| 118 | |||
| 119 | $this->printFooter(); |
||
| 120 | } |
||
| 121 | |||
| 122 | public function doDefault() |
||
| 128 | } |
||
| 129 | |||
| 130 | public function getContainer() |
||
| 131 | { |
||
| 132 | return $this->container; |
||
| 133 | } |
||
| 134 | |||
| 135 | private function getTableController() |
||
|
1 ignored issue
–
show
|
|||
| 136 | { |
||
| 137 | if (null === $this->table_controller) { |
||
| 138 | $this->table_controller = new \PHPPgAdmin\XHtml\HTMLTableController($this->getContainer(), $this->controller_name); |
||
| 139 | } |
||
| 140 | |||
| 141 | return $this->table_controller; |
||
| 142 | } |
||
| 143 | |||
| 144 | private function getFooterController() |
||
|
1 ignored issue
–
show
|
|||
| 145 | { |
||
| 146 | if (null === $this->footer_controller) { |
||
| 147 | $this->footer_controller = new \PHPPgAdmin\XHtml\HTMLFooterController($this->getContainer(), $this->controller_name); |
||
| 148 | } |
||
| 149 | |||
| 150 | return $this->footer_controller; |
||
| 151 | } |
||
| 152 | |||
| 153 | private function getHeaderController() |
||
|
1 ignored issue
–
show
|
|||
| 154 | { |
||
| 155 | if (null === $this->header_controller) { |
||
| 156 | $this->header_controller = new \PHPPgAdmin\XHtml\HTMLHeaderController($this->getContainer(), $this->controller_name); |
||
| 157 | } |
||
| 158 | |||
| 159 | return $this->header_controller; |
||
| 160 | } |
||
| 161 | |||
| 162 | private function getNavbarController() |
||
|
1 ignored issue
–
show
|
|||
| 163 | { |
||
| 164 | if (null === $this->trail_controller) { |
||
| 165 | $this->trail_controller = new \PHPPgAdmin\XHtml\HTMLNavbarController($this->getContainer(), $this->controller_name); |
||
| 166 | } |
||
| 167 | |||
| 168 | return $this->trail_controller; |
||
| 169 | } |
||
| 170 | |||
| 171 | private function getTreeController() |
||
|
1 ignored issue
–
show
|
|||
| 172 | { |
||
| 173 | if (null === $this->tree_controller) { |
||
| 174 | $this->tree_controller = new \PHPPgAdmin\XHtml\TreeController($this->getContainer(), $this->controller_name); |
||
| 175 | } |
||
| 176 | |||
| 177 | return $this->tree_controller; |
||
| 178 | } |
||
| 179 | |||
| 180 | /** |
||
| 181 | * Instances an HTMLTable and returns its html content. |
||
| 182 | * |
||
| 183 | * @param [type] &$tabledata [description] |
||
| 184 | * @param [type] &$columns [description] |
||
| 185 | * @param [type] &$actions [description] |
||
| 186 | * @param [type] $place [description] |
||
| 187 | * @param [type] $nodata [description] |
||
| 188 | * @param [type] $pre_fn [description] |
||
| 189 | * |
||
| 190 | * @return [type] [description] |
||
| 191 | */ |
||
| 192 | public function printTable(&$tabledata, &$columns, &$actions, $place, $nodata = null, $pre_fn = null) |
||
| 193 | { |
||
| 194 | $html_table = $this->getTableController(); |
||
| 195 | |||
| 196 | return $html_table->printTable($tabledata, $columns, $actions, $place, $nodata, $pre_fn); |
||
| 197 | } |
||
| 198 | |||
| 199 | public function adjustTabsForTree($tabs) |
||
| 200 | { |
||
| 201 | $tree = $this->getTreeController(); |
||
| 202 | |||
| 203 | return $tree->adjustTabsForTree($tabs); |
||
| 204 | } |
||
| 205 | |||
| 206 | public function printTree(&$_treedata, &$attrs, $section, $print = true) |
||
| 207 | { |
||
| 208 | $tree = $this->getTreeController(); |
||
| 209 | |||
| 210 | return $tree->printTree($_treedata, $attrs, $section, $print); |
||
| 211 | } |
||
| 212 | |||
| 213 | public function printTrail($trail = [], $do_print = true) |
||
| 219 | } |
||
| 220 | |||
| 221 | public function printNavLinks($navlinks, $place, $env = [], $do_print = true) |
||
| 222 | { |
||
| 223 | $from = __METHOD__; |
||
| 224 | $html_trail = $this->getNavbarController(); |
||
| 225 | |||
| 226 | return $html_trail->printNavLinks($navlinks, $place, $env, $do_print, $from); |
||
| 227 | } |
||
| 228 | |||
| 229 | public function printTabs($tabs, $activetab, $do_print = true) |
||
| 230 | { |
||
| 231 | $from = __METHOD__; |
||
| 232 | $html_trail = $this->getNavbarController(); |
||
| 233 | |||
| 234 | return $html_trail->printTabs($tabs, $activetab, $do_print, $from); |
||
| 235 | } |
||
| 236 | |||
| 237 | public function getLastTabURL($section) |
||
| 242 | } |
||
| 243 | |||
| 244 | public function printLink($link, $do_print = true, $from = null) |
||
| 245 | { |
||
| 246 | if (null === $from) { |
||
| 247 | $from = __METHOD__; |
||
| 248 | } |
||
| 249 | |||
| 250 | $html_trail = $this->getNavbarController(); |
||
| 251 | |||
| 252 | return $html_trail->printLink($link, $do_print, $from); |
||
| 253 | } |
||
| 254 | |||
| 255 | public function setReloadDropDatabase($flag) |
||
| 256 | { |
||
| 257 | $footer_controller = $this->getFooterController(); |
||
| 258 | |||
| 259 | return $footer_controller->setReloadDropDatabase($flag); |
||
| 260 | } |
||
| 261 | |||
| 262 | public function setNoBottomLink($flag) |
||
| 263 | { |
||
| 264 | $footer_controller = $this->getFooterController(); |
||
| 265 | |||
| 266 | return $footer_controller->setNoBottomLink($flag); |
||
| 267 | } |
||
| 268 | |||
| 269 | public function printFooter($doBody = true, $template = 'footer.twig') |
||
| 270 | { |
||
| 271 | $footer_controller = $this->getFooterController(); |
||
| 272 | |||
| 273 | return $footer_controller->printFooter($doBody, $template); |
||
| 274 | } |
||
| 275 | |||
| 276 | public function printReload($database, $do_print = true) |
||
| 277 | { |
||
| 278 | $footer_controller = $this->getFooterController(); |
||
| 279 | |||
| 280 | return $footer_controller->printReload($database, $do_print); |
||
| 281 | } |
||
| 282 | |||
| 283 | /** |
||
| 284 | * Outputs JavaScript to set default focus. |
||
| 285 | * |
||
| 286 | * @param mixed $object eg. forms[0].username |
||
| 287 | */ |
||
| 288 | public function setFocus($object) |
||
| 289 | { |
||
| 290 | $footer_controller = $this->getFooterController(); |
||
| 291 | |||
| 292 | return $footer_controller->setFocus($object); |
||
| 293 | } |
||
| 294 | |||
| 295 | /** |
||
| 296 | * Outputs JavaScript to set the name of the browser window. |
||
| 297 | * |
||
| 298 | * @param string $name the window name |
||
| 299 | * @param bool $addServer if true (default) then the server id is |
||
| 300 | * attached to the name |
||
| 301 | */ |
||
| 302 | public function setWindowName($name, $addServer = true) |
||
| 303 | { |
||
| 304 | $footer_controller = $this->getFooterController(); |
||
| 305 | |||
| 306 | return $footer_controller->setWindowName($name, $addServer); |
||
| 307 | } |
||
| 308 | |||
| 309 | public function setNoOutput($flag) |
||
| 310 | { |
||
| 311 | $header_controller = $this->getHeaderController(); |
||
| 312 | |||
| 313 | return $header_controller->setNoOutput((bool) $flag); |
||
| 314 | } |
||
| 315 | |||
| 316 | public function printHeader($title = '', $script = null, $do_print = true, $template = 'header.twig') |
||
| 321 | } |
||
| 322 | |||
| 323 | public function printBody($doBody = true, $bodyClass = 'detailbody', $onloadInit = false) |
||
| 324 | { |
||
| 325 | $header_controller = $this->getHeaderController(); |
||
| 326 | |||
| 327 | return $header_controller->printBody($doBody, $bodyClass, $onloadInit); |
||
| 328 | } |
||
| 329 | |||
| 330 | public function printTitle($title, $help = null, $do_print = true) |
||
| 335 | } |
||
| 336 | |||
| 337 | public function getRequestParam($key, $default = null) |
||
| 338 | { |
||
| 339 | return $this->container->requestobj->getParam($key, $default); |
||
| 340 | } |
||
| 341 | |||
| 342 | public function getPostParam($key, $default = null) |
||
| 343 | { |
||
| 344 | return $this->container->requestobj->getParsedBodyParam($key, $default); |
||
| 345 | } |
||
| 346 | |||
| 347 | public function getQueryParam($key, $default = null) |
||
| 348 | { |
||
| 349 | return $this->container->requestobj->getQueryParam($key, $default); |
||
| 350 | } |
||
| 351 | |||
| 352 | /** |
||
| 353 | * Print out a message. |
||
| 354 | * |
||
| 355 | * @param string $msg The message |
||
| 356 | * @param bool $do_print if true, print the message. Return the string otherwise |
||
| 357 | * |
||
| 358 | * @return string a paragraph containing the message, whose linebreaks are replaced by <br> elements |
||
| 359 | */ |
||
| 360 | public function printMsg($msg, $do_print = true) |
||
| 374 | } |
||
| 375 | } |
||
| 376 |