| Total Complexity | 49 |
| Total Lines | 510 |
| 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 |
||
| 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 |
||
| 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 |
||
| 316 | } |
||
| 317 | |||
| 318 | /** |
||
| 319 | * Adds priorities ащк [dial_create_chan] context section in the extensions.conf file |
||
| 320 | * @see https://docs.mikopbx.com/mikopbx-development/module-developement/module-class#extensiongenhints |
||
| 321 | * |
||
| 322 | * @return string |
||
| 323 | */ |
||
| 324 | public function extensionGenCreateChannelDialplan(): string |
||
| 325 | { |
||
| 326 | return ''; |
||
| 327 | } |
||
| 328 | |||
| 329 | /** |
||
| 330 | * Prepares additional parameters for [globals] section in the extensions.conf file |
||
| 331 | * @see https://docs.mikopbx.com/mikopbx-development/module-developement/module-class#extensionglobals |
||
| 332 | * |
||
| 333 | * @return string |
||
| 334 | */ |
||
| 335 | public function extensionGlobals(): string |
||
| 336 | { |
||
| 337 | return ''; |
||
| 338 | } |
||
| 339 | |||
| 340 | /** |
||
| 341 | * Prepares additional parameters for [featuremap] section in the features.conf file |
||
| 342 | * @see https://docs.mikopbx.com/mikopbx-development/module-developement/module-class#getfeaturemap |
||
| 343 | * |
||
| 344 | * @return string returns additional Star codes |
||
| 345 | */ |
||
| 346 | public function getFeatureMap(): string |
||
| 347 | { |
||
| 348 | return ''; |
||
| 349 | } |
||
| 350 | |||
| 351 | /** |
||
| 352 | * Prepares additional parameters for [public-direct-dial] section in the extensions.conf file |
||
| 353 | * @see https://docs.mikopbx.com/mikopbx-development/module-developement/module-class#generatepubliccontext |
||
| 354 | * |
||
| 355 | * @return string |
||
| 356 | */ |
||
| 357 | public function generatePublicContext(): string |
||
| 358 | { |
||
| 359 | return ''; |
||
| 360 | } |
||
| 361 | |||
| 362 | /** |
||
| 363 | * Prepares additional parameters for each incoming context for each incoming route before dial in the |
||
| 364 | * extensions.conf file |
||
| 365 | * @see https://docs.mikopbx.com/mikopbx-development/module-developement/module-class#generateincomingroutbeforedialpresystem |
||
| 366 | * |
||
| 367 | * @param string $rout_number |
||
| 368 | * |
||
| 369 | * @return string |
||
| 370 | */ |
||
| 371 | public function generateIncomingRoutBeforeDialPreSystem(string $rout_number): string |
||
| 372 | { |
||
| 373 | return ''; |
||
| 374 | } |
||
| 375 | |||
| 376 | /** |
||
| 377 | * Prepares additional parameters for each incoming context for each incoming route before dial in the |
||
| 378 | * extensions.conf file |
||
| 379 | * @see https://docs.mikopbx.com/mikopbx-development/module-developement/module-class#generateincomingroutbeforedialsystem |
||
| 380 | * |
||
| 381 | * @param string $rout_number |
||
| 382 | * |
||
| 383 | * @return string |
||
| 384 | */ |
||
| 385 | public function generateIncomingRoutBeforeDialSystem(string $rout_number): string |
||
| 386 | { |
||
| 387 | return ''; |
||
| 388 | } |
||
| 389 | |||
| 390 | /** |
||
| 391 | * Prepares additional parameters for each incoming context for each incoming route before dial in the |
||
| 392 | * extensions.conf file |
||
| 393 | * @see https://docs.mikopbx.com/mikopbx-development/module-developement/module-class#generateincomingroutbeforedial |
||
| 394 | * |
||
| 395 | * @param string $rout_number |
||
| 396 | * |
||
| 397 | * @return string |
||
| 398 | */ |
||
| 399 | public function generateIncomingRoutBeforeDial(string $rout_number): string |
||
| 402 | } |
||
| 403 | |||
| 404 | /** |
||
| 405 | * Returns the messages variable |
||
| 406 | * @see https://docs.mikopbx.com/mikopbx-development/module-developement/module-class#other |
||
| 407 | * |
||
| 408 | * @return array |
||
| 409 | */ |
||
| 410 | public function getMessages(): array |
||
| 411 | { |
||
| 412 | return $this->messages; |
||
| 413 | } |
||
| 414 | |||
| 415 | /** |
||
| 416 | * Returns models list of models which affect the current module settings |
||
| 417 | * @see https://docs.mikopbx.com/mikopbx-development/module-developement/module-class#other |
||
| 418 | * |
||
| 419 | * @return array |
||
| 420 | */ |
||
| 421 | public function getDependenceModels(): array |
||
| 422 | { |
||
| 423 | return []; |
||
| 424 | } |
||
| 425 | |||
| 426 | /** |
||
| 427 | * Prepares additional parameters for each outgoing route context |
||
| 428 | * before dial call in the extensions.conf file |
||
| 429 | * @see https://docs.mikopbx.com/mikopbx-development/module-developement/module-class#generateoutroutcontext |
||
| 430 | * |
||
| 431 | * @param array $rout |
||
| 432 | * |
||
| 433 | * @return string |
||
| 434 | */ |
||
| 435 | public function generateOutRoutContext(array $rout): string |
||
| 436 | { |
||
| 437 | return ''; |
||
| 438 | } |
||
| 439 | |||
| 440 | /** |
||
| 441 | * Override pjsip options for provider in the pjsip.conf file |
||
| 442 | * @see https://docs.mikopbx.com/mikopbx-development/module-developement/module-class#overrideproviderpjsipoptions |
||
| 443 | * |
||
| 444 | * @param string $uniqid the provider unique identifier |
||
| 445 | * @param array $options list of pjsip options |
||
| 446 | * |
||
| 447 | * @return array |
||
| 448 | */ |
||
| 449 | public function overrideProviderPJSIPOptions(string $uniqid, array $options): array |
||
| 450 | { |
||
| 451 | return $options; |
||
| 452 | } |
||
| 453 | |||
| 454 | /** |
||
| 455 | * Override pjsip options for peer in the pjsip.conf file |
||
| 456 | * @see https://docs.mikopbx.com/mikopbx-development/module-developement/module-class#overridepjsipoptions |
||
| 457 | * |
||
| 458 | * @param string $extension the endpoint extension |
||
| 459 | * @param array $options list of pjsip options |
||
| 460 | * |
||
| 461 | * @return array |
||
| 462 | */ |
||
| 463 | public function overridePJSIPOptions(string $extension, array $options): array |
||
| 464 | { |
||
| 465 | return $options; |
||
| 466 | } |
||
| 467 | |||
| 468 | /** |
||
| 469 | * Prepares additional parameters for each outgoing route context |
||
| 470 | * after dial call in the extensions.conf file |
||
| 471 | * @see https://docs.mikopbx.com/mikopbx-development/module-developement/module-class#generateoutroutafterdialcontext |
||
| 472 | * |
||
| 473 | * @param array $rout |
||
| 474 | * |
||
| 475 | * @return string |
||
| 476 | */ |
||
| 477 | public function generateOutRoutAfterDialContext(array $rout): string |
||
| 478 | { |
||
| 479 | return ''; |
||
| 480 | } |
||
| 481 | |||
| 482 | /** |
||
| 483 | * Prepares additional pjsip options on endpoint section in the pjsip.conf file for peer |
||
| 484 | * @see https://docs.mikopbx.com/mikopbx-development/module-developement/module-class#generatepeerpjadditionaloptions |
||
| 485 | * |
||
| 486 | * @param array $peer information about peer |
||
| 487 | * |
||
| 488 | * @return string |
||
| 489 | */ |
||
| 490 | public function generatePeerPjAdditionalOptions(array $peer): string |
||
| 491 | { |
||
| 492 | return ''; |
||
| 493 | } |
||
| 494 | |||
| 495 | /** |
||
| 496 | * Prepares additional AMI users data in the manager.conf file |
||
| 497 | * @see https://docs.mikopbx.com/mikopbx-development/module-developement/module-class#generatemanagerconf |
||
| 498 | * |
||
| 499 | * @return string |
||
| 500 | */ |
||
| 501 | public function generateManagerConf(): string |
||
| 502 | { |
||
| 503 | return ''; |
||
| 504 | } |
||
| 505 | |||
| 506 | /** |
||
| 507 | * Prepares additional parameters for each incoming context |
||
| 508 | * and incoming route after dial command in an extensions.conf file |
||
| 509 | * @see https://docs.mikopbx.com/mikopbx-development/module-developement/module-class#generateincomingroutafterdialcontext |
||
| 510 | * |
||
| 511 | * |
||
| 512 | * @param string $uniqId |
||
| 513 | * |
||
| 514 | * @return string |
||
| 515 | */ |
||
| 516 | public function generateIncomingRoutAfterDialContext(string $uniqId): string |
||
| 517 | { |
||
| 518 | return ''; |
||
| 519 | } |
||
| 520 | |||
| 521 | /** |
||
| 522 | * Prepares additional peers data in the pjsip.conf file |
||
| 523 | * @see https://docs.mikopbx.com/mikopbx-development/module-developement/module-class#generatepeerspj |
||
| 524 | * |
||
| 525 | * @return string |
||
| 526 | */ |
||
| 527 | public function generatePeersPj(): string |
||
| 530 | } |
||
| 531 | |||
| 532 | /** |
||
| 533 | * Allows overriding the execution priority of a method when called through hookModulesMethod. |
||
| 534 | * @see https://docs.mikopbx.com/mikopbx-development/module-developement/module-class#getmethodpriority |
||
| 535 | * |
||
| 536 | * @param string $methodName |
||
| 537 | * |
||
| 538 | * @return int |
||
| 539 | */ |
||
| 540 | public function getMethodPriority(string $methodName = ''): int |
||
| 548 | } |
||
| 549 | |||
| 550 | } |