Total Complexity | 48 |
Total Lines | 499 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like AsteriskConfigClass 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 AsteriskConfigClass, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
38 | class AsteriskConfigClass extends Injectable implements AsteriskConfigInterface |
||
39 | { |
||
40 | // The module hook applying priority |
||
41 | protected int $priority = 1000; |
||
42 | |||
43 | // Config file name i.e. extensions.conf |
||
44 | protected string $description; |
||
45 | private string $stageMessage = ''; |
||
46 | |||
47 | /** |
||
48 | * Easy way to get or set the PbxSettings values |
||
49 | * |
||
50 | * @var \MikoPBX\Core\System\MikoPBXConfig |
||
51 | */ |
||
52 | protected MikoPBXConfig $mikoPBXConfig; |
||
53 | |||
54 | /** |
||
55 | * Access to the /etc/inc/mikopbx-settings.json values |
||
56 | * |
||
57 | * @var \Phalcon\Config |
||
58 | */ |
||
59 | protected Config $config; |
||
60 | |||
61 | /** |
||
62 | * Shows if it is boot process now or usual work |
||
63 | * |
||
64 | * @var bool |
||
65 | */ |
||
66 | protected bool $booting; |
||
67 | |||
68 | /** |
||
69 | * Error and notice messages |
||
70 | * |
||
71 | * @var array |
||
72 | */ |
||
73 | protected array $messages; |
||
74 | |||
75 | /** |
||
76 | * Array of PbxSettings values |
||
77 | */ |
||
78 | protected array $generalSettings; |
||
79 | |||
80 | /** |
||
81 | * AsteriskConfigClass constructor. |
||
82 | */ |
||
83 | public function __construct() |
||
84 | { |
||
85 | $this->config = $this->getDI()->getShared(ConfigProvider::SERVICE_NAME); |
||
86 | $this->booting = $this->getDI()->getShared(RegistryProvider::SERVICE_NAME)->booting === true; |
||
87 | $this->mikoPBXConfig = new MikoPBXConfig(); |
||
88 | $this->generalSettings = $this->mikoPBXConfig->getGeneralSettings(); |
||
89 | $this->messages = []; |
||
90 | } |
||
91 | |||
92 | /** |
||
93 | * Calls the specified method on each module object and returns the concatenated results as a string. |
||
94 | * |
||
95 | * @param string $methodName The name of the method to call. |
||
96 | * @param array $arguments The arguments to pass to the method. |
||
97 | * |
||
98 | * @return string The concatenated results as a string. |
||
99 | */ |
||
100 | public function hookModulesMethod(string $methodName, array $arguments = []): string |
||
101 | { |
||
102 | $stringResult = ''; |
||
103 | $internalModules = $this->di->getShared(AsteriskConfModulesProvider::SERVICE_NAME); |
||
104 | $externalModules = $this->di->getShared(PBXConfModulesProvider::SERVICE_NAME); |
||
105 | $arrObjects = array_merge($internalModules, $externalModules); |
||
106 | |||
107 | // Sort the merged array based on the priority value |
||
108 | usort($arrObjects, function ($a, $b) use ($methodName) { |
||
109 | return $a->getMethodPriority($methodName) - $b->getMethodPriority($methodName); |
||
110 | }); |
||
111 | |||
112 | foreach ($arrObjects as $configClassObj) { |
||
113 | if (!method_exists($configClassObj, $methodName)) { |
||
114 | continue; |
||
115 | } |
||
116 | if (get_class($configClassObj) === get_class($this)) { |
||
117 | continue; //prevent recursion |
||
118 | } |
||
119 | try { |
||
120 | $includeString = call_user_func_array([$configClassObj, $methodName], $arguments); |
||
121 | if (!empty($includeString)) { |
||
122 | if (property_exists($configClassObj, 'moduleUniqueId')) { |
||
123 | $includeString = $this->confBlockWithComments($includeString, $configClassObj->moduleUniqueId); |
||
124 | } else { |
||
125 | $includeString = $this->confBlockWithComments($includeString); |
||
126 | } |
||
127 | if ( |
||
128 | substr($stringResult, -1) !== "\t" |
||
129 | && |
||
130 | substr($includeString, 0, 4) === 'same' |
||
131 | ) { |
||
132 | $stringResult .= "\t" . $includeString; |
||
133 | } else { |
||
134 | $stringResult .= $includeString; |
||
135 | } |
||
136 | } |
||
137 | } catch (\Throwable $e) { |
||
138 | global $errorLogger; |
||
139 | $errorLogger->captureException($e); |
||
140 | Util::sysLogMsg(__METHOD__, $e->getMessage(), LOG_ERR); |
||
141 | continue; |
||
142 | } |
||
143 | } |
||
144 | return $stringResult; |
||
145 | } |
||
146 | |||
147 | /** |
||
148 | * Adds comments and separators around the provided addition to create a configuration block. |
||
149 | * |
||
150 | * @param string $addition The content to add within the block. |
||
151 | * @param string $externalModuleUniqueId The unique identifier of the external module (optional). |
||
152 | * |
||
153 | * @return string The content wrapped in a configuration block with comments. |
||
154 | */ |
||
155 | protected function confBlockWithComments(string $addition, string $externalModuleUniqueId = ''): string |
||
164 | } |
||
165 | |||
166 | /** |
||
167 | * Generates core modules config files with cli messages before and after generation |
||
168 | * @see https://docs.mikopbx.com/mikopbx-development/module-developement/module-class#generateconfig |
||
169 | * |
||
170 | * @return void |
||
171 | */ |
||
172 | public function generateConfig(): void |
||
173 | { |
||
174 | $this->echoGenerateConfig(); // Display "Generating configuration" message |
||
175 | $this->getSettings(); // Retrieve the required settings |
||
176 | $this->generateConfigProtected(); // Generate the protected configuration content |
||
177 | $this->echoDone(); // Display "Configuration generated successfully" message |
||
178 | } |
||
179 | |||
180 | /** |
||
181 | * Displays a message indicating the start of the configuration generation process. |
||
182 | */ |
||
183 | protected function echoGenerateConfig(): void |
||
184 | { |
||
185 | if ($this->booting === true && !empty($this->description)) { |
||
186 | $this->stageMessage = " |- generate config {$this->description}..."; |
||
187 | Util::echoWithSyslog($this->stageMessage); // Output the message and log it in syslog |
||
188 | } |
||
189 | } |
||
190 | |||
191 | /** |
||
192 | * Prepares settings dataset for a PBX module |
||
193 | * @see https://docs.mikopbx.com/mikopbx-development/module-developement/module-class#other |
||
194 | * |
||
195 | * @return void |
||
196 | */ |
||
197 | public function getSettings(): void |
||
198 | { |
||
199 | } |
||
200 | |||
201 | /** |
||
202 | * Generates core modules config files |
||
203 | */ |
||
204 | protected function generateConfigProtected(): void |
||
205 | { |
||
206 | } |
||
207 | |||
208 | /** |
||
209 | * Displays a message indicating the successful completion of the configuration generation process. |
||
210 | */ |
||
211 | protected function echoDone(): void |
||
212 | { |
||
213 | if ($this->booting === true && !empty($this->description)) { |
||
214 | Util::echoResult($this->stageMessage); // Output the completion message |
||
215 | $this->stageMessage = ''; |
||
216 | } |
||
217 | } |
||
218 | |||
219 | /** |
||
220 | * Prepares additional includes for [internal] context section in the extensions.conf file |
||
221 | * @see https://docs.mikopbx.com/mikopbx-development/module-developement/module-class#getincludeinternal |
||
222 | * |
||
223 | * @return string |
||
224 | */ |
||
225 | public function getIncludeInternal(): string |
||
226 | { |
||
227 | return ''; |
||
228 | } |
||
229 | |||
230 | /** |
||
231 | * Generates the modules.conf file. |
||
232 | * @see https://docs.mikopbx.com/mikopbx-development/module-developement/module-class#generatemodulesconf |
||
233 | * |
||
234 | * @return string The generated modules.conf file content. |
||
235 | */ |
||
236 | public function generateModulesConf(): string |
||
239 | } |
||
240 | |||
241 | /** |
||
242 | * Prepares additional rules for [internal] context section in the extensions.conf file |
||
243 | * @see https://docs.mikopbx.com/mikopbx-development/module-developement/module-class#extensiongeninternal |
||
244 | * |
||
245 | * @return string |
||
246 | */ |
||
247 | public function extensionGenInternal(): string |
||
248 | { |
||
249 | return ''; |
||
250 | } |
||
251 | |||
252 | /** |
||
253 | * Prepares additional rules for [internal-users] context section in the extensions.conf file |
||
254 | * @see https://docs.mikopbx.com/mikopbx-development/module-developement/module-class#extensiongeninternaluserspredial |
||
255 | * |
||
256 | * @return string |
||
257 | */ |
||
258 | public function extensionGenInternalUsersPreDial(): string |
||
259 | { |
||
260 | return ''; |
||
261 | } |
||
262 | |||
263 | /** |
||
264 | * Prepares additional rules for [all_peers] context section in the extensions.conf file |
||
265 | * @see https://docs.mikopbx.com/mikopbx-development/module-developement/module-class#extensiongenallpeerscontext |
||
266 | * |
||
267 | * @return string |
||
268 | */ |
||
269 | public function extensionGenAllPeersContext(): string |
||
270 | { |
||
271 | return ''; |
||
272 | } |
||
273 | |||
274 | /** |
||
275 | * Prepares additional includes for [internal-transfer] context section in the extensions.conf file |
||
276 | * @see https://docs.mikopbx.com/mikopbx-development/module-developement/module-class#getincludeinternaltransfer |
||
277 | * |
||
278 | * @return string |
||
279 | */ |
||
280 | public function getIncludeInternalTransfer(): string |
||
281 | { |
||
282 | return ''; |
||
283 | } |
||
284 | |||
285 | /** |
||
286 | * Prepares additional rules for [internal-transfer] context section in the extensions.conf file |
||
287 | * @see https://docs.mikopbx.com/mikopbx-development/module-developement/module-class#extensiongeninternaltransfer |
||
288 | * |
||
289 | * @return string |
||
290 | */ |
||
291 | public function extensionGenInternalTransfer(): string |
||
292 | { |
||
293 | return ''; |
||
294 | } |
||
295 | |||
296 | /** |
||
297 | * Prepares additional contexts sections in the extensions.conf file |
||
298 | * @see https://docs.mikopbx.com/mikopbx-development/module-developement/module-class#extensiongencontexts |
||
299 | * |
||
300 | * @return string |
||
301 | */ |
||
302 | public function extensionGenContexts(): string |
||
303 | { |
||
304 | return ''; |
||
305 | } |
||
306 | |||
307 | /** |
||
308 | * Prepares additional hints for [internal-hints] context section in the extensions.conf file |
||
309 | * @see https://docs.mikopbx.com/mikopbx-development/module-developement/module-class#extensiongenhints |
||
310 | * |
||
311 | * @return string |
||
312 | */ |
||
313 | public function extensionGenHints(): string |
||
314 | { |
||
315 | return ''; |
||
316 | } |
||
317 | |||
318 | /** |
||
319 | * Prepares additional parameters for [globals] section in the extensions.conf file |
||
320 | * @see https://docs.mikopbx.com/mikopbx-development/module-developement/module-class#extensionglobals |
||
321 | * |
||
322 | * @return string |
||
323 | */ |
||
324 | public function extensionGlobals(): string |
||
325 | { |
||
326 | return ''; |
||
327 | } |
||
328 | |||
329 | /** |
||
330 | * Prepares additional parameters for [featuremap] section in the features.conf file |
||
331 | * @see https://docs.mikopbx.com/mikopbx-development/module-developement/module-class#getfeaturemap |
||
332 | * |
||
333 | * @return string returns additional Star codes |
||
334 | */ |
||
335 | public function getFeatureMap(): string |
||
336 | { |
||
337 | return ''; |
||
338 | } |
||
339 | |||
340 | /** |
||
341 | * Prepares additional parameters for [public-direct-dial] section in the extensions.conf file |
||
342 | * @see https://docs.mikopbx.com/mikopbx-development/module-developement/module-class#generatepubliccontext |
||
343 | * |
||
344 | * @return string |
||
345 | */ |
||
346 | public function generatePublicContext(): string |
||
347 | { |
||
348 | return ''; |
||
349 | } |
||
350 | |||
351 | /** |
||
352 | * Prepares additional parameters for each incoming context for each incoming route before dial in the |
||
353 | * extensions.conf file |
||
354 | * @see https://docs.mikopbx.com/mikopbx-development/module-developement/module-class#generateincomingroutbeforedialpresystem |
||
355 | * |
||
356 | * @param string $rout_number |
||
357 | * |
||
358 | * @return string |
||
359 | */ |
||
360 | public function generateIncomingRoutBeforeDialPreSystem(string $rout_number): string |
||
361 | { |
||
362 | return ''; |
||
363 | } |
||
364 | |||
365 | /** |
||
366 | * Prepares additional parameters for each incoming context for each incoming route before dial in the |
||
367 | * extensions.conf file |
||
368 | * @see https://docs.mikopbx.com/mikopbx-development/module-developement/module-class#generateincomingroutbeforedialsystem |
||
369 | * |
||
370 | * @param string $rout_number |
||
371 | * |
||
372 | * @return string |
||
373 | */ |
||
374 | public function generateIncomingRoutBeforeDialSystem(string $rout_number): string |
||
375 | { |
||
376 | return ''; |
||
377 | } |
||
378 | |||
379 | /** |
||
380 | * Prepares additional parameters for each incoming context for each incoming route before dial in the |
||
381 | * extensions.conf file |
||
382 | * @see https://docs.mikopbx.com/mikopbx-development/module-developement/module-class#generateincomingroutbeforedial |
||
383 | * |
||
384 | * @param string $rout_number |
||
385 | * |
||
386 | * @return string |
||
387 | */ |
||
388 | public function generateIncomingRoutBeforeDial(string $rout_number): string |
||
391 | } |
||
392 | |||
393 | /** |
||
394 | * Returns the messages variable |
||
395 | * @see https://docs.mikopbx.com/mikopbx-development/module-developement/module-class#other |
||
396 | * |
||
397 | * @return array |
||
398 | */ |
||
399 | public function getMessages(): array |
||
400 | { |
||
401 | return $this->messages; |
||
402 | } |
||
403 | |||
404 | /** |
||
405 | * Returns models list of models which affect the current module settings |
||
406 | * @see https://docs.mikopbx.com/mikopbx-development/module-developement/module-class#other |
||
407 | * |
||
408 | * @return array |
||
409 | */ |
||
410 | public function getDependenceModels(): array |
||
411 | { |
||
412 | return []; |
||
413 | } |
||
414 | |||
415 | /** |
||
416 | * Prepares additional parameters for each outgoing route context |
||
417 | * before dial call in the extensions.conf file |
||
418 | * @see https://docs.mikopbx.com/mikopbx-development/module-developement/module-class#generateoutroutcontext |
||
419 | * |
||
420 | * @param array $rout |
||
421 | * |
||
422 | * @return string |
||
423 | */ |
||
424 | public function generateOutRoutContext(array $rout): string |
||
425 | { |
||
426 | return ''; |
||
427 | } |
||
428 | |||
429 | /** |
||
430 | * Override pjsip options for provider in the pjsip.conf file |
||
431 | * @see https://docs.mikopbx.com/mikopbx-development/module-developement/module-class#overrideproviderpjsipoptions |
||
432 | * |
||
433 | * @param string $uniqid the provider unique identifier |
||
434 | * @param array $options list of pjsip options |
||
435 | * |
||
436 | * @return array |
||
437 | */ |
||
438 | public function overrideProviderPJSIPOptions(string $uniqid, array $options): array |
||
439 | { |
||
440 | return $options; |
||
441 | } |
||
442 | |||
443 | /** |
||
444 | * Override pjsip options for peer in the pjsip.conf file |
||
445 | * @see https://docs.mikopbx.com/mikopbx-development/module-developement/module-class#overridepjsipoptions |
||
446 | * |
||
447 | * @param string $extension the endpoint extension |
||
448 | * @param array $options list of pjsip options |
||
449 | * |
||
450 | * @return array |
||
451 | */ |
||
452 | public function overridePJSIPOptions(string $extension, array $options): array |
||
453 | { |
||
454 | return $options; |
||
455 | } |
||
456 | |||
457 | /** |
||
458 | * Prepares additional parameters for each outgoing route context |
||
459 | * after dial call in the extensions.conf file |
||
460 | * @see https://docs.mikopbx.com/mikopbx-development/module-developement/module-class#generateoutroutafterdialcontext |
||
461 | * |
||
462 | * @param array $rout |
||
463 | * |
||
464 | * @return string |
||
465 | */ |
||
466 | public function generateOutRoutAfterDialContext(array $rout): string |
||
467 | { |
||
468 | return ''; |
||
469 | } |
||
470 | |||
471 | /** |
||
472 | * Prepares additional pjsip options on endpoint section in the pjsip.conf file for peer |
||
473 | * @see https://docs.mikopbx.com/mikopbx-development/module-developement/module-class#generatepeerpjadditionaloptions |
||
474 | * |
||
475 | * @param array $peer information about peer |
||
476 | * |
||
477 | * @return string |
||
478 | */ |
||
479 | public function generatePeerPjAdditionalOptions(array $peer): string |
||
480 | { |
||
481 | return ''; |
||
482 | } |
||
483 | |||
484 | /** |
||
485 | * Prepares additional AMI users data in the manager.conf file |
||
486 | * @see https://docs.mikopbx.com/mikopbx-development/module-developement/module-class#generatemanagerconf |
||
487 | * |
||
488 | * @return string |
||
489 | */ |
||
490 | public function generateManagerConf(): string |
||
491 | { |
||
492 | return ''; |
||
493 | } |
||
494 | |||
495 | /** |
||
496 | * Prepares additional parameters for each incoming context |
||
497 | * and incoming route after dial command in an extensions.conf file |
||
498 | * @see https://docs.mikopbx.com/mikopbx-development/module-developement/module-class#generateincomingroutafterdialcontext |
||
499 | * |
||
500 | * |
||
501 | * @param string $uniqId |
||
502 | * |
||
503 | * @return string |
||
504 | */ |
||
505 | public function generateIncomingRoutAfterDialContext(string $uniqId): string |
||
506 | { |
||
507 | return ''; |
||
508 | } |
||
509 | |||
510 | /** |
||
511 | * Prepares additional peers data in the pjsip.conf file |
||
512 | * @see https://docs.mikopbx.com/mikopbx-development/module-developement/module-class#generatepeerspj |
||
513 | * |
||
514 | * @return string |
||
515 | */ |
||
516 | public function generatePeersPj(): string |
||
519 | } |
||
520 | |||
521 | /** |
||
522 | * Allows overriding the execution priority of a method when called through hookModulesMethod. |
||
523 | * @see https://docs.mikopbx.com/mikopbx-development/module-developement/module-class#getmethodpriority |
||
524 | * |
||
525 | * @param string $methodName |
||
526 | * |
||
527 | * @return int |
||
528 | */ |
||
529 | public function getMethodPriority(string $methodName = ''): int |
||
537 | } |
||
538 | |||
539 | } |