Total Complexity | 50 |
Total Lines | 461 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like CoreConfigClass 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 CoreConfigClass, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
33 | abstract class CoreConfigClass extends Injectable implements AsteriskConfigInterface |
||
34 | { |
||
35 | |||
36 | /** |
||
37 | * Config file name i.e. extensions.conf |
||
38 | */ |
||
39 | protected string $description; |
||
40 | private string $stageMessage = ''; |
||
41 | |||
42 | /** |
||
43 | * Easy way to get or set the PbxSettings values |
||
44 | * |
||
45 | * @var \MikoPBX\Core\System\MikoPBXConfig |
||
46 | */ |
||
47 | protected MikoPBXConfig $mikoPBXConfig; |
||
48 | |||
49 | /** |
||
50 | * Access to the /etc/inc/mikopbx-settings.json values |
||
51 | * |
||
52 | * @var \Phalcon\Config |
||
53 | */ |
||
54 | protected Config $config; |
||
55 | |||
56 | /** |
||
57 | * Shows if it is boot process now or usual work |
||
58 | * |
||
59 | * @var bool |
||
60 | */ |
||
61 | protected bool $booting; |
||
62 | |||
63 | /** |
||
64 | * Error and notice messages |
||
65 | * |
||
66 | * @var array |
||
67 | */ |
||
68 | protected array $messages; |
||
69 | |||
70 | /** |
||
71 | * Array of PbxSettings values |
||
72 | */ |
||
73 | protected array $generalSettings; |
||
74 | |||
75 | /** |
||
76 | * ConfigClass constructor. |
||
77 | */ |
||
78 | public function __construct() |
||
79 | { |
||
80 | $this->config = $this->getDI()->getShared(ConfigProvider::SERVICE_NAME); |
||
81 | $this->booting = $this->getDI()->getShared(RegistryProvider::SERVICE_NAME)->booting === true; |
||
82 | $this->mikoPBXConfig = new MikoPBXConfig(); |
||
83 | $this->generalSettings = $this->mikoPBXConfig->getGeneralSettings(); |
||
84 | $this->messages = []; |
||
85 | } |
||
86 | |||
87 | /** |
||
88 | * Calls additional module method by name and returns plain text result |
||
89 | * |
||
90 | * @param string $methodName |
||
91 | * @param array $arguments |
||
92 | * |
||
93 | * @return string |
||
94 | */ |
||
95 | public function hookModulesMethod(string $methodName, array $arguments = []): string |
||
96 | { |
||
97 | $stringResult = ''; |
||
98 | $additionalModules = $this->di->getShared(PBXConfModulesProvider::SERVICE_NAME); |
||
99 | foreach ($additionalModules as $configClassObj) { |
||
100 | if ( ! method_exists($configClassObj, $methodName)) { |
||
101 | continue; |
||
102 | } |
||
103 | if (get_class($configClassObj) === get_class($this)) { |
||
104 | continue; //prevent recursion |
||
105 | } |
||
106 | try { |
||
107 | $includeString = call_user_func_array([$configClassObj, $methodName], $arguments); |
||
108 | if ( ! empty($includeString)) { |
||
109 | $includeString = $configClassObj->confBlockWithComments($includeString); |
||
110 | if ( |
||
111 | substr($stringResult, -1) !== "\t" |
||
112 | && |
||
113 | substr($includeString, 0, 4) === 'same' |
||
114 | ) { |
||
115 | $stringResult .= "\t" . $includeString; |
||
116 | } else { |
||
117 | $stringResult .= $includeString; |
||
118 | } |
||
119 | } |
||
120 | } catch (\Throwable $e) { |
||
121 | global $errorLogger; |
||
122 | $errorLogger->captureException($e); |
||
123 | Util::sysLogMsg(__METHOD__, $e->getMessage(), LOG_ERR); |
||
124 | continue; |
||
125 | } |
||
126 | } |
||
127 | |||
128 | return $stringResult; |
||
129 | } |
||
130 | |||
131 | /** |
||
132 | * Calls additional module method by name and returns array of results |
||
133 | * |
||
134 | * @param string $methodName |
||
135 | * @param array $arguments |
||
136 | * |
||
137 | * @return array |
||
138 | */ |
||
139 | public function hookModulesMethodWithArrayResult(string $methodName, array $arguments = []): array |
||
165 | } |
||
166 | |||
167 | /** |
||
168 | * Makes pretty module text block into config file |
||
169 | * |
||
170 | * @param string $addition |
||
171 | * |
||
172 | * @return string |
||
173 | */ |
||
174 | protected function confBlockWithComments(string $addition): string |
||
175 | { |
||
176 | if (empty($addition)) { |
||
177 | return ''; |
||
178 | } |
||
179 | |||
180 | return rtrim($addition) . PHP_EOL; |
||
181 | } |
||
182 | |||
183 | /** |
||
184 | * Generates core modules config files with cli messages before and after generation |
||
185 | */ |
||
186 | public function generateConfig(): void |
||
187 | { |
||
188 | $this->echoGenerateConfig(); |
||
189 | $this->getSettings(); |
||
190 | $this->generateConfigProtected(); |
||
191 | $this->echoDone(); |
||
192 | } |
||
193 | |||
194 | /** |
||
195 | * Shows boot message which module was started |
||
196 | */ |
||
197 | protected function echoGenerateConfig(): void |
||
198 | { |
||
199 | if ($this->booting === true && ! empty($this->description)) { |
||
200 | $this->stageMessage = " |- generate config {$this->description}..."; |
||
201 | Util::echoWithSyslog($this->stageMessage); |
||
202 | } |
||
203 | } |
||
204 | |||
205 | /** |
||
206 | * Prepares settings dataset for a PBX module |
||
207 | */ |
||
208 | public function getSettings(): void |
||
209 | { |
||
210 | } |
||
211 | |||
212 | /** |
||
213 | * Generates core modules config files |
||
214 | */ |
||
215 | protected function generateConfigProtected(): void |
||
216 | { |
||
217 | } |
||
218 | |||
219 | /** |
||
220 | * Shows boot message which module generator was finished |
||
221 | */ |
||
222 | protected function echoDone(): void |
||
223 | { |
||
224 | if ($this->booting === true && ! empty($this->description)) { |
||
225 | Util::echoResult($this->stageMessage); |
||
226 | $this->stageMessage = ''; |
||
227 | } |
||
228 | } |
||
229 | |||
230 | /** |
||
231 | * Prepares additional includes for [internal] context section in the extensions.conf file |
||
232 | * |
||
233 | * @return string |
||
234 | */ |
||
235 | public function getIncludeInternal(): string |
||
236 | { |
||
237 | return ''; |
||
238 | } |
||
239 | |||
240 | /** |
||
241 | * Prepares additional rules for [internal] context section in the extensions.conf file |
||
242 | * |
||
243 | * @return string |
||
244 | */ |
||
245 | public function extensionGenInternal(): string |
||
246 | { |
||
247 | return ''; |
||
248 | } |
||
249 | |||
250 | /** |
||
251 | * Prepares additional rules for [internal-users] context section in the extensions.conf file |
||
252 | * |
||
253 | * @return string |
||
254 | */ |
||
255 | public function extensionGenInternalUsersPreDial(): string |
||
258 | } |
||
259 | |||
260 | /** |
||
261 | * Prepares additional rules for [all_peers] context section in the extensions.conf file |
||
262 | * |
||
263 | * @return string |
||
264 | */ |
||
265 | public function extensionGenAllPeersContext(): string |
||
266 | { |
||
267 | return ''; |
||
268 | } |
||
269 | |||
270 | /** |
||
271 | * Prepares additional includes for [internal-transfer] context section in the extensions.conf file |
||
272 | * |
||
273 | * @return string |
||
274 | */ |
||
275 | public function getIncludeInternalTransfer(): string |
||
276 | { |
||
277 | return ''; |
||
278 | } |
||
279 | |||
280 | /** |
||
281 | * Prepares additional rules for [internal-transfer] context section in the extensions.conf file |
||
282 | * |
||
283 | * @return string |
||
284 | */ |
||
285 | public function extensionGenInternalTransfer(): string |
||
286 | { |
||
287 | return ''; |
||
288 | } |
||
289 | |||
290 | /** |
||
291 | * Prepares additional contexts sections in the extensions.conf file |
||
292 | * |
||
293 | * @return string |
||
294 | */ |
||
295 | public function extensionGenContexts(): string |
||
298 | } |
||
299 | |||
300 | /** |
||
301 | * Prepares additional hints for [internal-hints] context section in the extensions.conf file |
||
302 | * |
||
303 | * @return string |
||
304 | */ |
||
305 | public function extensionGenHints(): string |
||
306 | { |
||
307 | return ''; |
||
308 | } |
||
309 | |||
310 | /** |
||
311 | * Prepares additional parameters for [globals] section in the extensions.conf file |
||
312 | * |
||
313 | * @return string |
||
314 | */ |
||
315 | public function extensionGlobals(): string |
||
316 | { |
||
317 | return ''; |
||
318 | } |
||
319 | |||
320 | /** |
||
321 | * Prepares additional parameters for [featuremap] section in the features.conf file |
||
322 | * |
||
323 | * @return string returns additional Star codes |
||
324 | */ |
||
325 | public function getFeatureMap(): string |
||
326 | { |
||
327 | return ''; |
||
328 | } |
||
329 | |||
330 | /** |
||
331 | * Prepares additional parameters for [public-direct-dial] section in the extensions.conf file |
||
332 | * |
||
333 | * @return string |
||
334 | */ |
||
335 | public function generatePublicContext(): string |
||
336 | { |
||
337 | return ''; |
||
338 | } |
||
339 | |||
340 | /** |
||
341 | * Prepares additional parameters for each incoming context for each incoming route before dial in the |
||
342 | * extensions.conf file |
||
343 | * |
||
344 | * @param string $rout_number |
||
345 | * |
||
346 | * @return string |
||
347 | */ |
||
348 | public function generateIncomingRoutBeforeDialSystem(string $rout_number): string |
||
351 | } |
||
352 | |||
353 | /** |
||
354 | * Prepares additional parameters for each incoming context for each incoming route before dial in the |
||
355 | * extensions.conf file |
||
356 | * |
||
357 | * @param string $rout_number |
||
358 | * |
||
359 | * @return string |
||
360 | */ |
||
361 | public function generateIncomingRoutBeforeDialPreSystem(string $rout_number): string |
||
362 | { |
||
363 | return ''; |
||
364 | } |
||
365 | |||
366 | /** |
||
367 | * Prepares additional parameters for each incoming context for each incoming route before dial in the |
||
368 | * extensions.conf file |
||
369 | * |
||
370 | * @param string $rout_number |
||
371 | * |
||
372 | * @return string |
||
373 | */ |
||
374 | public function generateIncomingRoutBeforeDial(string $rout_number): string |
||
375 | { |
||
376 | return ''; |
||
377 | } |
||
378 | |||
379 | /** |
||
380 | * Returns the messages variable |
||
381 | * |
||
382 | * @return array |
||
383 | */ |
||
384 | public function getMessages(): array |
||
385 | { |
||
386 | return $this->messages; |
||
387 | } |
||
388 | |||
389 | /** |
||
390 | * Returns models list of models which affect the current module settings |
||
391 | * |
||
392 | * @return array |
||
393 | */ |
||
394 | public function getDependenceModels(): array |
||
395 | { |
||
396 | return []; |
||
397 | } |
||
398 | |||
399 | /** |
||
400 | * Prepares additional parameters for each outgoing route context |
||
401 | * before dial call in the extensions.conf file |
||
402 | * |
||
403 | * @param array $rout |
||
404 | * |
||
405 | * @return string |
||
406 | */ |
||
407 | public function generateOutRoutContext(array $rout): string |
||
408 | { |
||
409 | return ''; |
||
410 | } |
||
411 | |||
412 | /** |
||
413 | * Override pjsip options for provider in the pjsip.conf file |
||
414 | * |
||
415 | * @param string $uniqid the provider unique identifier |
||
416 | * @param array $options list of pjsip options |
||
417 | * |
||
418 | * @return array |
||
419 | */ |
||
420 | public function overrideProviderPJSIPOptions(string $uniqid, array $options): array |
||
421 | { |
||
422 | return $options; |
||
423 | } |
||
424 | |||
425 | /** |
||
426 | * Override pjsip options for peer in the pjsip.conf file |
||
427 | * |
||
428 | * @param string $extension the endpoint extension |
||
429 | * @param array $options list of pjsip options |
||
430 | * |
||
431 | * @return array |
||
432 | */ |
||
433 | public function overridePJSIPOptions(string $extension, array $options): array |
||
434 | { |
||
435 | return $options; |
||
436 | } |
||
437 | |||
438 | /** |
||
439 | * Prepares additional parameters for each outgoing route context |
||
440 | * after dial call in the extensions.conf file |
||
441 | * |
||
442 | * @param array $rout |
||
443 | * |
||
444 | * @return string |
||
445 | */ |
||
446 | public function generateOutRoutAfterDialContext(array $rout): string |
||
447 | { |
||
448 | return ''; |
||
449 | } |
||
450 | |||
451 | /** |
||
452 | * Prepares additional pjsip options on endpoint section in the pjsip.conf file for peer |
||
453 | * |
||
454 | * @param array $peer information about peer |
||
455 | * |
||
456 | * @return string |
||
457 | */ |
||
458 | public function generatePeerPjAdditionalOptions(array $peer): string |
||
461 | } |
||
462 | |||
463 | /** |
||
464 | * Prepares additional AMI users data in the manager.conf file |
||
465 | * |
||
466 | * @return string |
||
467 | */ |
||
468 | public function generateManagerConf(): string |
||
471 | } |
||
472 | |||
473 | /** |
||
474 | * Prepares additional parameters for each incoming context |
||
475 | * and incoming route after dial command in an extensions.conf file |
||
476 | * |
||
477 | * @param string $uniqId |
||
478 | * |
||
479 | * @return string |
||
480 | */ |
||
481 | public function generateIncomingRoutAfterDialContext(string $uniqId): string |
||
484 | } |
||
485 | |||
486 | /** |
||
487 | * Prepares additional peers data in the pjsip.conf file |
||
488 | * |
||
489 | * @return string |
||
490 | */ |
||
491 | public function generatePeersPj(): string |
||
494 | } |
||
495 | |||
496 | } |