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 |
||
| 38 | class ConfigService { |
||
| 39 | |||
| 40 | const CIRCLES_ALLOW_CIRCLES = 'allow_circles'; |
||
| 41 | const CIRCLES_CONTACT_BACKEND = 'contact_backend'; |
||
| 42 | const CIRCLES_STILL_FRONTEND = 'still_frontend'; |
||
| 43 | const CIRCLES_SWAP_TO_TEAMS = 'swap_to_teams'; |
||
| 44 | const CIRCLES_ALLOW_FEDERATED_CIRCLES = 'allow_federated'; |
||
| 45 | const CIRCLES_GS_ENABLED = 'gs_enabled'; |
||
| 46 | const CIRCLES_MEMBERS_LIMIT = 'members_limit'; |
||
| 47 | const CIRCLES_ACCOUNTS_ONLY = 'accounts_only'; |
||
| 48 | const CIRCLES_ALLOW_LINKED_GROUPS = 'allow_linked_groups'; |
||
| 49 | const CIRCLES_ALLOW_NON_SSL_LINKS = 'allow_non_ssl_links'; |
||
| 50 | const CIRCLES_NON_SSL_LOCAL = 'local_is_non_ssl'; |
||
| 51 | const CIRCLES_SELF_SIGNED = 'self_signed_cert'; |
||
| 52 | const LOCAL_CLOUD_ID = 'local_cloud_id'; |
||
| 53 | const CIRCLES_LOCAL_GSKEY = 'local_gskey'; |
||
| 54 | const CIRCLES_ACTIVITY_ON_CREATION = 'creation_activity'; |
||
| 55 | const CIRCLES_SKIP_INVITATION_STEP = 'skip_invitation_to_closed_circles'; |
||
| 56 | const CIRCLES_SEARCH_FROM_COLLABORATOR = 'search_from_collaborator'; |
||
| 57 | const CIRCLES_TEST_ASYNC_LOCK = 'test_async_lock'; |
||
| 58 | const CIRCLES_TEST_ASYNC_INIT = 'test_async_init'; |
||
| 59 | const CIRCLES_TEST_ASYNC_HAND = 'test_async_hand'; |
||
| 60 | const CIRCLES_TEST_ASYNC_COUNT = 'test_async_count'; |
||
| 61 | const FORCE_NC_BASE = 'force_nc_base'; |
||
| 62 | const TEST_NC_BASE = 'test_nc_base'; |
||
| 63 | |||
| 64 | const GS_ENABLED = 'enabled'; |
||
| 65 | const GS_MODE = 'mode'; |
||
| 66 | const GS_KEY = 'key'; |
||
| 67 | const GS_LOOKUP = 'lookup'; |
||
| 68 | |||
| 69 | const GS_LOOKUP_INSTANCES = '/instances'; |
||
| 70 | const GS_LOOKUP_USERS = '/users'; |
||
| 71 | |||
| 72 | |||
| 73 | private $defaults = [ |
||
| 74 | self::CIRCLES_ALLOW_CIRCLES => Circle::CIRCLES_ALL, |
||
| 75 | self::CIRCLES_CONTACT_BACKEND => '0', |
||
| 76 | self::CIRCLES_STILL_FRONTEND => '0', |
||
| 77 | self::CIRCLES_TEST_ASYNC_INIT => '0', |
||
| 78 | self::CIRCLES_SWAP_TO_TEAMS => '0', |
||
| 79 | self::CIRCLES_ACCOUNTS_ONLY => '0', |
||
| 80 | self::CIRCLES_MEMBERS_LIMIT => '50', |
||
| 81 | self::CIRCLES_ALLOW_LINKED_GROUPS => '0', |
||
| 82 | self::CIRCLES_ALLOW_FEDERATED_CIRCLES => '0', |
||
| 83 | self::CIRCLES_GS_ENABLED => '0', |
||
| 84 | self::CIRCLES_LOCAL_GSKEY => '', |
||
| 85 | self::CIRCLES_ALLOW_NON_SSL_LINKS => '0', |
||
| 86 | self::CIRCLES_NON_SSL_LOCAL => '0', |
||
| 87 | self::CIRCLES_SELF_SIGNED => '0', |
||
| 88 | self::LOCAL_CLOUD_ID => '', |
||
| 89 | self::FORCE_NC_BASE => '', |
||
| 90 | self::CIRCLES_ACTIVITY_ON_CREATION => '1', |
||
| 91 | self::CIRCLES_SKIP_INVITATION_STEP => '0', |
||
| 92 | self::CIRCLES_SEARCH_FROM_COLLABORATOR => '0' |
||
| 93 | ]; |
||
| 94 | |||
| 95 | /** @var string */ |
||
| 96 | private $appName; |
||
| 97 | |||
| 98 | /** @var IConfig */ |
||
| 99 | private $config; |
||
| 100 | |||
| 101 | /** @var string */ |
||
| 102 | private $userId; |
||
| 103 | |||
| 104 | /** @var IRequest */ |
||
| 105 | private $request; |
||
| 106 | |||
| 107 | /** @var IURLGenerator */ |
||
| 108 | private $urlGenerator; |
||
| 109 | |||
| 110 | /** @var MiscService */ |
||
| 111 | private $miscService; |
||
| 112 | |||
| 113 | /** @var int */ |
||
| 114 | private $allowedCircle = -1; |
||
| 115 | |||
| 116 | /** @var int */ |
||
| 117 | private $allowedLinkedGroups = -1; |
||
| 118 | |||
| 119 | /** @var int */ |
||
| 120 | private $allowedFederatedCircles = -1; |
||
| 121 | |||
| 122 | /** @var int */ |
||
| 123 | private $allowedNonSSLLinks = -1; |
||
| 124 | |||
| 125 | /** @var int */ |
||
| 126 | private $localNonSSL = -1; |
||
| 127 | |||
| 128 | /** |
||
| 129 | * ConfigService constructor. |
||
| 130 | * |
||
| 131 | * @param string $appName |
||
| 132 | * @param IConfig $config |
||
| 133 | * @param IRequest $request |
||
| 134 | * @param string $userId |
||
| 135 | * @param IURLGenerator $urlGenerator |
||
| 136 | * @param MiscService $miscService |
||
| 137 | */ |
||
| 138 | public function __construct( |
||
| 149 | |||
| 150 | |||
| 151 | /** |
||
| 152 | * @return string |
||
| 153 | * @deprecated |
||
| 154 | */ |
||
| 155 | public function getLocalAddress() { |
||
| 159 | |||
| 160 | |||
| 161 | /** |
||
| 162 | * returns if this type of circle is allowed by the current configuration. |
||
| 163 | * |
||
| 164 | * @param $type |
||
| 165 | * |
||
| 166 | * @return int |
||
| 167 | */ |
||
| 168 | public function isCircleAllowed($type) { |
||
| 175 | |||
| 176 | |||
| 177 | /** |
||
| 178 | * @return bool |
||
| 179 | * @throws GSStatusException |
||
| 180 | */ |
||
| 181 | public function isLinkedGroupsAllowed() { |
||
| 190 | |||
| 191 | |||
| 192 | /** |
||
| 193 | * @return bool |
||
| 194 | */ |
||
| 195 | public function isFederatedCirclesAllowed() { |
||
| 203 | |||
| 204 | /** |
||
| 205 | * @return bool |
||
| 206 | */ |
||
| 207 | public function isInvitationSkipped() { |
||
| 210 | |||
| 211 | /** |
||
| 212 | * @return bool |
||
| 213 | */ |
||
| 214 | public function isLocalNonSSL() { |
||
| 222 | |||
| 223 | |||
| 224 | /** |
||
| 225 | * @return bool |
||
| 226 | */ |
||
| 227 | public function isNonSSLLinksAllowed() { |
||
| 235 | |||
| 236 | |||
| 237 | /** |
||
| 238 | * @param string $remote |
||
| 239 | * |
||
| 240 | * @return string |
||
| 241 | */ |
||
| 242 | public function generateRemoteHost($remote) { |
||
| 251 | |||
| 252 | |||
| 253 | /** |
||
| 254 | * Get a value by key |
||
| 255 | * |
||
| 256 | * @param string $key |
||
| 257 | * |
||
| 258 | * @return string |
||
| 259 | */ |
||
| 260 | public function getCoreValue($key) { |
||
| 265 | |||
| 266 | /** |
||
| 267 | * Get a value by key |
||
| 268 | * |
||
| 269 | * @param string $key |
||
| 270 | * |
||
| 271 | * @return string |
||
| 272 | */ |
||
| 273 | public function getSystemValue($key) { |
||
| 278 | |||
| 279 | |||
| 280 | /** |
||
| 281 | * Get available hosts |
||
| 282 | * |
||
| 283 | * @return array |
||
| 284 | */ |
||
| 285 | public function getAvailableHosts(): array { |
||
| 288 | |||
| 289 | |||
| 290 | /** |
||
| 291 | * Get a value by key |
||
| 292 | * |
||
| 293 | * @param string $key |
||
| 294 | * |
||
| 295 | * @return string |
||
| 296 | */ |
||
| 297 | public function getAppValue($key) { |
||
| 306 | |||
| 307 | /** |
||
| 308 | * Set a value by key |
||
| 309 | * |
||
| 310 | * @param string $key |
||
| 311 | * @param string $value |
||
| 312 | * |
||
| 313 | * @return void |
||
| 314 | */ |
||
| 315 | public function setAppValue($key, $value) { |
||
| 318 | |||
| 319 | /** |
||
| 320 | * remove a key |
||
| 321 | * |
||
| 322 | * @param string $key |
||
| 323 | * |
||
| 324 | * @return string |
||
| 325 | */ |
||
| 326 | public function deleteAppValue($key) { |
||
| 329 | |||
| 330 | /** |
||
| 331 | * Get a user value by key |
||
| 332 | * |
||
| 333 | * @param string $key |
||
| 334 | * |
||
| 335 | * @return string |
||
| 336 | */ |
||
| 337 | public function getUserValue($key) { |
||
| 340 | |||
| 341 | /** |
||
| 342 | * Set a user value by key |
||
| 343 | * |
||
| 344 | * @param string $key |
||
| 345 | * @param string $value |
||
| 346 | * |
||
| 347 | * @return string |
||
| 348 | * @throws PreConditionNotMetException |
||
| 349 | */ |
||
| 350 | public function setUserValue($key, $value) { |
||
| 353 | |||
| 354 | |||
| 355 | /** |
||
| 356 | * Get a user value by key and user |
||
| 357 | * |
||
| 358 | * @param string $userId |
||
| 359 | * @param string $key |
||
| 360 | * |
||
| 361 | * @param string $default |
||
| 362 | * |
||
| 363 | * @return string |
||
| 364 | */ |
||
| 365 | public function getCoreValueForUser($userId, $key, $default = '') { |
||
| 368 | |||
| 369 | |||
| 370 | /** |
||
| 371 | * Get a user value by key and user |
||
| 372 | * |
||
| 373 | * @param string $userId |
||
| 374 | * @param string $key |
||
| 375 | * |
||
| 376 | * @return string |
||
| 377 | */ |
||
| 378 | public function getValueForUser($userId, $key) { |
||
| 381 | |||
| 382 | /** |
||
| 383 | * Set a user value by key |
||
| 384 | * |
||
| 385 | * @param string $userId |
||
| 386 | * @param string $key |
||
| 387 | * @param string $value |
||
| 388 | * |
||
| 389 | * @return string |
||
| 390 | * @throws PreConditionNotMetException |
||
| 391 | */ |
||
| 392 | public function setValueForUser($userId, $key, $value) { |
||
| 395 | |||
| 396 | /** |
||
| 397 | * return the cloud version. |
||
| 398 | * if $complete is true, return a string x.y.z |
||
| 399 | * |
||
| 400 | * @param boolean $complete |
||
| 401 | * |
||
| 402 | * @return string|integer |
||
| 403 | */ |
||
| 404 | public function getCloudVersion($complete = false) { |
||
| 413 | |||
| 414 | |||
| 415 | /** |
||
| 416 | * @return bool |
||
| 417 | */ |
||
| 418 | public function isAccountOnly() { |
||
| 421 | |||
| 422 | |||
| 423 | /** |
||
| 424 | * @return bool |
||
| 425 | */ |
||
| 426 | public function isContactsBackend(): bool { |
||
| 430 | |||
| 431 | |||
| 432 | /** |
||
| 433 | * @return int |
||
| 434 | */ |
||
| 435 | public function contactsBackendType(): int { |
||
| 438 | |||
| 439 | |||
| 440 | /** |
||
| 441 | * @return bool |
||
| 442 | */ |
||
| 443 | public function stillFrontEnd(): bool { |
||
| 454 | |||
| 455 | |||
| 456 | /** |
||
| 457 | * should the password for a mail share be send to the recipient |
||
| 458 | * |
||
| 459 | * @return bool |
||
| 460 | */ |
||
| 461 | public function sendPasswordByMail() { |
||
| 468 | |||
| 469 | /** |
||
| 470 | * do we require a share by mail to be password protected |
||
| 471 | * |
||
| 472 | * @param Circle $circle |
||
| 473 | * |
||
| 474 | * @return bool |
||
| 475 | */ |
||
| 476 | public function enforcePasswordProtection(Circle $circle) { |
||
| 487 | |||
| 488 | |||
| 489 | /** |
||
| 490 | * @param string $type |
||
| 491 | * |
||
| 492 | * @return array|bool|mixed |
||
| 493 | * @throws GSStatusException |
||
| 494 | */ |
||
| 495 | public function getGSStatus(string $type = '') { |
||
| 531 | |||
| 532 | |||
| 533 | /** |
||
| 534 | * @return array |
||
| 535 | */ |
||
| 536 | public function getTrustedDomains(): array { |
||
| 539 | |||
| 540 | |||
| 541 | /** |
||
| 542 | * - returns host+port, does not specify any protocol |
||
| 543 | * - can be forced using LOCAL_CLOUD_ID |
||
| 544 | * - use 'overwrite.cli.url' |
||
| 545 | * - can use the first entry from trusted_domains is LOCAL_CLOUD_ID = 'use-trusted-domain' |
||
| 546 | * - used mainly to assign instance and source to a request |
||
| 547 | * - important only in remote environment; can be totally random in a jailed environment |
||
| 548 | * |
||
| 549 | * @return string |
||
| 550 | */ |
||
| 551 | public function getLocalInstance(): string { |
||
| 567 | |||
| 568 | |||
| 569 | /** |
||
| 570 | * @param string $instance |
||
| 571 | * |
||
| 572 | * @return bool |
||
| 573 | */ |
||
| 574 | public function isLocalInstance(string $instance): bool { |
||
| 585 | |||
| 586 | |||
| 587 | /** |
||
| 588 | * @param NC19Request $request |
||
| 589 | * @param string $routeName |
||
| 590 | * @param array $args |
||
| 591 | */ |
||
| 592 | public function configureRequest(NC19Request $request, string $routeName = '', array $args = []) { |
||
| 601 | |||
| 602 | /** |
||
| 603 | * - Create route using overwrite.cli.url. |
||
| 604 | * - can be forced using FORCE_NC_BASE or TEST_BC_BASE (temporary) |
||
| 605 | * - perfect for loopback request. |
||
| 606 | * |
||
| 607 | * @param NC19Request $request |
||
| 608 | * @param string $routeName |
||
| 609 | * @param array $args |
||
| 610 | * |
||
| 611 | * @return string |
||
| 612 | */ |
||
| 613 | private function configureRequestAddress(NC19Request $request, string $routeName, array $args = []) { |
||
| 629 | |||
| 630 | } |
||
| 631 | |||
| 632 |