| Total Complexity | 47 |
| Total Lines | 400 |
| Duplicated Lines | 0 % |
| Changes | 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 |
||
| 15 | class BaseController |
||
| 16 | { |
||
| 17 | use \PHPPgAdmin\Traits\HelperTrait; |
||
| 18 | |||
| 19 | protected $container; |
||
| 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; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Used. |
||
| 36 | * |
||
| 37 | * @var string |
||
| 38 | */ |
||
| 39 | public $view_name; |
||
| 40 | /** |
||
| 41 | * Used to print the title passing its value to $lang. |
||
| 42 | * |
||
| 43 | * @var string |
||
| 44 | */ |
||
| 45 | public $controller_title = 'base'; |
||
| 46 | protected $table_controller; |
||
| 47 | protected $trail_controller; |
||
| 48 | protected $tree_controller; |
||
| 49 | protected $footer_controller; |
||
| 50 | protected $header_controller; |
||
| 51 | protected $scripts = ''; |
||
| 52 | public $msg = ''; |
||
| 53 | public $view; |
||
| 54 | public $plugin_manager; |
||
| 55 | public $misc; |
||
| 56 | public $conf; |
||
| 57 | public $phpMinVer; |
||
| 58 | protected $no_db_connection = false; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Constructs the base controller (common for almost all controllers). |
||
| 62 | * |
||
| 63 | * @param \Slim\Container $container the $app container |
||
| 64 | * @param bool $no_db_connection [optional] if true, sets $this->misc->setNoDBConnection(true); |
||
| 65 | */ |
||
| 66 | public function __construct(\Slim\Container $container) |
||
| 67 | { |
||
| 68 | $this->container = $container; |
||
| 69 | $this->lang = $container->get('lang'); |
||
| 70 | |||
| 71 | $this->controller_name = str_replace(__NAMESPACE__ . '\\', '', get_class($this)); |
||
| 72 | $this->view_name = str_replace('controller', '', strtolower($this->controller_name)); |
||
| 73 | $this->script = $this->view_name; |
||
| 74 | |||
| 75 | $this->view = $container->get('view'); |
||
| 76 | $this->plugin_manager = $container->get('plugin_manager'); |
||
| 77 | $this->msg = $container->get('msg'); |
||
| 78 | $this->appLangFiles = $container->get('appLangFiles'); |
||
| 79 | |||
| 80 | $this->misc = $container->get('misc'); |
||
| 81 | $this->conf = $this->misc->getConf(); |
||
| 82 | |||
| 83 | $this->appThemes = $container->get('appThemes'); |
||
| 84 | $this->action = $container->get('action'); |
||
| 85 | |||
| 86 | $this->appName = $container->get('settings')['appName']; |
||
| 87 | $this->appVersion = $container->get('settings')['appVersion']; |
||
| 88 | $this->postgresqlMinVer = $container->get('settings')['postgresqlMinVer']; |
||
| 89 | $this->phpMinVer = $container->get('settings')['phpMinVer']; |
||
| 90 | |||
| 91 | $msg = $container->get('msg'); |
||
| 92 | |||
| 93 | if (true === $this->no_db_connection) { |
||
| 94 | $this->misc->setNoDBConnection(true); |
||
| 95 | } |
||
| 96 | |||
| 97 | if (false === $this->misc->getNoDBConnection()) { |
||
| 98 | if (null === $this->misc->getServerId()) { |
||
| 99 | $servers_controller = new \PHPPgAdmin\Controller\ServersController($container); |
||
| 100 | |||
| 101 | return $servers_controller->render(); |
||
| 102 | } |
||
| 103 | $_server_info = $this->misc->getServerInfo(); |
||
| 104 | // Redirect to the login form if not logged in |
||
| 105 | if (!isset($_server_info['username'])) { |
||
| 106 | $msg = sprintf($this->lang['strlogoutmsg'], $_server_info['desc']); |
||
| 107 | |||
| 108 | $servers_controller = new \PHPPgAdmin\Controller\ServersController($container); |
||
| 109 | |||
| 110 | return $servers_controller->render(); |
||
| 111 | } |
||
| 112 | } |
||
| 113 | |||
| 114 | //\PC::debug(['name' => $this->controller_name, 'no_db_connection' => $this->misc->getNoDBConnection()], 'instanced controller'); |
||
| 115 | } |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Default method to render the controller according to the action parameter. |
||
| 119 | */ |
||
| 120 | public function render() |
||
| 121 | { |
||
| 122 | $this->misc = $this->misc; |
||
| 123 | |||
| 124 | $this->printHeader(); |
||
| 125 | $this->printBody(); |
||
| 126 | |||
| 127 | switch ($this->action) { |
||
| 128 | default: |
||
| 129 | $this->doDefault(); |
||
| 130 | |||
| 131 | break; |
||
| 132 | } |
||
| 133 | |||
| 134 | $this->printFooter(); |
||
| 135 | } |
||
| 136 | |||
| 137 | public function doDefault() |
||
| 143 | } |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Returns the page title for each controller |
||
| 147 | * |
||
| 148 | * @param string $title The title |
||
| 149 | * @param string $prefix The prefix |
||
| 150 | * @param string $suffix The suffix |
||
| 151 | * |
||
| 152 | * @return string the page title |
||
| 153 | */ |
||
| 154 | public function headerTitle($title = '', $prefix = '', $suffix = '') |
||
| 155 | { |
||
| 156 | $title = $title ? $title : $this->controller_title; |
||
| 157 | return $prefix . $this->lang[$title] . ($suffix ? ': ' . $suffix : ''); |
||
| 158 | } |
||
| 159 | |||
| 160 | public function getContainer() |
||
| 161 | { |
||
| 162 | return $this->container; |
||
| 163 | } |
||
| 164 | |||
| 165 | private function _getTableController() |
||
| 166 | { |
||
| 167 | if (null === $this->table_controller) { |
||
| 168 | $this->table_controller = new \PHPPgAdmin\XHtml\HTMLTableController($this->getContainer(), $this->controller_name); |
||
| 169 | } |
||
| 170 | |||
| 171 | return $this->table_controller; |
||
| 172 | } |
||
| 173 | |||
| 174 | private function _getFooterController() |
||
| 175 | { |
||
| 176 | if (null === $this->footer_controller) { |
||
| 177 | $this->footer_controller = new \PHPPgAdmin\XHtml\HTMLFooterController($this->getContainer(), $this->controller_name); |
||
| 178 | } |
||
| 179 | |||
| 180 | return $this->footer_controller; |
||
| 181 | } |
||
| 182 | |||
| 183 | private function _getHeaderController() |
||
| 184 | { |
||
| 185 | if (null === $this->header_controller) { |
||
| 186 | $this->header_controller = new \PHPPgAdmin\XHtml\HTMLHeaderController($this->getContainer(), $this->controller_name); |
||
| 187 | } |
||
| 188 | |||
| 189 | return $this->header_controller; |
||
| 190 | } |
||
| 191 | |||
| 192 | private function _getNavbarController() |
||
| 193 | { |
||
| 194 | if (null === $this->trail_controller) { |
||
| 195 | $this->trail_controller = new \PHPPgAdmin\XHtml\HTMLNavbarController($this->getContainer(), $this->controller_name); |
||
| 196 | } |
||
| 197 | |||
| 198 | return $this->trail_controller; |
||
| 199 | } |
||
| 200 | |||
| 201 | private function _getTreeController() |
||
| 202 | { |
||
| 203 | if (null === $this->tree_controller) { |
||
| 204 | $this->tree_controller = new \PHPPgAdmin\XHtml\TreeController($this->getContainer(), $this->controller_name); |
||
| 205 | } |
||
| 206 | |||
| 207 | return $this->tree_controller; |
||
| 208 | } |
||
| 209 | |||
| 210 | /** |
||
| 211 | * Display a table of data. |
||
| 212 | * |
||
| 213 | * @param \PHPPgAdmin\ADORecordSet|\PHPPgAdmin\ArrayRecordSet $tabledata a set of data to be formatted |
||
| 214 | * @param array $columns An associative array of columns to be displayed: |
||
| 215 | * @param array $actions Actions that can be performed on each object: |
||
| 216 | * @param string $place Place where the $actions are displayed. Like 'display-browse', |
||
| 217 | * @param string $nodata (optional) Message to display if data set is empty |
||
| 218 | * @param callable $pre_fn (optional) callback closure for each row |
||
| 219 | * |
||
| 220 | * @return string the html of the table |
||
| 221 | */ |
||
| 222 | public function printTable(&$tabledata, &$columns, &$actions, $place, $nodata = null, $pre_fn = null) |
||
| 223 | { |
||
| 224 | $html_table = $this->_getTableController(); |
||
| 225 | |||
| 226 | return $html_table->printTable($tabledata, $columns, $actions, $place, $nodata, $pre_fn); |
||
| 227 | } |
||
| 228 | |||
| 229 | public function adjustTabsForTree($tabs) |
||
| 230 | { |
||
| 231 | $tree = $this->_getTreeController(); |
||
| 232 | |||
| 233 | return $tree->adjustTabsForTree($tabs); |
||
| 234 | } |
||
| 235 | |||
| 236 | /** |
||
| 237 | * Produce JSON data for the browser tree. |
||
| 238 | * |
||
| 239 | * @param array $_treedata a set of records to populate the tree |
||
| 240 | * @param array $attrs Attributes for tree items |
||
| 241 | * @param string $section The section where the branch is linked in the tree |
||
| 242 | * @param bool $print either to return or echo the result |
||
| 243 | * |
||
| 244 | * @return \Slim\Http\Response|string the json rendered tree |
||
| 245 | */ |
||
| 246 | public function printTree(&$_treedata, &$attrs, $section, $print = true) |
||
| 247 | { |
||
| 248 | $tree = $this->_getTreeController(); |
||
| 249 | |||
| 250 | return $tree->printTree($_treedata, $attrs, $section, $print); |
||
| 251 | } |
||
| 252 | |||
| 253 | public function printTrail($trail = [], $do_print = true) |
||
| 259 | } |
||
| 260 | |||
| 261 | public function printNavLinks($navlinks, $place, $env = [], $do_print = true) |
||
| 262 | { |
||
| 263 | $from = __METHOD__; |
||
| 264 | $html_trail = $this->_getNavbarController(); |
||
| 265 | |||
| 266 | return $html_trail->printNavLinks($navlinks, $place, $env, $do_print, $from); |
||
| 267 | } |
||
| 268 | |||
| 269 | public function printTabs($tabs, $activetab, $do_print = true) |
||
| 270 | { |
||
| 271 | $from = __METHOD__; |
||
| 272 | $html_trail = $this->_getNavbarController(); |
||
| 273 | |||
| 274 | return $html_trail->printTabs($tabs, $activetab, $do_print, $from); |
||
| 275 | } |
||
| 276 | |||
| 277 | public function getLastTabURL($section) |
||
| 282 | } |
||
| 283 | |||
| 284 | public function printLink($link, $do_print = true, $from = null) |
||
| 285 | { |
||
| 286 | if (null === $from) { |
||
| 287 | $from = __METHOD__; |
||
| 288 | } |
||
| 289 | |||
| 290 | $html_trail = $this->_getNavbarController(); |
||
| 291 | |||
| 292 | return $html_trail->printLink($link, $do_print, $from); |
||
| 293 | } |
||
| 294 | |||
| 295 | public function setReloadDropDatabase($flag) |
||
| 296 | { |
||
| 297 | $footer_controller = $this->_getFooterController(); |
||
| 298 | |||
| 299 | return $footer_controller->setReloadDropDatabase($flag); |
||
| 300 | } |
||
| 301 | |||
| 302 | public function setNoBottomLink($flag) |
||
| 303 | { |
||
| 304 | $footer_controller = $this->_getFooterController(); |
||
| 305 | |||
| 306 | return $footer_controller->setNoBottomLink($flag); |
||
| 307 | } |
||
| 308 | |||
| 309 | public function printFooter($doBody = true, $template = 'footer.twig') |
||
| 310 | { |
||
| 311 | $footer_controller = $this->_getFooterController(); |
||
| 312 | |||
| 313 | return $footer_controller->printFooter($doBody, $template); |
||
| 314 | } |
||
| 315 | |||
| 316 | public function printReload($database, $do_print = true) |
||
| 317 | { |
||
| 318 | $footer_controller = $this->_getFooterController(); |
||
| 319 | |||
| 320 | return $footer_controller->printReload($database, $do_print); |
||
| 321 | } |
||
| 322 | |||
| 323 | /** |
||
| 324 | * Outputs JavaScript to set default focus. |
||
| 325 | * |
||
| 326 | * @param mixed $object eg. forms[0].username |
||
| 327 | */ |
||
| 328 | public function setFocus($object) |
||
| 329 | { |
||
| 330 | $footer_controller = $this->_getFooterController(); |
||
| 331 | |||
| 332 | return $footer_controller->setFocus($object); |
||
| 333 | } |
||
| 334 | |||
| 335 | /** |
||
| 336 | * Outputs JavaScript to set the name of the browser window. |
||
| 337 | * |
||
| 338 | * @param string $name the window name |
||
| 339 | * @param bool $addServer if true (default) then the server id is |
||
| 340 | * attached to the name |
||
| 341 | */ |
||
| 342 | public function setWindowName($name, $addServer = true) |
||
| 343 | { |
||
| 344 | $footer_controller = $this->_getFooterController(); |
||
| 345 | |||
| 346 | return $footer_controller->setWindowName($name, $addServer); |
||
| 347 | } |
||
| 348 | |||
| 349 | public function setNoOutput($flag) |
||
| 350 | { |
||
| 351 | $header_controller = $this->_getHeaderController(); |
||
| 352 | |||
| 353 | return $header_controller->setNoOutput((bool) $flag); |
||
| 354 | } |
||
| 355 | |||
| 356 | public function printHeader($title = '', $script = null, $do_print = true, $template = 'header.twig') |
||
| 362 | } |
||
| 363 | |||
| 364 | public function printBody($doBody = true, $bodyClass = 'detailbody', $onloadInit = false) |
||
| 365 | { |
||
| 366 | $header_controller = $this->_getHeaderController(); |
||
| 367 | |||
| 368 | return $header_controller->printBody($doBody, $bodyClass, $onloadInit); |
||
| 369 | } |
||
| 370 | |||
| 371 | public function printTitle($title, $help = null, $do_print = true) |
||
| 376 | } |
||
| 377 | |||
| 378 | public function getRequestParam($key, $default = null) |
||
| 379 | { |
||
| 380 | return $this->container->requestobj->getParam($key, $default); |
||
| 381 | } |
||
| 382 | |||
| 383 | public function getPostParam($key, $default = null) |
||
| 384 | { |
||
| 385 | return $this->container->requestobj->getParsedBodyParam($key, $default); |
||
| 386 | } |
||
| 387 | |||
| 388 | public function getQueryParam($key, $default = null) |
||
| 389 | { |
||
| 390 | return $this->container->requestobj->getQueryParam($key, $default); |
||
| 391 | } |
||
| 392 | |||
| 393 | /** |
||
| 394 | * Print out a message. |
||
| 395 | * |
||
| 396 | * @param string $msg The message |
||
| 397 | * @param bool $do_print if true, print the message. Return the string otherwise |
||
| 398 | * |
||
| 399 | * @return string a paragraph containing the message, whose linebreaks are replaced by <br> elements |
||
| 400 | */ |
||
| 401 | public function printMsg($msg, $do_print = true) |
||
| 415 | } |
||
| 416 | } |
||
| 417 |