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