| Total Complexity | 90 | 
| Total Lines | 444 | 
| Duplicated Lines | 0 % | 
| Changes | 2 | ||
| Bugs | 0 | Features | 0 | 
Complex classes like PbxSettings 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 PbxSettings, and based on these observations, apply Extract Interface, too.
| 1 | <?php | ||
| 21 | class PbxSettings extends ModelsBase | ||
| 22 | { | ||
| 23 | |||
| 24 | /** | ||
| 25 | * @Primary | ||
| 26 | * @Column(type="string", nullable=false) | ||
| 27 | */ | ||
| 28 | public string $key; | ||
| 29 | |||
| 30 | /** | ||
| 31 | * @Column(type="string", nullable=true) | ||
| 32 | */ | ||
| 33 | public ?string $value; | ||
| 34 | |||
| 35 | |||
| 36 | /** | ||
| 37 | * Prepares default values for PbxSettings keys | ||
| 38 | * | ||
| 39 | * @return array default values | ||
| 40 | */ | ||
| 41 | public static function getDefaultArrayValues(): array | ||
| 42 |     { | ||
| 43 | return [ | ||
| 44 | 'Name' => 'PBX system', | ||
| 45 | 'VirtualHardwareType' => 'REAL',//VMWARE,HYPERV,AWS,AZURE | ||
| 46 | 'Description' => '', | ||
| 47 | 'RestartEveryNight' => '0', | ||
| 48 | 'SIPPort' => '5060', | ||
| 49 | 'SIPDefaultExpiry' => '120', | ||
| 50 | 'SIPMinExpiry' => '60', | ||
| 51 | 'SIPMaxExpiry' => '3600', | ||
| 52 | 'RTPPortFrom' => '10000', | ||
| 53 | 'RTPPortTo' => '10200', | ||
| 54 | 'IAXPort' => '4569', | ||
| 55 | 'AMIEnabled' => '1', | ||
| 56 | 'AMIPort' => '5038', | ||
| 57 | 'AJAMEnabled' => '1', | ||
| 58 | 'AJAMPort' => '8088', | ||
| 59 | 'AJAMPortTLS' => '8089', | ||
| 60 | 'SSHPort' => '22', | ||
| 61 | 'SSHPassword' => 'admin', | ||
| 62 | 'SSHRsaKey' => '', | ||
| 63 | 'SSHDssKey' => '', | ||
| 64 | 'SSHAuthorizedKeys' => '', | ||
| 65 | 'SSHecdsaKey' => '', | ||
| 66 | 'SSHLanguage' => 'en', | ||
| 67 | 'WEBPort' => '80', | ||
| 68 | 'WEBHTTPSPort' => '443', | ||
| 69 | 'WEBHTTPSPublicKey' => '', | ||
| 70 | 'WEBHTTPSPrivateKey' => '', | ||
| 71 | 'RedirectToHttps' => '0', | ||
| 72 | 'MailSMTPUseTLS' => '0', | ||
| 73 | 'MailSMTPCertCheck' => '0', | ||
| 74 | 'MailSMTPHost' => '', | ||
| 75 | 'MailSMTPPort' => '25', | ||
| 76 | 'MailSMTPUsername' => '', | ||
| 77 | 'MailSMTPPassword' => '', | ||
| 78 | 'MailSMTPFromUsername' => 'PBX', | ||
| 79 | 'MailSMTPSenderAddress' => '', | ||
| 80 | 'MailEnableNotifications' => '0', | ||
| 81 | 'MailTplMissedCallSubject' => 'You have missing call from <MailSMTPSenderAddress>', | ||
| 82 | 'MailTplMissedCallBody' => 'You have missed calls (NOTIFICATION_MISSEDCAUSE) from <NOTIFICATION_CALLERID> at <NOTIFICATION_DATE>', | ||
| 83 | 'MailTplMissedCallFooter' => '', | ||
| 84 | 'MailTplVoicemailSubject' => 'VoiceMail from PBX', | ||
| 85 | 'MailTplVoicemailBody' => 'See attach', | ||
| 86 | 'MailTplVoicemailFooter' => '', | ||
| 87 | 'NTPServer' => '0.pool.ntp.org'.PHP_EOL.'1.pool.ntp.org'.PHP_EOL, | ||
| 88 | 'VoicemailNotificationsEmail' => '[email protected]', | ||
| 89 | 'VoicemailExten' => '*001', | ||
| 90 | 'PBXLanguage' => 'en-en', | ||
| 91 | 'PBXInternalExtensionLength' => '3',// Длина внутреннего номера | ||
| 92 | 'PBXRecordCalls' => '1', | ||
| 93 | 'PBXSplitAudioThread' => '0', | ||
| 94 | 'PBXCallParkingExt' => '800', | ||
| 95 | 'PBXCallParkingStartSlot' => '801', | ||
| 96 | 'PBXCallParkingEndSlot' => '820', | ||
| 97 | 'PBXFeatureAttendedTransfer' => '##', | ||
| 98 | 'PBXFeatureBlindTransfer' => '**', | ||
| 99 | 'PBXFeaturePickupExten' => '*8', | ||
| 100 | 'PBXFeatureDigitTimeout' => '2500', | ||
| 101 | 'PBXFeatureAtxferNoAnswerTimeout' => '45', | ||
| 102 | 'PBXFeatureTransferDigitTimeout' => '3', | ||
| 103 | 'PBXFirewallEnabled' => '0', | ||
| 104 | 'PBXFail2BanEnabled' => '0', | ||
| 105 | 'PBXTimezone' => 'Europe/Moscow', | ||
| 106 | 'PBXVersion' => '1', | ||
| 107 | 'WebAdminLogin' => 'admin', | ||
| 108 | 'WebAdminPassword' => 'admin', | ||
| 109 | 'WebAdminLanguage' => 'en', | ||
| 110 | 'SystemNotificationsEmail' => '[email protected]', | ||
| 111 | 'SendMetrics' => '1', | ||
| 112 | |||
| 113 | |||
| 114 | ]; | ||
| 115 | } | ||
| 116 | |||
| 117 | /** | ||
| 118 | * Returns default or saved values for all predefined keys if it exists on DB | ||
| 119 | * | ||
| 120 | * @return array | ||
| 121 | */ | ||
| 122 | public static function getAllPbxSettings(): array | ||
| 123 |     { | ||
| 124 | $arrOfDefaultValues = self::getDefaultArrayValues(); | ||
| 125 |         foreach ($arrOfDefaultValues as $key => $record) { | ||
| 126 | $arrOfDefaultValues[$key] = self::getValueByKey($key); | ||
| 127 | } | ||
| 128 | |||
| 129 | return $arrOfDefaultValues; | ||
| 130 | } | ||
| 131 | |||
| 132 | /** | ||
| 133 | * Returns default or saved value for key if it exists on DB | ||
| 134 | * | ||
| 135 | * @param $key string value key | ||
| 136 | * | ||
| 137 | * @return string | ||
| 138 | */ | ||
| 139 | public static function getValueByKey(string $key): string | ||
| 140 |     { | ||
| 141 | |||
| 142 | $result = parent::findFirstByKey($key); | ||
| 143 |         if ($result === null || $result->value === null) { | ||
| 144 | $arrOfDefaultValues = self::getDefaultArrayValues(); | ||
| 145 |             if ( ! array_key_exists($key, $arrOfDefaultValues)) { | ||
| 146 | return ''; | ||
| 147 | } | ||
| 148 | |||
| 149 | return $arrOfDefaultValues[$key]; | ||
| 150 | } | ||
| 151 | |||
| 152 | return $result->value; | ||
| 153 | } | ||
| 154 | |||
| 155 | public function initialize(): void | ||
| 156 |     { | ||
| 157 |         $this->setSource('m_PbxSettings'); | ||
| 158 | parent::initialize(); | ||
| 159 | } | ||
| 160 | |||
| 161 | /** | ||
| 162 | * Before PbxSettings entity save callback | ||
| 163 | * @return bool | ||
| 164 | */ | ||
| 165 | public function validation(): bool | ||
| 166 |     { | ||
| 167 | $validation = new Validation(); | ||
| 168 | $validation->add( | ||
| 169 | 'key', | ||
| 170 | new UniquenessValidator( | ||
| 171 | [ | ||
| 172 |                     'message' => $this->t('mo_ThisKeyMustBeUniqueForPbxSettingsModels'), | ||
| 173 | ] | ||
| 174 | ) | ||
| 175 | ); | ||
| 176 | |||
| 177 | return $this->validate($validation); | ||
| 178 | } | ||
| 179 | |||
| 180 | /** | ||
| 181 | * After PbxSettings entity save callback | ||
| 182 | */ | ||
| 183 | public function afterSave(): void | ||
| 184 |     { | ||
| 185 | parent::afterSave(); | ||
| 186 |         if ($this->itHasFirewallParametersChanges()) { | ||
| 187 | FirewallRules::updatePorts($this); | ||
| 188 | } | ||
| 189 | } | ||
| 190 | |||
| 191 | /** | ||
| 192 | * Check if this changes influence to the iptables daemon | ||
| 193 | * | ||
| 194 | * @return bool | ||
| 195 | */ | ||
| 196 | public function itHasFirewallParametersChanges(): bool | ||
| 197 |     { | ||
| 198 |         switch ($this->key) { | ||
| 199 | case 'SIPPort': | ||
| 200 | case 'RTPPortFrom': | ||
| 201 | case 'RTPPortTo': | ||
| 202 | case 'IAXPort': | ||
| 203 | case 'AMIPort': | ||
| 204 | case 'AJAMPort': | ||
| 205 | case 'AJAMPortTLS': | ||
| 206 | case 'WEBPort': | ||
| 207 | case 'WEBHTTPSPort': | ||
| 208 | case 'SSHPort': | ||
| 209 | case 'PBXFirewallEnabled': | ||
| 210 | case 'PBXFail2BanEnabled': | ||
| 211 | return true; | ||
| 212 | default: | ||
| 213 |                 if (strpos($this->key, 'FirewallSettings') !== false) { | ||
| 214 | return true; | ||
| 215 | } | ||
| 216 | } | ||
| 217 | |||
| 218 | return false; | ||
| 219 | } | ||
| 220 | |||
| 221 | /** | ||
| 222 | * Check if this changes influence to the pjsip.conf | ||
| 223 | * | ||
| 224 | * @return bool | ||
| 225 | */ | ||
| 226 | public function itHasSipParametersChanges(): bool | ||
| 227 |     { | ||
| 228 |         switch ($this->key) { | ||
| 229 | case 'SIPPort': | ||
| 230 | case 'RTPPortFrom': | ||
| 231 | case 'RTPPortTo': | ||
| 232 | case 'SIPDefaultExpiry': | ||
| 233 | case 'SIPMinExpiry': | ||
| 234 | case 'SIPMaxExpiry': | ||
| 235 | case 'PBXLanguage': | ||
| 236 | return true; | ||
| 237 | default: | ||
| 238 | return false; | ||
| 239 | } | ||
| 240 | } | ||
| 241 | |||
| 242 | /** | ||
| 243 | * Check if this changes influence to the iax2.conf | ||
| 244 | * | ||
| 245 | * @return bool | ||
| 246 | */ | ||
| 247 | public function itHasIaxParametersChanges(): bool | ||
| 248 |     { | ||
| 249 |         switch ($this->key) { | ||
| 250 | case 'IAXPort': | ||
| 251 | return true; | ||
| 252 | default: | ||
| 253 | return false; | ||
| 254 | } | ||
| 255 | } | ||
| 256 | |||
| 257 | /** | ||
| 258 | * Check if this changes influence to the manager.conf | ||
| 259 | * | ||
| 260 | * @return bool | ||
| 261 | */ | ||
| 262 | public function itHasAMIParametersChanges(): bool | ||
| 263 |     { | ||
| 264 |         switch ($this->key) { | ||
| 265 | case 'AMIPort': | ||
| 266 | case 'AJAMPort': | ||
| 267 | case 'AJAMPortTLS': | ||
| 268 | return true; | ||
| 269 | default: | ||
| 270 | return false; | ||
| 271 | } | ||
| 272 | } | ||
| 273 | |||
| 274 | /** | ||
| 275 | * Check if this changes influence to the features.conf | ||
| 276 | * | ||
| 277 | * @return bool | ||
| 278 | */ | ||
| 279 | public function itHasFeaturesSettingsChanges(): bool | ||
| 280 |     { | ||
| 281 |         switch ($this->key) { | ||
| 282 | case 'PBXLanguage': | ||
| 283 | case 'PBXInternalExtensionLength': | ||
| 284 | case 'PBXRecordCalls': | ||
| 285 | case 'PBXCallParkingExt': | ||
| 286 | case 'PBXCallParkingStartSlot': | ||
| 287 | case 'PBXCallParkingEndSlot': | ||
| 288 | case 'PBXFeatureAttendedTransfer': | ||
| 289 | case 'PBXFeatureBlindTransfer': | ||
| 290 | case 'PBXFeatureDigitTimeout': | ||
| 291 | case 'PBXFeatureAtxferNoAnswerTimeout': | ||
| 292 | case 'PBXFeatureTransferDigitTimeout': | ||
| 293 | case 'PBXFeaturePickupExten': | ||
| 294 | return true; | ||
| 295 | default: | ||
| 296 | return false; | ||
| 297 | } | ||
| 298 | } | ||
| 299 | |||
| 300 | /** | ||
| 301 | * Check if this changes influence to the SSHd daemon | ||
| 302 | * | ||
| 303 | * @return bool | ||
| 304 | */ | ||
| 305 | public function itHasSSHParametersChanges(): bool | ||
| 317 | } | ||
| 318 | } | ||
| 319 | |||
| 320 | /** | ||
| 321 | * Check if this changes influence to the Nginx daemon | ||
| 322 | * | ||
| 323 | * @return bool | ||
| 324 | */ | ||
| 325 | public function itHasWebParametersChanges(): bool | ||
| 336 | } | ||
| 337 | } | ||
| 338 | |||
| 339 | /** | ||
| 340 | * Check if this changes influence to the Crond daemon | ||
| 341 | * | ||
| 342 | * @return bool | ||
| 343 | */ | ||
| 344 | public function itHasCronParametersChanges(): bool | ||
| 345 |     { | ||
| 346 |         switch ($this->key) { | ||
| 347 | case 'RestartEveryNight': | ||
| 348 | return true; | ||
| 349 | default: | ||
| 350 | return false; | ||
| 351 | } | ||
| 352 | } | ||
| 353 | |||
| 354 | /** | ||
| 355 | * Check if this changes influence to the extensions.conf | ||
| 356 | * | ||
| 357 | * @return bool | ||
| 358 | */ | ||
| 359 | public function itHasDialplanParametersChanges(): bool | ||
| 366 | } | ||
| 367 | } | ||
| 368 | |||
| 369 | /** | ||
| 370 | * Check if this changes influence to the voicemail.conf | ||
| 371 | * | ||
| 372 | * @return bool | ||
| 373 | */ | ||
| 374 | public function itHasVoiceMailParametersChanges(): bool | ||
| 375 |     { | ||
| 376 |         switch ($this->key) { | ||
| 377 | case 'MailTplVoicemailSubject': | ||
| 378 | case 'MailTplVoicemailBody': | ||
| 379 | case 'MailSMTPSenderAddress': | ||
| 380 | case 'MailSMTPUsername': | ||
| 381 | case 'PBXTimezone': | ||
| 382 | case 'VoicemailNotificationsEmail': | ||
| 383 | case 'SystemNotificationsEmail': | ||
| 384 | return true; | ||
| 385 | default: | ||
| 386 | return false; | ||
| 387 | } | ||
| 388 | } | ||
| 389 | |||
| 390 | /** | ||
| 391 | * Check if this changes influence to translations | ||
| 392 | * | ||
| 393 | * @return bool | ||
| 394 | */ | ||
| 395 | public function itHasVisualLanguageSettings(): bool | ||
| 396 |     { | ||
| 397 |         switch ($this->key) { | ||
| 398 | case 'SSHLanguage': | ||
| 399 | case 'WebAdminLanguage': | ||
| 400 | return true; | ||
| 401 | default: | ||
| 402 | return false; | ||
| 403 | } | ||
| 404 | } | ||
| 405 | |||
| 406 | /** | ||
| 407 | * Check if this changes influence to timezone and logs | ||
| 408 | * | ||
| 409 | * @return bool | ||
| 410 | */ | ||
| 411 | public function itHasTimeZoneSettings(): bool | ||
| 412 |     { | ||
| 413 |         switch ($this->key) { | ||
| 414 | case 'PBXTimezone': | ||
| 415 | return true; | ||
| 416 | default: | ||
| 417 | return false; | ||
| 418 | } | ||
| 419 | } | ||
| 420 | |||
| 421 | /** | ||
| 422 | * Check if this changes influence to NTP daemon settings | ||
| 423 | * | ||
| 424 | * @return bool | ||
| 425 | */ | ||
| 426 | public function itHasNTPSettings(): bool | ||
| 427 |     { | ||
| 428 |         switch ($this->key) { | ||
| 429 | case 'PBXManualTimeSettings': | ||
| 430 | case 'NTPServer': | ||
| 431 | return true; | ||
| 432 | default: | ||
| 433 | return false; | ||
| 434 | } | ||
| 435 | } | ||
| 436 | |||
| 437 | /** | ||
| 438 | * Check if this changes influence to licensing | ||
| 439 | * | ||
| 440 | * @return bool | ||
| 441 | */ | ||
| 442 | public function itHasLicenseSettings(): bool | ||
| 449 | } | ||
| 450 | } | ||
| 451 | |||
| 452 | /** | ||
| 453 | * Check if this changes influence to call recording | ||
| 454 | * | ||
| 455 | * @return bool | ||
| 456 | */ | ||
| 457 | public function itHasCallRecordSettings(): bool | ||
| 465 | } | ||
| 466 | } | ||
| 467 | |||
| 468 | |||
| 469 | |||
| 470 | } |