Total Complexity | 49 |
Total Lines | 558 |
Duplicated Lines | 0 % |
Changes | 2 | ||
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 |
||
25 | class BaseController |
||
26 | { |
||
27 | use HelperTrait; |
||
28 | |||
29 | public $appLangFiles = []; |
||
30 | |||
31 | public $appThemes = []; |
||
32 | |||
33 | public $appName = ''; |
||
34 | |||
35 | public $appVersion = ''; |
||
36 | |||
37 | public $form = ''; |
||
38 | |||
39 | public $href = ''; |
||
40 | |||
41 | public $lang = []; |
||
42 | |||
43 | public $action = ''; |
||
44 | |||
45 | public $controller_name; |
||
46 | |||
47 | /** |
||
48 | * Used. |
||
49 | * |
||
50 | * @var string |
||
51 | */ |
||
52 | public $view_name; |
||
53 | |||
54 | /** |
||
55 | * Used to print the title passing its value to $lang. |
||
56 | * |
||
57 | * @var string |
||
58 | */ |
||
59 | public $controller_title = 'base'; |
||
60 | |||
61 | public $msg = ''; |
||
62 | |||
63 | /** |
||
64 | * @var ViewManager |
||
65 | */ |
||
66 | public $view; |
||
67 | |||
68 | /** |
||
69 | * @var Misc |
||
70 | */ |
||
71 | public $misc; |
||
72 | |||
73 | public $conf; |
||
74 | |||
75 | public $phpMinVer; |
||
76 | |||
77 | /** |
||
78 | * @var ContainerUtils |
||
79 | */ |
||
80 | protected $container; |
||
81 | |||
82 | protected $script; |
||
83 | |||
84 | protected $data; |
||
85 | |||
86 | protected $database; |
||
87 | |||
88 | protected $server_id; |
||
89 | |||
90 | /** |
||
91 | * @var XHtml\HTMLTableController |
||
92 | * @psalm-suppress PropertyNotSetInConstructor |
||
93 | */ |
||
94 | protected $_table_controller; |
||
95 | |||
96 | /** |
||
97 | * @var XHtml\HTMLFooterController |
||
98 | * @psalm-suppress PropertyNotSetInConstructor |
||
99 | */ |
||
100 | protected $_footer_controller; |
||
101 | |||
102 | /** |
||
103 | * @var XHtml\HTMLHeaderController |
||
104 | * @psalm-suppress PropertyNotSetInConstructor |
||
105 | */ |
||
106 | protected $_header_controller; |
||
107 | |||
108 | /** |
||
109 | * @var XHtml\HTMLNavbarController |
||
110 | * @psalm-suppress PropertyNotSetInConstructor |
||
111 | */ |
||
112 | protected $_trail_controller; |
||
113 | |||
114 | /** |
||
115 | * @var TreeController |
||
116 | * @psalm-suppress PropertyNotSetInConstructor |
||
117 | */ |
||
118 | protected $_tree_controller; |
||
119 | |||
120 | protected $scripts = ''; |
||
121 | |||
122 | protected $no_db_connection = false; |
||
123 | |||
124 | /** |
||
125 | * Constructs the base controller (common for almost all controllers). |
||
126 | * |
||
127 | * @param ContainerUtils $container the $app container |
||
128 | * @param bool $no_db_connection [optional] if true, sets $this->misc->setNoDBConnection(true); |
||
129 | */ |
||
130 | public function __construct(ContainerUtils $container) |
||
131 | { |
||
132 | $this->container = $container; |
||
133 | $this->lang = $container->get('lang'); |
||
134 | |||
135 | $this->controller_name = \str_replace(__NAMESPACE__ . '\\', '', \get_class($this)); |
||
136 | $this->view_name = \str_replace('controller', '', \mb_strtolower($this->controller_name)); |
||
137 | $this->script = $this->view_name; |
||
138 | |||
139 | $this->view = $container->get('view'); |
||
140 | |||
141 | $this->msg = $container->get('msg'); |
||
142 | $this->appLangFiles = $container->get('appLangFiles'); |
||
143 | |||
144 | $this->misc = $container->get('misc'); |
||
145 | $this->conf = $this->misc->getConf(); |
||
146 | |||
147 | $this->appThemes = $container->get('appThemes'); |
||
148 | $this->action = $container->get('action'); |
||
149 | |||
150 | $this->appName = $container->get('settings')['appName']; |
||
151 | $this->appVersion = $container->get('settings')['appVersion']; |
||
152 | $this->postgresqlMinVer = $container->get('settings')['postgresqlMinVer']; |
||
153 | $this->phpMinVer = $container->get('settings')['phpMinVer']; |
||
154 | |||
155 | $msg = $container->get('msg'); |
||
156 | |||
157 | if (true === $this->no_db_connection) { |
||
158 | $this->misc->setNoDBConnection(true); |
||
159 | } |
||
160 | |||
161 | if (!$this->container->IN_TEST) { |
||
162 | $this->renderInitialPageIfNotLogged(); |
||
163 | } |
||
164 | } |
||
165 | |||
166 | /** |
||
167 | * Default method to render the controller according to the action parameter. It should return with a PSR |
||
168 | * responseObject but it prints texts whatsoeever. |
||
169 | * |
||
170 | * @return void |
||
171 | */ |
||
172 | public function render() |
||
173 | { |
||
174 | $this->misc = $this->misc; |
||
175 | |||
176 | $this->printHeader(); |
||
177 | $this->printBody(); |
||
178 | |||
179 | switch ($this->action) { |
||
180 | default: |
||
181 | $this->doDefault(); |
||
182 | |||
183 | break; |
||
184 | } |
||
185 | |||
186 | $this->printFooter(); |
||
187 | } |
||
188 | |||
189 | /** |
||
190 | * @return string |
||
191 | */ |
||
192 | public function doDefault() |
||
198 | } |
||
199 | |||
200 | /** |
||
201 | * Returns the page title for each controller. |
||
202 | * |
||
203 | * @param string $title The title |
||
204 | * @param string $prefix The prefix |
||
205 | * @param string $suffix The suffix |
||
206 | * |
||
207 | * @return string the page title |
||
208 | */ |
||
209 | public function headerTitle($title = '', $prefix = '', $suffix = '') |
||
214 | } |
||
215 | |||
216 | /** |
||
217 | * @return ContainerUtils |
||
218 | */ |
||
219 | public function getContainer() |
||
220 | { |
||
221 | return $this->container; |
||
222 | } |
||
223 | |||
224 | /** |
||
225 | * Display a table of data. |
||
226 | * |
||
227 | * @param ADORecordSet|ArrayRecordSet $tabledata a set of data to be formatted |
||
228 | * @param array $columns An associative array of columns to be displayed: |
||
229 | * @param array $actions Actions that can be performed on each object: |
||
230 | * @param string $place Place where the $actions are displayed. Like 'display-browse', |
||
231 | * @param string $nodata (optional) Message to display if data set is empty |
||
232 | * @param callable $pre_fn (optional) callback closure for each row |
||
233 | * |
||
234 | * @return string the html of the table |
||
235 | */ |
||
236 | public function printTable(&$tabledata, &$columns, &$actions, $place, $nodata = '', $pre_fn = null) |
||
237 | { |
||
238 | $html_table = $this->_getTableController(); |
||
239 | |||
240 | $html_table->initialize($tabledata, $columns, $actions, $place, $nodata, $pre_fn); |
||
241 | |||
242 | return $html_table->printTable(); |
||
243 | } |
||
244 | |||
245 | /** |
||
246 | * Hides or show tree tabs according to their properties. |
||
247 | * |
||
248 | * @param array $tabs The tabs |
||
249 | * |
||
250 | * @return ADORecordSet|ArrayRecordSet filtered tabs in the form of an ArrayRecordSet |
||
251 | */ |
||
252 | public function adjustTabsForTree(&$tabs) |
||
253 | { |
||
254 | $tree = $this->_getTreeController(); |
||
255 | |||
256 | return $tree->adjustTabsForTree($tabs); |
||
257 | } |
||
258 | |||
259 | /** |
||
260 | * Produce JSON data for the browser tree. |
||
261 | * |
||
262 | * @param ADORecordSet|ArrayRecordSet $_treedata a set of records to populate the tree |
||
263 | * @param array $attrs Attributes for tree items |
||
264 | * @param string $section The section where the branch is linked in the tree |
||
265 | * @param bool $print either to return or echo the result |
||
266 | * |
||
267 | * @return Response|string the json rendered tree |
||
268 | */ |
||
269 | public function printTree(&$_treedata, &$attrs, $section, $print = true) |
||
270 | { |
||
271 | $tree = $this->_getTreeController(); |
||
272 | |||
273 | return $tree->printTree($_treedata, $attrs, $section, $print); |
||
274 | } |
||
275 | |||
276 | /** |
||
277 | * Prints a trail. |
||
278 | * |
||
279 | * @param array|string $trail |
||
280 | * @param bool $do_print The do print |
||
281 | * |
||
282 | * @return string ( description_of_the_return_value ) |
||
283 | */ |
||
284 | public function printTrail($trail = [], bool $do_print = true) |
||
290 | } |
||
291 | |||
292 | /** |
||
293 | * @param (array[][]|mixed)[][] $navlinks |
||
294 | * @param string $place |
||
295 | * @param array $env |
||
296 | * @param mixed $do_print |
||
297 | */ |
||
298 | public function printNavLinks(array $navlinks, string $place, array $env = [], $do_print = true) |
||
299 | { |
||
300 | $from = __METHOD__; |
||
301 | $footer_controller = $this->_getFooterController(); |
||
302 | |||
303 | return $footer_controller->printNavLinks($navlinks, $place, $env, $do_print, $from); |
||
304 | } |
||
305 | |||
306 | public function printTabs(string $tabs, string $activetab, bool $do_print = true) |
||
307 | { |
||
308 | $from = __METHOD__; |
||
309 | $html_trail = $this->_getNavbarController(); |
||
310 | |||
311 | return $html_trail->printTabs($tabs, $activetab, $do_print, $from); |
||
312 | } |
||
313 | |||
314 | /** |
||
315 | * @param true $do_print |
||
316 | * @param null|string $from |
||
317 | * @param mixed $link |
||
318 | */ |
||
319 | public function printLink($link, bool $do_print = true, ?string $from = null) |
||
320 | { |
||
321 | if (null === $from) { |
||
322 | $from = __METHOD__; |
||
323 | } |
||
324 | |||
325 | $html_trail = $this->_getNavbarController(); |
||
326 | |||
327 | return $html_trail->printLink($link, $do_print, $from); |
||
328 | } |
||
329 | |||
330 | /** |
||
331 | * @param true $flag |
||
332 | */ |
||
333 | public function setReloadDropDatabase(bool $flag) |
||
334 | { |
||
335 | $footer_controller = $this->_getFooterController(); |
||
336 | |||
337 | return $footer_controller->setReloadDropDatabase($flag); |
||
338 | } |
||
339 | |||
340 | /** |
||
341 | * @param true $flag |
||
342 | */ |
||
343 | public function setNoBottomLink(bool $flag) |
||
344 | { |
||
345 | $footer_controller = $this->_getFooterController(); |
||
346 | |||
347 | return $footer_controller->setNoBottomLink($flag); |
||
348 | } |
||
349 | |||
350 | public function printFooter(bool $doBody = true, string $template = 'footer.twig') |
||
351 | { |
||
352 | $footer_controller = $this->_getFooterController(); |
||
353 | |||
354 | return $footer_controller->printFooter($doBody, $template); |
||
355 | } |
||
356 | |||
357 | public function printReload($database, $do_print = true) |
||
358 | { |
||
359 | $footer_controller = $this->_getFooterController(); |
||
360 | |||
361 | return $footer_controller->printReload($database, $do_print); |
||
362 | } |
||
363 | |||
364 | /** |
||
365 | * Outputs JavaScript to set default focus. |
||
366 | * |
||
367 | * @param mixed $object eg. forms[0].username |
||
368 | */ |
||
369 | public function setFocus($object) |
||
370 | { |
||
371 | $footer_controller = $this->_getFooterController(); |
||
372 | |||
373 | return $footer_controller->setFocus($object); |
||
374 | } |
||
375 | |||
376 | /** |
||
377 | * Outputs JavaScript to set the name of the browser window. |
||
378 | * |
||
379 | * @param string $name the window name |
||
380 | * @param bool $addServer if true (default) then the server id is |
||
381 | * attached to the name |
||
382 | */ |
||
383 | public function setWindowName($name, $addServer = true) |
||
384 | { |
||
385 | $footer_controller = $this->_getFooterController(); |
||
386 | |||
387 | return $footer_controller->setWindowName($name, $addServer); |
||
388 | } |
||
389 | |||
390 | /** |
||
391 | * @param true $flag |
||
392 | */ |
||
393 | public function setNoOutput(bool $flag) |
||
394 | { |
||
395 | $header_controller = $this->_getHeaderController(); |
||
396 | |||
397 | return $header_controller->setNoOutput((bool) $flag); |
||
398 | } |
||
399 | |||
400 | /** |
||
401 | * @param null|string $script |
||
402 | * @param string $title |
||
403 | * @param bool $do_print |
||
404 | * @param string $template |
||
405 | */ |
||
406 | public function printHeader(string $title = '', ?string $script = null, bool $do_print = true, string $template = 'header.twig') |
||
407 | { |
||
408 | $title = $title ? $title : $this->headerTitle(); |
||
409 | $header_controller = $this->_getHeaderController(); |
||
410 | |||
411 | return $header_controller->printHeader($title, $script, $do_print, $template); |
||
412 | } |
||
413 | |||
414 | public function printBody(bool $doBody = true, string $bodyClass = 'detailbody', bool $onloadInit = false) |
||
415 | { |
||
416 | $header_controller = $this->_getHeaderController(); |
||
417 | |||
418 | return $header_controller->printBody($doBody, $bodyClass, $onloadInit); |
||
419 | } |
||
420 | |||
421 | /** |
||
422 | * @param null|string $help |
||
423 | * @param string $title |
||
424 | * @param bool $do_print |
||
425 | */ |
||
426 | public function printTitle(string $title, ?string $help = null, bool $do_print = true) |
||
427 | { |
||
428 | $header_controller = $this->_getHeaderController(); |
||
429 | |||
430 | return $header_controller->printTitle($title, $help, $do_print); |
||
431 | } |
||
432 | |||
433 | /** |
||
434 | * @param string $key |
||
435 | * @param null|string $default |
||
436 | */ |
||
437 | public function getRequestParam(string $key, ?string $default = null) |
||
438 | { |
||
439 | return \requestInstance()->getParam($key, $default); |
||
440 | } |
||
441 | |||
442 | /** |
||
443 | * @param string $key |
||
444 | * @param null|array|bool|float|int|string $default |
||
445 | * |
||
446 | * @return bool| null|array|string|int|float |
||
447 | */ |
||
448 | public function getPostParam(string $key, $default = null) |
||
449 | { |
||
450 | return \requestInstance()->getParsedBodyParam($key, $default); |
||
451 | } |
||
452 | |||
453 | /** |
||
454 | * @param string $key |
||
455 | * @param null|array|float|int|string $default |
||
456 | * |
||
457 | * @return null|array|float|int|string |
||
458 | */ |
||
459 | public function getQueryStrinParam($key, $default = null) |
||
460 | { |
||
461 | return \requestInstance()->getQueryParam($key, $default); |
||
462 | } |
||
463 | |||
464 | /** |
||
465 | * @return array |
||
466 | */ |
||
467 | public function getAllParams(): array |
||
468 | { |
||
469 | return \array_merge( |
||
470 | \requestInstance()->getQueryParams() ?? [], |
||
471 | \requestInstance()->getParsedBody() ?? [] |
||
472 | ); |
||
473 | } |
||
474 | |||
475 | /** |
||
476 | * Print out a message. |
||
477 | * |
||
478 | * @param string $msg The message |
||
479 | * @param bool $do_print if true, print the message. Return the string otherwise |
||
480 | * |
||
481 | * @return string a paragraph containing the message, whose linebreaks are replaced by <br> elements |
||
482 | */ |
||
483 | public function printMsg($msg, $do_print = true) |
||
484 | { |
||
485 | $html = ''; |
||
486 | $msg = \htmlspecialchars(ContainerUtils::br2ln($msg)); |
||
487 | |||
488 | if ('' !== $msg) { |
||
489 | $html .= '<p class="message">' . \nl2br($msg) . '</p>' . \PHP_EOL; |
||
490 | } |
||
491 | |||
492 | if ($do_print) { |
||
493 | echo $html; |
||
494 | |||
495 | return $html; |
||
496 | } |
||
497 | |||
498 | return $html; |
||
499 | } |
||
500 | |||
501 | private function renderInitialPageIfNotLogged(): void |
||
502 | { |
||
503 | if (false === $this->misc->getNoDBConnection()) { |
||
504 | if (null === $this->misc->getServerId()) { |
||
505 | $servers_controller = new ServersController($this->container); |
||
506 | |||
507 | $servers_controller->render(); |
||
508 | } else { |
||
509 | $_server_info = $this->misc->getServerInfo(); |
||
510 | // Redirect to the login form if not logged in |
||
511 | if (!isset($_server_info['username'])) { |
||
512 | $msg = \sprintf( |
||
513 | $this->lang['strlogoutmsg'], |
||
514 | $_server_info['desc'] |
||
515 | ); |
||
516 | |||
517 | $servers_controller = new ServersController($this->container); |
||
518 | |||
519 | $servers_controller->render(); |
||
520 | } |
||
521 | } |
||
522 | } |
||
523 | } |
||
524 | |||
525 | /** |
||
526 | * @return HTMLTableController |
||
527 | */ |
||
528 | private function _getTableController() |
||
529 | { |
||
530 | if (null === $this->_table_controller) { |
||
531 | $this->_table_controller = new HTMLTableController($this->getContainer(), $this->controller_name); |
||
532 | } |
||
533 | |||
534 | return $this->_table_controller; |
||
535 | } |
||
536 | |||
537 | /** |
||
538 | * @return HTMLFooterController |
||
539 | */ |
||
540 | private function _getFooterController() |
||
541 | { |
||
542 | if (null === $this->_footer_controller) { |
||
543 | $this->_footer_controller = new HTMLFooterController($this->getContainer(), $this->controller_name); |
||
544 | } |
||
545 | |||
546 | return $this->_footer_controller; |
||
547 | } |
||
548 | |||
549 | /** |
||
550 | * @return HTMLHeaderController |
||
551 | */ |
||
552 | private function _getHeaderController() |
||
559 | } |
||
560 | |||
561 | /** |
||
562 | * @return HTMLNavbarController |
||
563 | */ |
||
564 | private function _getNavbarController() |
||
565 | { |
||
566 | if (null === $this->_trail_controller) { |
||
567 | $this->_trail_controller = new HTMLNavbarController($this->getContainer(), $this->controller_name); |
||
568 | } |
||
569 | |||
570 | return $this->_trail_controller; |
||
571 | } |
||
572 | |||
573 | /** |
||
574 | * @return TreeController |
||
575 | */ |
||
576 | private function _getTreeController() |
||
583 | } |
||
584 | } |
||
585 |