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