| Total Complexity | 45 |
| Total Lines | 276 |
| 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 |
||
| 42 | class BaseController extends Controller |
||
| 43 | { |
||
| 44 | protected string $actionName; |
||
| 45 | protected string $controllerName; |
||
| 46 | protected string $controllerNameUnCamelized; |
||
| 47 | |||
| 48 | public const WIKI_LINKS = '/var/etc/wiki-links-LANG.json'; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Initializes base class |
||
| 52 | */ |
||
| 53 | public function initialize(): void |
||
| 54 | { |
||
| 55 | $this->actionName = $this->dispatcher->getActionName(); |
||
| 56 | $this->controllerName = Text::camelize($this->dispatcher->getControllerName(), '_'); |
||
| 57 | $this->controllerNameUnCamelized = Text::uncamelize($this->controllerName, '-'); |
||
| 58 | |||
| 59 | $this->moduleName = $this->dispatcher->getModuleName(); |
||
| 60 | |||
| 61 | if ($this->request->isAjax() === false) { |
||
| 62 | $this->prepareView(); |
||
| 63 | } |
||
| 64 | } |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Кастомизация ссылок на wiki документацию. |
||
| 68 | * @return void |
||
| 69 | */ |
||
| 70 | private function customWikiLinks(): void |
||
| 71 | { |
||
| 72 | $filename = str_replace('LANG', $this->language, self::WIKI_LINKS); |
||
| 73 | if(!file_exists($filename)){ |
||
| 74 | return; |
||
| 75 | } |
||
| 76 | try { |
||
| 77 | $links = json_decode(file_get_contents($filename), true, 512, JSON_THROW_ON_ERROR); |
||
| 78 | }catch (\Exception $e){ |
||
| 79 | return; |
||
| 80 | } |
||
| 81 | $this->view->urlToWiki = $links[$this->view->urlToWiki]??$this->view->urlToWiki; |
||
| 82 | $this->view->urlToSupport = $links[$this->view->urlToSupport]??$this->view->urlToSupport; |
||
| 83 | } |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Prepares some environments to every controller and view |
||
| 87 | * |
||
| 88 | */ |
||
| 89 | protected function prepareView(): void |
||
| 90 | { |
||
| 91 | date_default_timezone_set($this->getSessionData('PBXTimezone')); |
||
| 92 | $roSession = $this->sessionRO; |
||
| 93 | $this->view->PBXVersion = $this->getSessionData('PBXVersion'); |
||
| 94 | if ($roSession !== null && array_key_exists('auth', $roSession)) { |
||
| 95 | $this->view->SSHPort = $this->getSessionData('SSHPort'); |
||
| 96 | $this->view->PBXLicense = $this->getSessionData('PBXLicense'); |
||
| 97 | } else { |
||
| 98 | $this->view->SSHPort = ''; |
||
| 99 | $this->view->PBXLicense = ''; |
||
| 100 | } |
||
| 101 | // Кеш версий модулей и атс, для правильной работы АТС при установке модулей |
||
| 102 | $versionHash = $this->getVersionsHash(); |
||
| 103 | $this->session->set('versionHash', $versionHash); |
||
| 104 | |||
| 105 | $this->view->WebAdminLanguage = $this->getSessionData('WebAdminLanguage'); |
||
| 106 | $this->view->AvailableLanguages = json_encode($this->elements->getAvailableWebAdminLanguages()); |
||
| 107 | |||
| 108 | if ($roSession !== null && array_key_exists('SubmitMode', $roSession)) { |
||
| 109 | $this->view->submitMode = $roSession['SubmitMode']; |
||
| 110 | } else { |
||
| 111 | $this->view->submitMode = 'SaveSettings'; |
||
| 112 | } |
||
| 113 | |||
| 114 | // Добавим версию модуля, если это модуль |
||
| 115 | if ($this->moduleName === 'PBXExtension') { |
||
| 116 | $module = PbxExtensionModules::findFirstByUniqid($this->controllerName); |
||
| 117 | if ($module === null) { |
||
| 118 | $module = new PbxExtensionModules(); |
||
| 119 | $module->disabled = '1'; |
||
| 120 | $module->name = 'Unknown module'; |
||
| 121 | } |
||
| 122 | $this->view->module = $module; |
||
| 123 | } |
||
| 124 | |||
| 125 | // Разрешим отправку анонимной информации об ошибках |
||
| 126 | if ($this->getSessionData('SendMetrics') === '1') { |
||
| 127 | touch('/tmp/sendmetrics'); |
||
| 128 | $this->view->lastSentryEventId = SentrySdk::getCurrentHub()->getLastEventId(); |
||
| 129 | } else { |
||
| 130 | if (file_exists('/tmp/sendmetrics')) { |
||
| 131 | unlink('/tmp/sendmetrics'); |
||
| 132 | } |
||
| 133 | $this->view->lastSentryEventId = null; |
||
| 134 | } |
||
| 135 | $title = 'MikoPBX'; |
||
| 136 | switch ($this->actionName) { |
||
| 137 | case'index': |
||
| 138 | case'delete': |
||
| 139 | case'save': |
||
| 140 | case'modify': |
||
| 141 | case'*** WITHOUT ACTION ***': |
||
| 142 | $title .= '|'. $this->translation->_("Breadcrumb{$this->controllerName}"); |
||
| 143 | break; |
||
| 144 | default: |
||
| 145 | $title .= '|'. $this->translation->_("Breadcrumb{$this->controllerName}{$this->actionName}"); |
||
| 146 | } |
||
| 147 | Tag::setTitle($title); |
||
| 148 | $this->view->t = $this->translation; |
||
| 149 | $this->view->debugMode = $this->config->path('adminApplication.debugMode'); |
||
| 150 | $this->view->urlToLogo = $this->url->get('assets/img/logo-mikopbx.svg'); |
||
| 151 | if ($this->language === 'ru') { |
||
| 152 | $this->view->urlToWiki |
||
| 153 | = "https://wiki.mikopbx.com/{$this->controllerNameUnCamelized}"; |
||
| 154 | $this->view->urlToSupport |
||
| 155 | = 'https://www.mikopbx.ru/support/?fromPBX=true'; |
||
| 156 | } else { |
||
| 157 | $this->view->urlToWiki |
||
| 158 | = "https://wiki.mikopbx.com/{$this->language}:{$this->controllerNameUnCamelized}"; |
||
| 159 | $this->view->urlToSupport |
||
| 160 | = 'https://www.mikopbx.com/support/?fromPBX=true'; |
||
| 161 | } |
||
| 162 | |||
| 163 | $this->customWikiLinks(); |
||
| 164 | |||
| 165 | $this->view->urlToController = $this->url->get($this->controllerNameUnCamelized); |
||
| 166 | $this->view->represent = ''; |
||
| 167 | $this->view->cacheName = "{$this->controllerName}{$this->actionName}{$this->language}{$versionHash}"; |
||
| 168 | |||
| 169 | // If it is module we have to use another template |
||
| 170 | if ($this->moduleName === 'PBXExtension') { |
||
| 171 | $this->view->setTemplateAfter('modules'); |
||
| 172 | } else { |
||
| 173 | $this->view->setTemplateAfter('main'); |
||
| 174 | } |
||
| 175 | |||
| 176 | } |
||
| 177 | |||
| 178 | /** |
||
| 179 | * Gets data from session or database if it not exists in session store |
||
| 180 | * |
||
| 181 | * @param $key string session parameter |
||
| 182 | * |
||
| 183 | * @return string |
||
| 184 | */ |
||
| 185 | protected function getSessionData(string $key): string |
||
| 186 | { |
||
| 187 | $roSession = $this->sessionRO; |
||
| 188 | if ($roSession !== null && array_key_exists($key, $roSession) && ! empty($roSession[$key])) { |
||
| 189 | $value = $roSession[$key]; |
||
| 190 | } else { |
||
| 191 | $value = PbxSettings::getValueByKey($key); |
||
| 192 | $this->session->set($key, $value); |
||
| 193 | } |
||
| 194 | |||
| 195 | return $value; |
||
| 196 | } |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Generates common hash sum for correct combine CSS and JS according to installed modules |
||
| 200 | * |
||
| 201 | */ |
||
| 202 | private function getVersionsHash(): string |
||
| 203 | { |
||
| 204 | $result = PbxSettings::getValueByKey('PBXVersion'); |
||
| 205 | $modulesVersions = PbxExtensionModules::getModulesArray(); |
||
| 206 | foreach ($modulesVersions as $module) { |
||
| 207 | $result .= "{$module['id']}{$module['version']}"; |
||
| 208 | } |
||
| 209 | |||
| 210 | return md5($result); |
||
| 211 | } |
||
| 212 | |||
| 213 | /** |
||
| 214 | * Changes the AJAX response by expected format |
||
| 215 | * |
||
| 216 | * @return \Phalcon\Http\Response|\Phalcon\Http\ResponseInterface |
||
| 217 | */ |
||
| 218 | public function afterExecuteRoute() |
||
| 246 | } |
||
| 247 | |||
| 248 | /** |
||
| 249 | * Callback before execute any route |
||
| 250 | */ |
||
| 251 | public function beforeExecuteRoute(): void |
||
| 257 | } |
||
| 258 | } |
||
| 259 | } |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Change page without reload browser page |
||
| 263 | * |
||
| 264 | * @param string $uri |
||
| 265 | */ |
||
| 266 | protected function forward(string $uri): void |
||
| 267 | { |
||
| 268 | $uriParts = explode('/', $uri); |
||
| 269 | $params = array_slice($uriParts, 2); |
||
| 270 | |||
| 271 | $this->dispatcher->forward( |
||
| 272 | [ |
||
| 273 | 'controller' => $uriParts[0], |
||
| 274 | 'action' => $uriParts[1], |
||
| 275 | 'params' => $params, |
||
| 276 | ] |
||
| 277 | |||
| 278 | ); |
||
| 279 | } |
||
| 280 | |||
| 281 | /** |
||
| 282 | * Removes all dangerous symbols from CallerID |
||
| 283 | * @param string $callerId |
||
| 284 | * |
||
| 285 | * @return string |
||
| 286 | */ |
||
| 287 | protected function sanitizeCallerId(string $callerId): string |
||
| 290 | } |
||
| 291 | |||
| 292 | /** |
||
| 293 | * Sorts array by priority field |
||
| 294 | * |
||
| 295 | * @param $a |
||
| 296 | * @param $b |
||
| 297 | * |
||
| 298 | * @return int|null |
||
| 299 | */ |
||
| 300 | protected function sortArrayByPriority($a, $b): ?int |
||
| 318 | } |
||
| 319 | } |
||
| 320 | } |
||
| 321 |