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