Total Complexity | 41 |
Total Lines | 421 |
Duplicated Lines | 0 % |
Changes | 5 | ||
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 |
||
57 | class BaseController |
||
58 | { |
||
59 | use FirewallTrait; |
||
60 | use DemoModeTrait; |
||
61 | use ConfigMethodsTrait; |
||
62 | |||
63 | /** |
||
64 | * LogPaeser instance. |
||
65 | * |
||
66 | * @var object |
||
67 | */ |
||
68 | protected $parser; |
||
69 | |||
70 | /** |
||
71 | * Messages. |
||
72 | * |
||
73 | * @var array |
||
74 | */ |
||
75 | protected $messages = []; |
||
76 | |||
77 | /** |
||
78 | * Check page availability. |
||
79 | * |
||
80 | * @var array |
||
81 | */ |
||
82 | protected $pageAvailability = [ |
||
83 | |||
84 | // Need to implement Action Logger to make it true. |
||
85 | 'logs' => false, |
||
86 | ]; |
||
87 | |||
88 | /** |
||
89 | * See $this->csrf() |
||
90 | * |
||
91 | * @var array |
||
92 | */ |
||
93 | protected $csrfField = []; |
||
94 | |||
95 | /** |
||
96 | * Language code. |
||
97 | * |
||
98 | * @var string |
||
99 | */ |
||
100 | protected $locate = 'en'; |
||
101 | |||
102 | /** |
||
103 | * Captcha modules. |
||
104 | * |
||
105 | * @var Interface |
||
|
|||
106 | */ |
||
107 | protected $captcha = []; |
||
108 | |||
109 | /** |
||
110 | * The base URL of the firewall panel. |
||
111 | * |
||
112 | * @var string |
||
113 | */ |
||
114 | public $base = ''; |
||
115 | |||
116 | /** |
||
117 | * Firewall panel base controller. |
||
118 | */ |
||
119 | public function __construct() |
||
120 | { |
||
121 | $firewall = Container::get('firewall'); |
||
122 | |||
123 | if (!($firewall instanceof Firewall)) { |
||
124 | throw new RuntimeException( |
||
125 | 'The Firewall instance should be initialized first.' |
||
126 | ); |
||
127 | } |
||
128 | |||
129 | $this->mode = 'managed'; |
||
130 | $this->kernel = $firewall->getKernel(); |
||
131 | $this->configuration = $firewall->getConfiguration(); |
||
132 | $this->directory = $firewall->getDirectory(); |
||
133 | $this->filename = $firewall->getFilename(); |
||
134 | $this->base = SHIELDON_PANEL_BASE; |
||
135 | |||
136 | if (!empty($this->kernel->logger)) { |
||
137 | |||
138 | // We need to know where the logs stored in. |
||
139 | $logDirectory = $this->kernel->logger->getDirectory(); |
||
140 | |||
141 | // Load ActionLogParser for parsing log files. |
||
142 | $this->parser = new ActionLogParser($logDirectory); |
||
143 | |||
144 | $this->pageAvailability['logs'] = true; |
||
145 | } |
||
146 | |||
147 | $flashMessage = get_session()->get('flash_messages'); |
||
148 | |||
149 | // Flash message, use it when redirecting page. |
||
150 | if (!empty($flashMessage)) { |
||
151 | $this->messages = $flashMessage; |
||
152 | get_session()->remove('flash_messages'); |
||
153 | } |
||
154 | |||
155 | $this->locate = get_user_lang(); |
||
156 | } |
||
157 | |||
158 | /** |
||
159 | * Load view file. |
||
160 | * |
||
161 | * @param string $page The page type. (filename) |
||
162 | * @param array $data The variables passed to that page. |
||
163 | * |
||
164 | * @return string |
||
165 | */ |
||
166 | protected function loadView(string $page, array $data = []): string |
||
167 | { |
||
168 | if (!defined('SHIELDON_VIEW')) { |
||
169 | define('SHIELDON_VIEW', true); |
||
170 | } |
||
171 | |||
172 | $viewFilePath = __DIR__ . '/../../../templates/' . $page . '.php'; |
||
173 | |||
174 | if (!empty($data)) { |
||
175 | extract($data); |
||
176 | } |
||
177 | |||
178 | $output = ''; |
||
179 | |||
180 | if (file_exists($viewFilePath)) { |
||
181 | ob_start(); |
||
182 | include $viewFilePath; |
||
183 | $output = ob_get_contents(); |
||
184 | ob_end_clean(); |
||
185 | } |
||
186 | |||
187 | return $output; |
||
188 | } |
||
189 | |||
190 | /** |
||
191 | * Render the web page with full layout. |
||
192 | * |
||
193 | * @param string $page The page type. (filename) |
||
194 | * @param array $data The variables passed to that page. |
||
195 | * |
||
196 | * @return ResponseInterface |
||
197 | */ |
||
198 | protected function renderPage(string $page, array $data): ResponseInterface |
||
199 | { |
||
200 | $channelName = $this->kernel->driver->getChannel(); |
||
201 | $body = []; |
||
202 | |||
203 | if (empty($channelName)) { |
||
204 | $channelName = 'default'; |
||
205 | } |
||
206 | |||
207 | $body['channel_name'] = $channelName; |
||
208 | $body['mode_name'] = $this->mode; |
||
209 | $body['page_url'] = $this->url(); |
||
210 | $body['content'] = $this->loadView($page, $data); |
||
211 | $body['title'] = $data['title'] ?? ''; |
||
212 | |||
213 | $body['title'] .= ' - ' . __('panel', 'title_site_wide', 'Shieldon Firewall'); |
||
214 | $body['title'] .= ' v' . SHIELDON_FIREWALL_VERSION; |
||
215 | |||
216 | $page = $this->loadView('panel/template', $body); |
||
217 | |||
218 | return $this->respond($page); |
||
219 | } |
||
220 | |||
221 | /** |
||
222 | * Return the response instance. |
||
223 | * |
||
224 | * @param string $body The content body. |
||
225 | * |
||
226 | * @return ResponseInterface |
||
227 | */ |
||
228 | protected function respond(string $body): ResponseInterface |
||
236 | } |
||
237 | |||
238 | /** |
||
239 | * Include a view file. This |
||
240 | * This method is used in a template loading other templates. |
||
241 | * |
||
242 | * @param string $page The page type. (filename) |
||
243 | * @param array $data The variables passed to that page. |
||
244 | * |
||
245 | * @return void |
||
246 | */ |
||
247 | protected function loadViewPart(string $page, array $data = []): void |
||
248 | { |
||
249 | if (!defined('SHIELDON_VIEW')) { |
||
250 | define('SHIELDON_VIEW', true); |
||
251 | } |
||
252 | |||
253 | foreach ($data as $k => $v) { |
||
254 | ${$k} = $v; |
||
255 | } |
||
256 | |||
257 | include __DIR__ . '/../../../templates/' . $page . '.php'; |
||
258 | } |
||
259 | |||
260 | /** |
||
261 | * Response message to front. |
||
262 | * |
||
263 | * @param string $type The message status type. error|success |
||
264 | * @param string $text The message body. |
||
265 | * |
||
266 | * @return void |
||
267 | */ |
||
268 | protected function pushMessage(string $type, string $text): void |
||
269 | { |
||
270 | $class = $type; |
||
271 | |||
272 | if ($type == 'error') { |
||
273 | $class = 'danger'; |
||
274 | } |
||
275 | |||
276 | array_push( |
||
277 | $this->messages, |
||
278 | [ |
||
279 | 'type' => $type, |
||
280 | 'text' => $text, |
||
281 | 'class' => $class, |
||
282 | ] |
||
283 | ); |
||
284 | } |
||
285 | |||
286 | /** |
||
287 | * Return the relative URL. |
||
288 | * |
||
289 | * @param string $path The page's path. |
||
290 | * |
||
291 | * @return string |
||
292 | */ |
||
293 | protected function url(string $path = ''): string |
||
294 | { |
||
295 | return '/' . trim($this->base, '/') . '/' . $path . '/'; |
||
296 | } |
||
297 | |||
298 | /** |
||
299 | * Output HTML input element with CSRF token. |
||
300 | * |
||
301 | * @return void |
||
302 | */ |
||
303 | public function fieldCsrf(): void |
||
308 | } |
||
309 | } |
||
310 | } |
||
311 | |||
312 | /** |
||
313 | * Save the configuration settings to the JSON file. |
||
314 | * |
||
315 | * @return void |
||
316 | */ |
||
317 | protected function saveConfig(): void |
||
355 | ) |
||
356 | ); |
||
357 | } |
||
358 | } |
||
359 | |||
360 | /** |
||
361 | * Echo the setting string to the template. |
||
362 | * |
||
363 | * @param string $field Field. |
||
364 | * @param mixed $default Default value. |
||
365 | * |
||
366 | * @return void |
||
367 | */ |
||
368 | protected function _(string $field, $default = ''): void |
||
369 | { |
||
370 | if ($this->mode === 'demo') { |
||
371 | |||
372 | // Hide sensitive data because of security concerns. |
||
373 | $hiddenForDemo = [ |
||
374 | 'drivers.redis.auth', |
||
375 | 'drivers.file.directory_path', |
||
376 | 'drivers.sqlite.directory_path', |
||
377 | 'drivers.mysql.dbname', |
||
378 | 'drivers.mysql.user', |
||
379 | 'drivers.mysql.pass', |
||
380 | 'captcha_modules.recaptcha.config.site_key', |
||
381 | 'captcha_modules.recaptcha.config.secret_key', |
||
382 | 'loggers.action.config.directory_path', |
||
383 | 'admin.user', |
||
384 | 'admin.pass', |
||
385 | 'admin.last_modified', |
||
386 | 'messengers.telegram.config.api_key', |
||
387 | 'messengers.telegram.config.channel', |
||
388 | 'messengers.sendgrid.config.api_key', |
||
389 | 'messengers.sendgrid.config.sender', |
||
390 | 'messengers.sendgrid.config.recipients', |
||
391 | 'messengers.line_notify.config.access_token', |
||
392 | 'iptables.config.watching_folder', |
||
393 | 'ip6tables.config.watching_folder', |
||
394 | 'messengers.sendgrid.config.recipients', // array |
||
395 | ]; |
||
396 | |||
397 | if (in_array($field, $hiddenForDemo)) { |
||
398 | echo __('panel', 'field_not_visible', 'Cannot view this field in demo mode.'); |
||
399 | return; |
||
400 | } |
||
401 | } |
||
402 | |||
403 | $fieldtype = gettype($this->getConfig($field)); |
||
404 | |||
405 | if (in_array($fieldtype, ['integer', 'string', 'double'])) { |
||
406 | echo (!empty($this->getConfig($field))) ? $this->getConfig($field) : $default; |
||
407 | return; |
||
408 | |||
409 | } elseif (in_array($fieldtype, ['array'])) { |
||
410 | echo implode("\n", $this->getConfig($field)); |
||
411 | return; |
||
412 | } |
||
413 | |||
414 | echo ''; |
||
415 | } |
||
416 | |||
417 | /** |
||
418 | * Use on HTML checkbox and radio elements. |
||
419 | * |
||
420 | * @param string $value The variable or configuation field. |
||
421 | * @param mixed $valueChecked The value. |
||
422 | * @param bool $isConfig Is it a configuration field or not. |
||
423 | * |
||
424 | * @return void |
||
425 | */ |
||
426 | protected function checked(string $value, $valueChecked, bool $isConfig = true): void |
||
441 | } |
||
442 | |||
443 | /** |
||
444 | * Echo correspondence string on Messenger setting page. |
||
445 | * |
||
446 | * @param string $moduleName The messenger module's name. |
||
447 | * @param string $echoType Value: css | icon |
||
448 | * |
||
449 | * @return void |
||
450 | */ |
||
451 | protected function messengerAjaxStatus(string $moduleName, string $echoType = 'css'): void |
||
462 | } |
||
463 | |||
464 | /** |
||
465 | * Use on HTML select elemets. |
||
466 | * |
||
467 | * @param string $value The value. |
||
468 | * @param mixed $valueChecked The value to confirm. |
||
469 | * |
||
470 | * @return void |
||
471 | */ |
||
472 | protected function selected(string $value, $valueChecked): void |
||
481 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths