Total Complexity | 50 |
Total Lines | 359 |
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(Dispatcher $dispatcher): 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,[$dispatcher]); |
||
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(Dispatcher $dispatcher): 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 | $sentry = $this->di->get(SentryErrorHandlerProvider::SERVICE_NAME,['admin-cabinet']); |
||
196 | if ($sentry){ |
||
197 | $data['lastSentryEventId'] = $sentry->getLastEventId(); |
||
198 | } |
||
199 | |||
200 | $result = json_encode($data); |
||
201 | } |
||
202 | $this->response->setContent($result); |
||
203 | } |
||
204 | |||
205 | PBXConfModulesProvider::hookModulesMethod(WebUIConfigInterface::ON_AFTER_EXECUTE_ROUTE,[$dispatcher]); |
||
206 | |||
207 | return $this->response->send(); |
||
208 | } |
||
209 | |||
210 | /** |
||
211 | * Forwards the request to a different controller and action based on the provided URI. |
||
212 | * |
||
213 | * @param string $uri The URI to forward to. |
||
214 | * @return void |
||
215 | */ |
||
216 | protected function forward(string $uri): void |
||
217 | { |
||
218 | $uriParts = explode('/', $uri); |
||
219 | if ($this->isExternalModuleController and count($uriParts)>2){ |
||
220 | $params = array_slice($uriParts, 3); |
||
221 | $moduleUniqueID = $this->getModuleUniqueId(); |
||
222 | $this->dispatcher->forward( |
||
223 | [ |
||
224 | 'namespace'=>"Modules\\{$moduleUniqueID}\\App\\Controllers", |
||
225 | 'controller' => $uriParts[1], |
||
226 | 'action' => $uriParts[2], |
||
227 | 'params' => $params, |
||
228 | ] |
||
229 | ); |
||
230 | } else { |
||
231 | $params = array_slice($uriParts, 2); |
||
232 | |||
233 | $this->dispatcher->forward( |
||
234 | [ |
||
235 | 'namespace'=>"MikoPBX\AdminCabinet\Controllers", |
||
236 | 'controller' => $uriParts[0], |
||
237 | 'action' => $uriParts[1], |
||
238 | 'params' => $params, |
||
239 | ] |
||
240 | ); |
||
241 | } |
||
242 | } |
||
243 | |||
244 | /** |
||
245 | * Sanitizes the caller ID by removing any characters that are not alphanumeric or spaces. |
||
246 | * |
||
247 | * @param string $callerId The caller ID to sanitize. |
||
248 | * @return string The sanitized caller ID. |
||
249 | */ |
||
250 | protected function sanitizeCallerId(string $callerId): string |
||
251 | { |
||
252 | return preg_replace('/[^a-zA-Zа-яА-Я0-9 ]/ui', '', $callerId); |
||
253 | } |
||
254 | |||
255 | /** |
||
256 | * Sorts array by priority field |
||
257 | * |
||
258 | * @param $a |
||
259 | * @param $b |
||
260 | * |
||
261 | * @return int|null |
||
262 | */ |
||
263 | protected function sortArrayByPriority($a, $b): ?int |
||
264 | { |
||
265 | if (is_array($a)) { |
||
266 | $a = (int)$a['priority']; |
||
267 | } else { |
||
268 | $a = (int)$a->priority; |
||
269 | } |
||
270 | |||
271 | if (is_array($b)) { |
||
272 | $b = (int)$b['priority']; |
||
273 | } else { |
||
274 | $b = (int)$b->priority; |
||
275 | } |
||
276 | |||
277 | if ($a === $b) { |
||
278 | return 0; |
||
279 | } else { |
||
280 | return ($a < $b) ? -1 : 1; |
||
281 | } |
||
282 | } |
||
283 | |||
284 | /** |
||
285 | * Sets the last Sentry event ID. |
||
286 | * |
||
287 | * @return \Sentry\EventId|null The last Sentry event ID, or null if metrics sending is disabled. |
||
288 | */ |
||
289 | private function setLastSentryEventId(): ?\Sentry\EventId |
||
290 | { |
||
291 | $result = null; |
||
292 | // Allow anonymous statistics collection for JS code |
||
293 | $sentry = $this->di->get(SentryErrorHandlerProvider::SERVICE_NAME); |
||
294 | if ($sentry){ |
||
295 | $result = $sentry->getLastEventId(); |
||
296 | } |
||
297 | return $result; |
||
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 | /** |
||
375 | * Creates a JPEG file from the provided image. |
||
376 | * |
||
377 | * @param string $base64_string The base64 encoded image string. |
||
378 | * @param string $output_file The output file path to save the JPEG file. |
||
379 | * |
||
380 | * @return void |
||
381 | */ |
||
382 | protected function base64ToJpegFile(string $base64_string, string $output_file): void |
||
404 |