| Total Complexity | 59 |
| Total Lines | 411 |
| 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 |
||
| 47 | class BaseController extends Controller |
||
| 48 | { |
||
| 49 | protected string $actionName; |
||
| 50 | protected string $controllerName; |
||
| 51 | protected string $controllerClass; |
||
| 52 | protected string $controllerNameUnCamelized; |
||
| 53 | protected bool $isExternalModuleController; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Initializes base class |
||
| 57 | */ |
||
| 58 | public function initialize(): void |
||
| 59 | { |
||
| 60 | $this->actionName = $this->dispatcher->getActionName(); |
||
| 61 | /** @scrutinizer ignore-call */ |
||
| 62 | $this->controllerClass = $this->dispatcher->getHandlerClass(); |
||
| 63 | $this->controllerName = Text::camelize($this->dispatcher->getControllerName(), '_'); |
||
| 64 | $this->controllerNameUnCamelized = Text::uncamelize($this->controllerName, '-'); |
||
| 65 | $this->isExternalModuleController = str_starts_with($this->dispatcher->getNamespaceName(), 'Modules'); |
||
| 66 | |||
| 67 | if ($this->request->isAjax() === false) { |
||
| 68 | $this->prepareView(); |
||
| 69 | } |
||
| 70 | } |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Prepares the view by setting the necessary variables and configurations. |
||
| 74 | * |
||
| 75 | * @return void |
||
| 76 | */ |
||
| 77 | protected function prepareView(): void |
||
| 78 | { |
||
| 79 | // Set the default timezone based on PBX settings |
||
| 80 | date_default_timezone_set(PbxSettings::getValueByKey(PbxSettings::PBX_TIMEZONE)); |
||
| 81 | |||
| 82 | // Set PBXLicense view variable if session exists |
||
| 83 | if ($this->session->has(SessionController::SESSION_ID)) { |
||
| 84 | $this->view->PBXLicense = PbxSettings::getValueByKey(PbxSettings::PBX_LICENSE); |
||
| 85 | } else { |
||
| 86 | $this->view->PBXLicense = ''; |
||
| 87 | } |
||
| 88 | |||
| 89 | // Set URLs for Wiki and Support based on language |
||
| 90 | $this->view->urlToWiki = "https://wiki.mikopbx.com/{$this->controllerNameUnCamelized}"; |
||
| 91 | if ($this->language === 'ru') { |
||
| 92 | $this->view->urlToSupport = 'https://www.mikopbx.ru/support/?fromPBX=true'; |
||
| 93 | } else { |
||
| 94 | $this->view->urlToSupport = 'https://www.mikopbx.com/support/?fromPBX=true'; |
||
| 95 | } |
||
| 96 | |||
| 97 | // Set the title based on the current action |
||
| 98 | $title = 'MikoPBX'; |
||
| 99 | switch ($this->actionName) { |
||
| 100 | case 'index': |
||
| 101 | case 'delete': |
||
| 102 | case 'save': |
||
| 103 | case 'modify': |
||
| 104 | case '*** WITHOUT ACTION ***': |
||
| 105 | $title .= '|' . $this->translation->_("Breadcrumb{$this->controllerName}"); |
||
| 106 | break; |
||
| 107 | default: |
||
| 108 | $title .= '|' . $this->translation->_("Breadcrumb{$this->controllerName}{$this->actionName} "); |
||
| 109 | } |
||
| 110 | Tag::setTitle($title); |
||
| 111 | |||
| 112 | // Set other view variables |
||
| 113 | $this->view->t = $this->translation; |
||
| 114 | $this->view->debugMode = $this->config->path('adminApplication.debugMode'); |
||
| 115 | $this->view->urlToLogo = $this->url->get('assets/img/logo-mikopbx.svg'); |
||
| 116 | $this->view->urlToController = $this->url->get($this->controllerNameUnCamelized); |
||
| 117 | $this->view->represent = ''; |
||
| 118 | $this->view->WebAdminLanguage = $this->session->get(PbxSettings::WEB_ADMIN_LANGUAGE)??PbxSettings::getValueByKey(PbxSettings::WEB_ADMIN_LANGUAGE); |
||
| 119 | $this->view->AvailableLanguages = json_encode(LanguageController::getAvailableWebAdminLanguages()); |
||
| 120 | $this->view->submitMode = $this->session->get('SubmitMode') ?? 'SaveSettings'; |
||
| 121 | $this->view->lastSentryEventId = $this->setLastSentryEventId(); |
||
| 122 | $this->view->PBXVersion = PbxSettings::getValueByKey(PbxSettings::PBX_VERSION); |
||
| 123 | $this->view->PBXName = PbxSettings::getValueByKey(PbxSettings::PBX_NAME); |
||
| 124 | $this->view->MetaTegHeadDescription = $this->translation->_('MetaTegHeadDescription'); |
||
| 125 | $this->view->isExternalModuleController = $this->isExternalModuleController; |
||
| 126 | if ($this->controllerClass!==SessionController::class) { |
||
| 127 | $this->view->setTemplateAfter('main'); |
||
| 128 | } |
||
| 129 | |||
| 130 | $this->view->globalModuleUniqueId = ''; |
||
| 131 | $this->view->actionName = $this->actionName; |
||
| 132 | $this->view->controllerName = $this->controllerName; |
||
| 133 | $this->view->controllerClass = $this->controllerClass; |
||
| 134 | $this->view->currentPage = "AdminCabinet/$this->controllerName/$this->actionName"; |
||
| 135 | |||
| 136 | // Add module variables into view if it is an external module controller |
||
| 137 | if ($this->isExternalModuleController) { |
||
| 138 | /** @var PbxExtensionModules $module */ |
||
| 139 | $module = PbxExtensionModules::findFirstByUniqid($this->getModuleUniqueId()); |
||
| 140 | if ($module === null) { |
||
| 141 | $module = new PbxExtensionModules(); |
||
| 142 | $module->disabled = '1'; |
||
| 143 | $module->name = 'Unknown module'; |
||
| 144 | } |
||
| 145 | $this->view->module = $module->toArray(); |
||
| 146 | $this->view->globalModuleUniqueId = $module->uniqid; |
||
| 147 | $this->view->showModuleStatusToggle = $this->showModuleStatusToggle??true; |
||
| 148 | $this->view->currentPage = "$module->uniqid/$this->controllerName/$this->actionName"; |
||
| 149 | } |
||
| 150 | } |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Performs actions before executing the route. |
||
| 154 | * |
||
| 155 | * @param Dispatcher $dispatcher |
||
| 156 | * @return void |
||
| 157 | */ |
||
| 158 | public function beforeExecuteRoute(Dispatcher $dispatcher): void |
||
| 159 | { |
||
| 160 | // Check if the request method is POST |
||
| 161 | if ($this->request->isPost()) { |
||
| 162 | // Retrieve the 'submitMode' data from the request |
||
| 163 | $data = $this->request->getPost('submitMode'); |
||
| 164 | if (!empty($data)) { |
||
| 165 | // Set the 'SubmitMode' session variable to the retrieved data |
||
| 166 | $this->session->set('SubmitMode', $data); |
||
| 167 | } |
||
| 168 | } |
||
| 169 | |||
| 170 | $this->actionName = $this->dispatcher->getActionName(); |
||
| 171 | $this->controllerName = Text::camelize($this->dispatcher->getControllerName(), '_'); |
||
| 172 | // Add module variables into view if it is an external module controller |
||
| 173 | if (str_starts_with($this->dispatcher->getNamespaceName(), 'Modules')) { |
||
| 174 | $this->view->pick("Modules/{$this->getModuleUniqueId()}/$this->controllerName/$this->actionName"); |
||
| 175 | } else { |
||
| 176 | $this->view->pick("$this->controllerName/$this->actionName"); |
||
| 177 | } |
||
| 178 | |||
| 179 | PBXConfModulesProvider::hookModulesMethod(WebUIConfigInterface::ON_BEFORE_EXECUTE_ROUTE, [$dispatcher]); |
||
| 180 | } |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Performs actions after executing the route and returns the response. |
||
| 184 | * |
||
| 185 | * @param Dispatcher $dispatcher |
||
| 186 | * @return ResponseInterface |
||
| 187 | * @throws Exception |
||
| 188 | */ |
||
| 189 | public function afterExecuteRoute(Dispatcher $dispatcher): ResponseInterface |
||
| 190 | { |
||
| 191 | |||
| 192 | if ($this->request->isAjax() === true) { |
||
| 193 | $this->view->setRenderLevel(View::LEVEL_NO_RENDER); |
||
| 194 | $this->response->setContentType('application/json', 'UTF-8'); |
||
| 195 | $data = $this->view->getParamsToView(); |
||
| 196 | |||
| 197 | /* Set global params if is not set in controller/action */ |
||
| 198 | if (isset($data['raw_response'])) { |
||
| 199 | $result = $data['raw_response']; |
||
| 200 | } else { |
||
| 201 | $data['success'] = $data['success'] ?? true; |
||
| 202 | $data['reload'] = $data['reload'] ?? false; |
||
| 203 | $data['message'] = $data['message'] ?? $this->flash->getMessages(); |
||
| 204 | |||
| 205 | // Let's add information about the last error to display a dialog window for the user. |
||
| 206 | $sentry = $this->di->get(SentryErrorHandlerProvider::SERVICE_NAME, ['admin-cabinet']); |
||
| 207 | if ($sentry) { |
||
| 208 | $data['lastSentryEventId'] = $sentry->getLastEventId(); |
||
| 209 | } |
||
| 210 | |||
| 211 | $result = json_encode($data); |
||
| 212 | } |
||
| 213 | $this->response->setContent($result); |
||
| 214 | } |
||
| 215 | |||
| 216 | PBXConfModulesProvider::hookModulesMethod(WebUIConfigInterface::ON_AFTER_EXECUTE_ROUTE, [$dispatcher]); |
||
| 217 | |||
| 218 | return $this->response->send(); |
||
| 219 | } |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Forwards the request to a different controller and action based on the provided URI. |
||
| 223 | * |
||
| 224 | * @param string $uri The URI to forward to. |
||
| 225 | * @return void |
||
| 226 | */ |
||
| 227 | protected function forward(string $uri): void |
||
| 228 | { |
||
| 229 | $uriParts = explode('/', $uri); |
||
| 230 | if ($this->isExternalModuleController and count($uriParts)>2) { |
||
| 231 | $params = array_slice($uriParts, 3); |
||
| 232 | $moduleUniqueID = $this->getModuleUniqueId(); |
||
| 233 | $this->dispatcher->forward( |
||
| 234 | [ |
||
| 235 | 'namespace'=>"Modules\\$moduleUniqueID\\App\\Controllers", |
||
| 236 | 'controller' => $uriParts[1], |
||
| 237 | 'action' => $uriParts[2], |
||
| 238 | 'params' => $params, |
||
| 239 | ] |
||
| 240 | ); |
||
| 241 | } else { |
||
| 242 | $params = array_slice($uriParts, 2); |
||
| 243 | |||
| 244 | $this->dispatcher->forward( |
||
| 245 | [ |
||
| 246 | 'namespace'=>"MikoPBX\AdminCabinet\Controllers", |
||
| 247 | 'controller' => $uriParts[0], |
||
| 248 | 'action' => $uriParts[1], |
||
| 249 | 'params' => $params, |
||
| 250 | ] |
||
| 251 | ); |
||
| 252 | } |
||
| 253 | } |
||
| 254 | |||
| 255 | /** |
||
| 256 | * Sanitizes the caller ID by removing any characters that are not alphanumeric or spaces. |
||
| 257 | * |
||
| 258 | * @param string $callerId The caller ID to sanitize. |
||
| 259 | * @return string The sanitized caller ID. |
||
| 260 | */ |
||
| 261 | protected function sanitizeCallerId(string $callerId): string |
||
| 264 | } |
||
| 265 | |||
| 266 | /** |
||
| 267 | * Sorts array by priority field |
||
| 268 | * |
||
| 269 | * @param $a |
||
| 270 | * @param $b |
||
| 271 | * |
||
| 272 | * @return int|null |
||
| 273 | */ |
||
| 274 | protected function sortArrayByPriority($a, $b): ?int |
||
| 275 | { |
||
| 276 | if (is_array($a)) { |
||
| 277 | $a = (int)$a['priority']; |
||
| 278 | } else { |
||
| 279 | $a = (int)$a->priority; |
||
| 280 | } |
||
| 281 | |||
| 282 | if (is_array($b)) { |
||
| 283 | $b = (int)$b['priority']; |
||
| 284 | } else { |
||
| 285 | $b = (int)$b->priority; |
||
| 286 | } |
||
| 287 | |||
| 288 | if ($a === $b) { |
||
| 289 | return 0; |
||
| 290 | } else { |
||
| 291 | return ($a < $b) ? -1 : 1; |
||
| 292 | } |
||
| 293 | } |
||
| 294 | |||
| 295 | /** |
||
| 296 | * Sets the last Sentry event ID. |
||
| 297 | * |
||
| 298 | * @return \Sentry\EventId|null The last Sentry event ID, or null if metrics sending is disabled. |
||
| 299 | */ |
||
| 300 | private function setLastSentryEventId(): ?\Sentry\EventId |
||
| 301 | { |
||
| 302 | $result = null; |
||
| 303 | // Allow anonymous statistics collection for JS code |
||
| 304 | $sentry = $this->di->get(SentryErrorHandlerProvider::SERVICE_NAME); |
||
| 305 | if ($sentry) { |
||
| 306 | $result = $sentry->getLastEventId(); |
||
| 307 | } |
||
| 308 | return $result; |
||
| 309 | } |
||
| 310 | |||
| 311 | /** |
||
| 312 | * Returns the unique ID of the module parsing controller namespace; |
||
| 313 | * @return string |
||
| 314 | */ |
||
| 315 | private function getModuleUniqueId():string |
||
| 316 | { |
||
| 317 | // Split the namespace into an array using the backslash as a separator |
||
| 318 | $parts = explode('\\', get_class($this)); |
||
| 319 | |||
| 320 | // Get the second part of the namespace |
||
| 321 | return $parts[1]; |
||
| 322 | } |
||
| 323 | |||
| 324 | /** |
||
| 325 | * Save an entity and handle success or error messages. |
||
| 326 | * |
||
| 327 | * @param mixed $entity The entity to be saved. |
||
| 328 | * @return bool True if the entity was successfully saved, false otherwise. |
||
| 329 | */ |
||
| 330 | protected function saveEntity(mixed $entity, string $reloadPath = ''): bool |
||
| 352 | } |
||
| 353 | |||
| 354 | |||
| 355 | /** |
||
| 356 | * Delete an entity and handle success or error messages. |
||
| 357 | * |
||
| 358 | * @param mixed $entity The entity to be deleted. |
||
| 359 | * @return bool True if the entity was successfully deleted, false otherwise. |
||
| 360 | */ |
||
| 361 | protected function deleteEntity(mixed $entity, string $reloadPath = ''): bool |
||
| 362 | { |
||
| 363 | $success = $entity->delete(); |
||
| 364 | |||
| 365 | if (!$success) { |
||
| 366 | $errors = $entity->getMessages(); |
||
| 367 | $this->flash->error(implode('<br>', $errors)); |
||
| 368 | } elseif (!$this->request->isAjax()) { |
||
| 369 | // $this->flash->success($this->translation->_('ms_SuccessfulSaved')); |
||
| 370 | if ($reloadPath!=='') { |
||
| 371 | $this->forward($reloadPath); |
||
| 372 | } |
||
| 373 | } |
||
| 374 | |||
| 375 | if ($this->request->isAjax()) { |
||
| 376 | $this->view->success = $success; |
||
| 377 | if ($reloadPath!=='' && $success) { |
||
| 378 | $this->view->reload = $reloadPath; |
||
| 379 | } |
||
| 380 | } |
||
| 381 | |||
| 382 | return $success; |
||
| 383 | } |
||
| 384 | |||
| 385 | /** |
||
| 386 | * Creates a JPEG file from the provided image. |
||
| 387 | * |
||
| 388 | * @param string $base64_string The base64 encoded image string. |
||
| 389 | * @param string $output_file The output file path to save the JPEG file. |
||
| 390 | * |
||
| 391 | * @return void |
||
| 392 | */ |
||
| 393 | protected function base64ToJpegFile(string $base64_string, string $output_file): void |
||
| 412 | } |
||
| 413 | } |
||
| 414 | |||
| 415 | /** |
||
| 416 | * Recursively sanitizes input data based on the provided filter. |
||
| 417 | * |
||
| 418 | * @param array $data The data to be sanitized. |
||
| 419 | * @param \Phalcon\Filter\FilterInterface $filter The filter object used for sanitization. |
||
| 420 | * |
||
| 421 | * @return array The sanitized data. |
||
| 422 | */ |
||
| 423 | public static function sanitizeData(array $data, \Phalcon\Filter\FilterInterface $filter): array |
||
| 458 | } |
||
| 459 | } |
||
| 460 |