Complex classes like ConfigService 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 ConfigService, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 53 | class ConfigService { |
||
| 54 | |||
| 55 | |||
| 56 | use TStringTools; |
||
| 57 | use TArrayTools; |
||
| 58 | |||
| 59 | |||
| 60 | const FRONTAL_CLOUD_ID = 'frontal_cloud_id'; |
||
| 61 | const FRONTAL_CLOUD_SCHEME = 'frontal_cloud_scheme'; |
||
| 62 | const INTERNAL_CLOUD_ID = 'internal_cloud_id'; |
||
| 63 | const INTERNAL_CLOUD_SCHEME = 'internal_cloud_scheme'; |
||
| 64 | const LOOPBACK_CLOUD_ID = 'loopback_cloud_id'; |
||
| 65 | const LOOPBACK_CLOUD_SCHEME = 'loopback_cloud_scheme'; |
||
| 66 | const IFACE0_CLOUD_ID = 'iface0_cloud_id'; |
||
| 67 | const IFACE0_CLOUD_SCHEME = 'iface0_cloud_scheme'; |
||
| 68 | const IFACE1_CLOUD_ID = 'iface1_cloud_id'; |
||
| 69 | const IFACE1_CLOUD_SCHEME = 'iface1_cloud_scheme'; |
||
| 70 | const IFACE2_CLOUD_ID = 'iface2_cloud_id'; |
||
| 71 | const IFACE2_CLOUD_SCHEME = 'iface2_cloud_scheme'; |
||
| 72 | const IFACE3_CLOUD_ID = 'iface3_cloud_id'; |
||
| 73 | const IFACE3_CLOUD_SCHEME = 'iface3_cloud_scheme'; |
||
| 74 | const IFACE4_CLOUD_ID = 'iface4_cloud_id'; |
||
| 75 | const IFACE4_CLOUD_SCHEME = 'iface4_cloud_scheme'; |
||
| 76 | const IFACE_TEST_ID = 'iface_test_id'; |
||
| 77 | const IFACE_TEST_SCHEME = 'iface_test_scheme'; |
||
| 78 | const IFACE_TEST_TOKEN = 'iface_test_token'; |
||
| 79 | |||
| 80 | const HARD_MODERATION = 'hard_moderation'; |
||
| 81 | const ROUTE_TO_CIRCLE = 'route_to_circle'; |
||
| 82 | const EVENT_EXAMPLES = 'event_examples'; |
||
| 83 | |||
| 84 | const SELF_SIGNED_CERT = 'self_signed_cert'; |
||
| 85 | const MEMBERS_LIMIT = 'members_limit'; |
||
| 86 | const ACTIVITY_ON_NEW_CIRCLE = 'creation_activity'; |
||
| 87 | const MIGRATION_22 = 'migration_22'; |
||
| 88 | |||
| 89 | const LOOPBACK_TMP_ID = 'loopback_tmp_id'; |
||
| 90 | const LOOPBACK_TMP_SCHEME = 'loopback_tmp_scheme'; |
||
| 91 | |||
| 92 | const GS_MODE = 'mode'; |
||
| 93 | const GS_KEY = 'key'; |
||
| 94 | |||
| 95 | const GS_LOOKUP_INSTANCES = '/instances'; |
||
| 96 | const GS_LOOKUP_USERS = '/users'; |
||
| 97 | |||
| 98 | |||
| 99 | // deprecated -- removing in NC25 |
||
| 100 | const CIRCLES_CONTACT_BACKEND = 'contact_backend'; |
||
| 101 | const CIRCLES_ACCOUNTS_ONLY = 'accounts_only'; // only UserType=1 |
||
| 102 | const CIRCLES_SEARCH_FROM_COLLABORATOR = 'search_from_collaborator'; |
||
| 103 | |||
| 104 | const FORCE_NC_BASE = 'force_nc_base'; |
||
| 105 | const TEST_NC_BASE = 'test_nc_base'; |
||
| 106 | |||
| 107 | |||
| 108 | private $defaults = [ |
||
| 109 | self::FRONTAL_CLOUD_ID => '', |
||
| 110 | self::FRONTAL_CLOUD_SCHEME => 'https', |
||
| 111 | self::INTERNAL_CLOUD_ID => '', |
||
| 112 | self::INTERNAL_CLOUD_SCHEME => 'https', |
||
| 113 | self::LOOPBACK_CLOUD_ID => '', |
||
| 114 | self::LOOPBACK_CLOUD_SCHEME => 'https', |
||
| 115 | self::LOOPBACK_TMP_ID => '', |
||
| 116 | self::LOOPBACK_TMP_SCHEME => '', |
||
| 117 | self::IFACE0_CLOUD_ID => '', |
||
| 118 | self::IFACE0_CLOUD_SCHEME => 'https', |
||
| 119 | self::IFACE1_CLOUD_ID => '', |
||
| 120 | self::IFACE1_CLOUD_SCHEME => 'https', |
||
| 121 | self::IFACE2_CLOUD_ID => '', |
||
| 122 | self::IFACE2_CLOUD_SCHEME => 'https', |
||
| 123 | self::IFACE3_CLOUD_ID => '', |
||
| 124 | self::IFACE3_CLOUD_SCHEME => 'https', |
||
| 125 | self::IFACE4_CLOUD_ID => '', |
||
| 126 | self::IFACE4_CLOUD_SCHEME => 'https', |
||
| 127 | self::IFACE_TEST_ID => '', |
||
| 128 | self::IFACE_TEST_SCHEME => 'https', |
||
| 129 | self::IFACE_TEST_TOKEN => '', |
||
| 130 | |||
| 131 | self::HARD_MODERATION => '0', |
||
| 132 | self::ROUTE_TO_CIRCLE => 'contacts.contacts.directcircle', |
||
| 133 | self::EVENT_EXAMPLES => '0', |
||
| 134 | |||
| 135 | self::SELF_SIGNED_CERT => '0', |
||
| 136 | self::MEMBERS_LIMIT => '50', |
||
| 137 | self::ACTIVITY_ON_NEW_CIRCLE => '1', |
||
| 138 | self::MIGRATION_22 => '0', |
||
| 139 | |||
| 140 | self::FORCE_NC_BASE => '', |
||
| 141 | self::TEST_NC_BASE => '', |
||
| 142 | self::CIRCLES_CONTACT_BACKEND => '0', |
||
| 143 | self::CIRCLES_ACCOUNTS_ONLY => '0', |
||
| 144 | self::CIRCLES_SEARCH_FROM_COLLABORATOR => '0', |
||
| 145 | ]; |
||
| 146 | |||
| 147 | |||
| 148 | /** @var IConfig */ |
||
| 149 | private $config; |
||
| 150 | |||
| 151 | /** @var IURLGenerator */ |
||
| 152 | private $urlGenerator; |
||
| 153 | |||
| 154 | |||
| 155 | /** |
||
| 156 | * ConfigService constructor. |
||
| 157 | * |
||
| 158 | * @param IConfig $config |
||
| 159 | * @param IURLGenerator $urlGenerator |
||
| 160 | */ |
||
| 161 | public function __construct(IConfig $config, IURLGenerator $urlGenerator) { |
||
| 165 | |||
| 166 | |||
| 167 | /** |
||
| 168 | * Get a value by key |
||
| 169 | * |
||
| 170 | * @param string $key |
||
| 171 | * |
||
| 172 | * @return string |
||
| 173 | */ |
||
| 174 | public function getAppValue(string $key): string { |
||
| 185 | |||
| 186 | /** |
||
| 187 | * @param string $key |
||
| 188 | * |
||
| 189 | * @return int |
||
| 190 | */ |
||
| 191 | public function getAppValueInt(string $key): int { |
||
| 194 | |||
| 195 | /** |
||
| 196 | * @param string $key |
||
| 197 | * |
||
| 198 | * @return bool |
||
| 199 | */ |
||
| 200 | public function getAppValueBool(string $key): bool { |
||
| 203 | |||
| 204 | |||
| 205 | /** |
||
| 206 | * Set a value by key |
||
| 207 | * |
||
| 208 | * @param string $key |
||
| 209 | * @param string $value |
||
| 210 | * |
||
| 211 | * @return void |
||
| 212 | */ |
||
| 213 | public function setAppValue(string $key, string $value): void { |
||
| 216 | |||
| 217 | |||
| 218 | /** |
||
| 219 | * |
||
| 220 | */ |
||
| 221 | public function unsetAppConfig(): void { |
||
| 224 | |||
| 225 | |||
| 226 | /** |
||
| 227 | * Get available hosts |
||
| 228 | * |
||
| 229 | * @return array |
||
| 230 | */ |
||
| 231 | public function getAvailableHosts(): array { |
||
| 234 | |||
| 235 | |||
| 236 | /** |
||
| 237 | * Get a user value by key and user |
||
| 238 | * |
||
| 239 | * |
||
| 240 | * @param string $userId |
||
| 241 | * @param string $key |
||
| 242 | * |
||
| 243 | * @param string $default |
||
| 244 | * |
||
| 245 | * @return string |
||
| 246 | */ |
||
| 247 | public function getCoreValueForUser($userId, $key, $default = '') { |
||
| 250 | |||
| 251 | |||
| 252 | /** |
||
| 253 | * @return bool |
||
| 254 | * @deprecated |
||
| 255 | */ |
||
| 256 | public function isContactsBackend(): bool { |
||
| 260 | |||
| 261 | |||
| 262 | /** |
||
| 263 | * @return int |
||
| 264 | * @deprecated |
||
| 265 | */ |
||
| 266 | public function contactsBackendType(): int { |
||
| 269 | |||
| 270 | |||
| 271 | /** |
||
| 272 | * @return bool |
||
| 273 | * @deprecated |
||
| 274 | * should the password for a mail share be send to the recipient |
||
| 275 | * |
||
| 276 | */ |
||
| 277 | public function sendPasswordByMail() { |
||
| 284 | |||
| 285 | /** |
||
| 286 | * @param DeprecatedCircle $circle |
||
| 287 | * |
||
| 288 | * @return bool |
||
| 289 | * @deprecated |
||
| 290 | * do we require a share by mail to be password protected |
||
| 291 | * |
||
| 292 | */ |
||
| 293 | public function enforcePasswordProtection(DeprecatedCircle $circle) { |
||
| 304 | |||
| 305 | |||
| 306 | /** |
||
| 307 | * // TODO: fetch data from somewhere else than hard coded... |
||
| 308 | * |
||
| 309 | * @return array |
||
| 310 | */ |
||
| 311 | public function getSettings(): array { |
||
| 318 | |||
| 319 | |||
| 320 | /** |
||
| 321 | * @return bool |
||
| 322 | */ |
||
| 323 | public function isGSAvailable(): bool { |
||
| 330 | |||
| 331 | |||
| 332 | /** |
||
| 333 | * @return string |
||
| 334 | * @throws GSStatusException |
||
| 335 | */ |
||
| 336 | public function getGSLookup(): string { |
||
| 345 | |||
| 346 | |||
| 347 | /** |
||
| 348 | * @return array |
||
| 349 | */ |
||
| 350 | public function getGSSMockup(): array { |
||
| 353 | |||
| 354 | |||
| 355 | /** |
||
| 356 | * @param string $type |
||
| 357 | * |
||
| 358 | * @return string |
||
| 359 | */ |
||
| 360 | public function getGSInfo(string $type): string { |
||
| 375 | |||
| 376 | |||
| 377 | /** |
||
| 378 | * @return array |
||
| 379 | * @throws GSStatusException |
||
| 380 | */ |
||
| 381 | public function getGSData(): array { |
||
| 390 | |||
| 391 | |||
| 392 | /** |
||
| 393 | * @return array |
||
| 394 | */ |
||
| 395 | public function getTrustedDomains(): array { |
||
| 402 | |||
| 403 | |||
| 404 | /** |
||
| 405 | * @return string |
||
| 406 | */ |
||
| 407 | public function getLoopbackInstance(): string { |
||
| 445 | |||
| 446 | /** |
||
| 447 | * returns loopback address based on getLoopbackInstance and LOOPBACK_CLOUD_SCHEME |
||
| 448 | * should be used to async process |
||
| 449 | * |
||
| 450 | * @param string $route |
||
| 451 | * @param array $args |
||
| 452 | * |
||
| 453 | * @return string |
||
| 454 | */ |
||
| 455 | public function getLoopbackPath(string $route = '', array $args = []): string { |
||
| 469 | |||
| 470 | |||
| 471 | /** |
||
| 472 | * - must be configured using INTERNAL_CLOUD_ID |
||
| 473 | * - returns host+port, does not specify any protocol |
||
| 474 | * - used mainly to assign instance and source to a request to local GlobalScale |
||
| 475 | * - important only in GlobalScale environment |
||
| 476 | * |
||
| 477 | * @return string |
||
| 478 | * |
||
| 479 | */ |
||
| 480 | public function getInternalInstance(): string { |
||
| 483 | |||
| 484 | |||
| 485 | /** |
||
| 486 | * - must be configured using FRONTAL_CLOUD_ID |
||
| 487 | * - returns host+port, does not specify any protocol |
||
| 488 | * - used mainly to assign instance and source to a request |
||
| 489 | * - important only in remote environment |
||
| 490 | * |
||
| 491 | * @return string |
||
| 492 | */ |
||
| 493 | public function getFrontalInstance(): string { |
||
| 506 | |||
| 507 | |||
| 508 | /** |
||
| 509 | * @param int $iface |
||
| 510 | * |
||
| 511 | * @return string |
||
| 512 | */ |
||
| 513 | public function getIfaceInstance(int $iface): string { |
||
| 529 | |||
| 530 | |||
| 531 | /** |
||
| 532 | * @param string $instance |
||
| 533 | * |
||
| 534 | * @return bool |
||
| 535 | */ |
||
| 536 | public function isLocalInstance(string $instance): bool { |
||
| 556 | |||
| 557 | |||
| 558 | /** |
||
| 559 | * @param IFederatedUser $federatedUser |
||
| 560 | * |
||
| 561 | * @return string |
||
| 562 | */ |
||
| 563 | public function displayFederatedUser(IFederatedUser $federatedUser): string { |
||
| 566 | |||
| 567 | /** |
||
| 568 | * @param string $instance |
||
| 569 | * @param bool $showAt |
||
| 570 | * |
||
| 571 | * @return string |
||
| 572 | */ |
||
| 573 | public function displayInstance(string $instance, bool $showAt = false): string { |
||
| 580 | |||
| 581 | |||
| 582 | /** |
||
| 583 | * - Create route using getLoopbackAddress() |
||
| 584 | * - perfect for loopback request. |
||
| 585 | * |
||
| 586 | * @param NC22Request $request |
||
| 587 | * @param string $route |
||
| 588 | * @param array $args |
||
| 589 | */ |
||
| 590 | public function configureLoopbackRequest( |
||
| 599 | |||
| 600 | |||
| 601 | /** |
||
| 602 | * @param NC22Request $request |
||
| 603 | */ |
||
| 604 | public function configureRequest(NC22Request $request): void { |
||
| 612 | |||
| 613 | } |
||
| 614 | |||
| 615 |
This method has been deprecated.