| Total Complexity | 47 |
| Total Lines | 285 |
| Duplicated Lines | 0 % |
| Changes | 4 | ||
| 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 |
||
| 46 | class BaseController extends Controller |
||
| 47 | { |
||
| 48 | protected string $actionName; |
||
| 49 | protected string $controllerName; |
||
| 50 | protected string $controllerNameUnCamelized; |
||
| 51 | |||
| 52 | public const WIKI_LINKS = '/var/etc/wiki-links-LANG.json'; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Initializes base class |
||
| 56 | */ |
||
| 57 | public function initialize(): void |
||
| 65 | } |
||
| 66 | } |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Customization of links to the wiki documentation for modules. |
||
| 70 | * @param array $links |
||
| 71 | * @return void |
||
| 72 | */ |
||
| 73 | private function customModuleWikiLinks(array $links): void |
||
| 74 | { |
||
| 75 | $this->view->urlToWiki = $links[$this->language][$this->view->urlToWiki] ?? $this->view->urlToWiki; |
||
| 76 | $this->view->urlToSupport = $links[$this->language][$this->view->urlToSupport] ?? $this->view->urlToSupport; |
||
| 77 | } |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Customization of links to the wiki documentation. |
||
| 81 | * @return void |
||
| 82 | */ |
||
| 83 | private function customWikiLinks(): void |
||
| 130 | } |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Prepares some environments to every controller and view |
||
| 134 | * |
||
| 135 | */ |
||
| 136 | protected function prepareView(): void |
||
| 137 | { |
||
| 138 | date_default_timezone_set(PbxSettings::getValueByKey('PBXTimezone')); |
||
| 139 | $this->view->PBXVersion = PbxSettings::getValueByKey('PBXVersion'); |
||
| 140 | $this->view->MetaTegHeadDescription=$this->translation->_('MetategHeadDescription'); |
||
| 141 | if ($this->session->has(SessionController::SESSION_ID)) { |
||
| 142 | $this->view->PBXLicense = PbxSettings::getValueByKey('PBXLicense'); |
||
| 143 | } else { |
||
| 144 | $this->view->PBXLicense = ''; |
||
| 145 | } |
||
| 146 | |||
| 147 | $this->view->WebAdminLanguage = PbxSettings::getValueByKey('WebAdminLanguage'); |
||
| 148 | $this->view->AvailableLanguages = json_encode($this->elements->getAvailableWebAdminLanguages()); |
||
| 149 | $this->view->submitMode = $this->session->get('SubmitMode')??'SaveSettings'; |
||
| 150 | |||
| 151 | // Allow anonymous statistics collection for JS code |
||
| 152 | if (PbxSettings::getValueByKey('SendMetrics') === '1') { |
||
| 153 | touch('/tmp/sendmetrics'); |
||
| 154 | $this->view->lastSentryEventId = SentrySdk::getCurrentHub()->getLastEventId(); |
||
| 155 | } else { |
||
| 156 | if (file_exists('/tmp/sendmetrics')) { |
||
| 157 | unlink('/tmp/sendmetrics'); |
||
| 158 | } |
||
| 159 | $this->view->lastSentryEventId = null; |
||
| 160 | } |
||
| 161 | |||
| 162 | $title = 'MikoPBX'; |
||
| 163 | switch ($this->actionName) { |
||
| 164 | case'index': |
||
| 165 | case'delete': |
||
| 166 | case'save': |
||
| 167 | case'modify': |
||
| 168 | case'*** WITHOUT ACTION ***': |
||
| 169 | $title .= '|' . $this->translation->_("Breadcrumb{$this->controllerName}"); |
||
| 170 | break; |
||
| 171 | default: |
||
| 172 | $title .= '|' . $this->translation->_("Breadcrumb{$this->controllerName}{$this->actionName}"); |
||
| 173 | } |
||
| 174 | Tag::setTitle($title); |
||
| 175 | $this->view->t = $this->translation; |
||
| 176 | $this->view->debugMode = $this->config->path('adminApplication.debugMode'); |
||
| 177 | $this->view->urlToLogo = $this->url->get('assets/img/logo-mikopbx.svg'); |
||
| 178 | $this->view->urlToWiki = "https://wiki.mikopbx.com/{$this->controllerNameUnCamelized}"; |
||
| 179 | if ($this->language === 'ru') { |
||
| 180 | $this->view->urlToSupport = 'https://www.mikopbx.ru/support/?fromPBX=true'; |
||
| 181 | } else { |
||
| 182 | $this->view->urlToSupport = 'https://www.mikopbx.com/support/?fromPBX=true'; |
||
| 183 | } |
||
| 184 | $this->view->urlToController = $this->url->get($this->controllerNameUnCamelized); |
||
| 185 | $this->view->represent = ''; |
||
| 186 | |||
| 187 | $isExternalModuleController = stripos($this->dispatcher->getNamespaceName(), '\\Module') === 0; |
||
| 188 | // We can disable module status toggle from module controller, using the showModuleStatusToggle variable |
||
| 189 | if (property_exists($this, 'showModuleStatusToggle')) { |
||
| 190 | $this->view->setVar('showModuleStatusToggle', $this->showModuleStatusToggle); |
||
| 191 | } else { |
||
| 192 | $this->view->setVar('showModuleStatusToggle', true); |
||
| 193 | } |
||
| 194 | |||
| 195 | // Add module variables into view |
||
| 196 | if ($isExternalModuleController) { |
||
| 197 | $moduleLinks = []; |
||
| 198 | /** @var PbxExtensionModules $module */ |
||
| 199 | $module = PbxExtensionModules::findFirstByUniqid($this->controllerName); |
||
| 200 | if ($module === null) { |
||
| 201 | $module = new PbxExtensionModules(); |
||
| 202 | $module->disabled = '1'; |
||
| 203 | $module->name = 'Unknown module'; |
||
| 204 | } else { |
||
| 205 | try { |
||
| 206 | $links = json_decode($module->wiki_links, true, 512, JSON_THROW_ON_ERROR); |
||
|
|
|||
| 207 | if (is_array($links)) { |
||
| 208 | $moduleLinks = $links; |
||
| 209 | } |
||
| 210 | } catch (\JsonException $e) { |
||
| 211 | Util::sysLogMsg(__CLASS__, $e->getMessage()); |
||
| 212 | } |
||
| 213 | } |
||
| 214 | $this->view->setVar('module', $module); |
||
| 215 | $this->customModuleWikiLinks($moduleLinks); |
||
| 216 | |||
| 217 | // If it is module we have to use another volt template |
||
| 218 | $this->view->setTemplateAfter('modules'); |
||
| 219 | } else { |
||
| 220 | $this->customWikiLinks(); |
||
| 221 | $this->view->setTemplateAfter('main'); |
||
| 222 | } |
||
| 223 | |||
| 224 | } |
||
| 225 | |||
| 226 | /** |
||
| 227 | * Changes the AJAX response by expected format |
||
| 228 | * |
||
| 229 | * @return \Phalcon\Http\ResponseInterface |
||
| 230 | */ |
||
| 231 | public function afterExecuteRoute():ResponseInterface |
||
| 259 | } |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Callback before execute any route |
||
| 263 | */ |
||
| 264 | public function beforeExecuteRoute(): void |
||
| 270 | } |
||
| 271 | } |
||
| 272 | } |
||
| 273 | |||
| 274 | /** |
||
| 275 | * Change page without reload browser page |
||
| 276 | * |
||
| 277 | * @param string $uri |
||
| 278 | */ |
||
| 279 | protected function forward(string $uri): void |
||
| 280 | { |
||
| 281 | $uriParts = explode('/', $uri); |
||
| 282 | $params = array_slice($uriParts, 2); |
||
| 283 | |||
| 284 | $this->dispatcher->forward( |
||
| 285 | [ |
||
| 286 | 'controller' => $uriParts[0], |
||
| 287 | 'action' => $uriParts[1], |
||
| 288 | 'params' => $params, |
||
| 289 | ] |
||
| 290 | |||
| 291 | ); |
||
| 292 | } |
||
| 293 | |||
| 294 | /** |
||
| 295 | * Removes all dangerous symbols from CallerID |
||
| 296 | * @param string $callerId |
||
| 297 | * |
||
| 298 | * @return string |
||
| 299 | */ |
||
| 300 | protected function sanitizeCallerId(string $callerId): string |
||
| 303 | } |
||
| 304 | |||
| 305 | /** |
||
| 306 | * Sorts array by priority field |
||
| 307 | * |
||
| 308 | * @param $a |
||
| 309 | * @param $b |
||
| 310 | * |
||
| 311 | * @return int|null |
||
| 312 | */ |
||
| 313 | protected function sortArrayByPriority($a, $b): ?int |
||
| 331 | } |
||
| 332 | } |
||
| 334 |