Complex classes like SyncService 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 SyncService, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 81 | class SyncService { |
||
| 82 | |||
| 83 | |||
| 84 | use TStringTools; |
||
| 85 | use TNC22Logger; |
||
| 86 | |||
| 87 | |||
| 88 | const SYNC_APPS = 1; |
||
| 89 | const SYNC_USERS = 2; |
||
| 90 | const SYNC_GROUPS = 4; |
||
| 91 | const SYNC_GLOBALSCALE = 8; |
||
| 92 | const SYNC_REMOTES = 16; |
||
| 93 | const SYNC_CONTACTS = 32; |
||
| 94 | const SYNC_ALL = 63; |
||
| 95 | |||
| 96 | |||
| 97 | /** @var IUserManager */ |
||
| 98 | private $userManager; |
||
| 99 | |||
| 100 | /** @var IGroupManager */ |
||
| 101 | private $groupManager; |
||
| 102 | |||
| 103 | /** @var IDBConnection */ |
||
| 104 | private $dbConnection; |
||
| 105 | |||
| 106 | /** @var CircleRequest */ |
||
| 107 | private $circleRequest; |
||
| 108 | |||
| 109 | /** @var MemberRequest */ |
||
| 110 | private $memberRequest; |
||
| 111 | |||
| 112 | /** @var FederatedUserService */ |
||
| 113 | private $federatedUserService; |
||
| 114 | |||
| 115 | /** @var federatedEventService */ |
||
| 116 | private $federatedEventService; |
||
| 117 | |||
| 118 | /** @var CircleService */ |
||
| 119 | private $circleService; |
||
| 120 | |||
| 121 | /** @var MemberService */ |
||
| 122 | private $memberService; |
||
| 123 | |||
| 124 | /** @var MembershipService */ |
||
| 125 | private $membershipService; |
||
| 126 | |||
| 127 | /** @var TimezoneService */ |
||
| 128 | private $timezoneService; |
||
| 129 | |||
| 130 | /** @var ConfigService */ |
||
| 131 | private $configService; |
||
| 132 | |||
| 133 | |||
| 134 | /** @var IOutput */ |
||
| 135 | private $migrationOutput; |
||
| 136 | |||
| 137 | /** @var OutputInterface */ |
||
| 138 | private $occOutput; |
||
| 139 | |||
| 140 | |||
| 141 | /** |
||
| 142 | * SyncService constructor. |
||
| 143 | * |
||
| 144 | * @param IUserManager $userManager |
||
| 145 | * @param IGroupManager $groupManager |
||
| 146 | * @param IDBConnection $dbConnection |
||
| 147 | * @param CircleRequest $circleRequest |
||
| 148 | * @param MemberRequest $memberRequest |
||
| 149 | * @param FederatedUserService $federatedUserService |
||
| 150 | * @param federatedEventService $federatedEventService |
||
| 151 | * @param CircleService $circleService |
||
| 152 | * @param MemberService $memberService |
||
| 153 | * @param MembershipService $membershipService |
||
| 154 | * @param TimezoneService $timezoneService |
||
| 155 | * @param ConfigService $configService |
||
| 156 | */ |
||
| 157 | public function __construct( |
||
| 186 | |||
| 187 | |||
| 188 | /** |
||
| 189 | * @param OutputInterface $output |
||
| 190 | */ |
||
| 191 | public function setOccOutput(OutputInterface $output): void { |
||
| 194 | |||
| 195 | /** |
||
| 196 | * @param IOutput $output |
||
| 197 | */ |
||
| 198 | public function setMigrationOutput(IOutput $output): void { |
||
| 201 | |||
| 202 | |||
| 203 | /** |
||
| 204 | * @param int $sync |
||
| 205 | * |
||
| 206 | * @return void |
||
| 207 | */ |
||
| 208 | public function sync(int $sync = self::SYNC_ALL): void { |
||
| 241 | |||
| 242 | |||
| 243 | /** |
||
| 244 | * @param int $item |
||
| 245 | * @param int $all |
||
| 246 | * |
||
| 247 | * @return bool |
||
| 248 | */ |
||
| 249 | private function shouldSync(int $item, int $all): bool { |
||
| 252 | |||
| 253 | |||
| 254 | /** |
||
| 255 | */ |
||
| 256 | public function syncApps(): void { |
||
| 266 | |||
| 267 | |||
| 268 | /** |
||
| 269 | * @return void |
||
| 270 | */ |
||
| 271 | public function syncNextcloudUsers(): void { |
||
| 281 | |||
| 282 | /** |
||
| 283 | * @param string $userId |
||
| 284 | * |
||
| 285 | * @return FederatedUser |
||
| 286 | * @throws ContactAddressBookNotFoundException |
||
| 287 | * @throws ContactFormatException |
||
| 288 | * @throws ContactNotFoundException |
||
| 289 | * @throws FederatedUserException |
||
| 290 | * @throws FederatedUserNotFoundException |
||
| 291 | * @throws InvalidIdException |
||
| 292 | * @throws RequestBuilderException |
||
| 293 | * @throws SingleCircleNotFoundException |
||
| 294 | */ |
||
| 295 | public function syncNextcloudUser(string $userId): FederatedUser { |
||
| 298 | |||
| 299 | |||
| 300 | /** |
||
| 301 | * @return void |
||
| 302 | */ |
||
| 303 | public function syncNextcloudGroups(): void { |
||
| 313 | |||
| 314 | /** |
||
| 315 | * @param string $groupId |
||
| 316 | * |
||
| 317 | * @return Circle |
||
| 318 | * @throws FederatedUserException |
||
| 319 | * @throws FederatedUserNotFoundException |
||
| 320 | * @throws GroupNotFoundException |
||
| 321 | * @throws InvalidIdException |
||
| 322 | * @throws SingleCircleNotFoundException |
||
| 323 | * @throws FederatedEventException |
||
| 324 | * @throws FederatedItemException |
||
| 325 | * @throws InitiatorNotConfirmedException |
||
| 326 | * @throws OwnerNotFoundException |
||
| 327 | * @throws RemoteInstanceException |
||
| 328 | * @throws RemoteNotFoundException |
||
| 329 | * @throws RemoteResourceNotFoundException |
||
| 330 | * @throws UnknownRemoteException |
||
| 331 | * @throws RequestBuilderException |
||
| 332 | */ |
||
| 333 | public function syncNextcloudGroup(string $groupId): Circle { |
||
| 350 | |||
| 351 | |||
| 352 | /** |
||
| 353 | * @param string $userId |
||
| 354 | * |
||
| 355 | * @throws ContactAddressBookNotFoundException |
||
| 356 | * @throws ContactFormatException |
||
| 357 | * @throws ContactNotFoundException |
||
| 358 | * @throws FederatedUserException |
||
| 359 | * @throws FederatedUserNotFoundException |
||
| 360 | * @throws InvalidIdException |
||
| 361 | * @throws RequestBuilderException |
||
| 362 | */ |
||
| 363 | public function userDeleted(string $userId): void { |
||
| 386 | |||
| 387 | |||
| 388 | /** |
||
| 389 | * @param string $groupId |
||
| 390 | * |
||
| 391 | * @throws ContactAddressBookNotFoundException |
||
| 392 | * @throws ContactFormatException |
||
| 393 | * @throws ContactNotFoundException |
||
| 394 | * @throws FederatedUserException |
||
| 395 | * @throws InvalidIdException |
||
| 396 | * @throws RequestBuilderException |
||
| 397 | * @throws SingleCircleNotFoundException |
||
| 398 | */ |
||
| 399 | public function groupDeleted(string $groupId): void { |
||
| 428 | |||
| 429 | |||
| 430 | /** |
||
| 431 | * @param Circle $circle |
||
| 432 | * @param string $userId |
||
| 433 | * |
||
| 434 | * @return Member |
||
| 435 | * @throws ContactAddressBookNotFoundException |
||
| 436 | * @throws ContactFormatException |
||
| 437 | * @throws ContactNotFoundException |
||
| 438 | * @throws FederatedUserException |
||
| 439 | * @throws FederatedUserNotFoundException |
||
| 440 | * @throws InvalidIdException |
||
| 441 | * @throws RequestBuilderException |
||
| 442 | * @throws SingleCircleNotFoundException |
||
| 443 | */ |
||
| 444 | private function generateGroupMember(Circle $circle, string $userId): Member { |
||
| 457 | |||
| 458 | |||
| 459 | /** |
||
| 460 | * @param string $groupId |
||
| 461 | * @param string $userId |
||
| 462 | * |
||
| 463 | * @return Member |
||
| 464 | * @throws FederatedEventException |
||
| 465 | * @throws FederatedItemException |
||
| 466 | * @throws FederatedUserException |
||
| 467 | * @throws FederatedUserNotFoundException |
||
| 468 | * @throws GroupNotFoundException |
||
| 469 | * @throws InitiatorNotConfirmedException |
||
| 470 | * @throws InvalidIdException |
||
| 471 | * @throws OwnerNotFoundException |
||
| 472 | * @throws RemoteInstanceException |
||
| 473 | * @throws RemoteNotFoundException |
||
| 474 | * @throws RemoteResourceNotFoundException |
||
| 475 | * @throws RequestBuilderException |
||
| 476 | * @throws SingleCircleNotFoundException |
||
| 477 | * @throws UnknownRemoteException |
||
| 478 | */ |
||
| 479 | public function groupMemberAdded(string $groupId, string $userId): void { |
||
| 492 | |||
| 493 | |||
| 494 | /** |
||
| 495 | * @param string $groupId |
||
| 496 | * @param string $userId |
||
| 497 | * |
||
| 498 | * @throws FederatedEventException |
||
| 499 | * @throws FederatedItemException |
||
| 500 | * @throws FederatedUserException |
||
| 501 | * @throws FederatedUserNotFoundException |
||
| 502 | * @throws GroupNotFoundException |
||
| 503 | * @throws InitiatorNotConfirmedException |
||
| 504 | * @throws InvalidIdException |
||
| 505 | * @throws OwnerNotFoundException |
||
| 506 | * @throws RemoteInstanceException |
||
| 507 | * @throws RemoteNotFoundException |
||
| 508 | * @throws RemoteResourceNotFoundException |
||
| 509 | * @throws RequestBuilderException |
||
| 510 | * @throws SingleCircleNotFoundException |
||
| 511 | * @throws UnknownRemoteException |
||
| 512 | */ |
||
| 513 | public function groupMemberRemoved(string $groupId, string $userId): void { |
||
| 520 | |||
| 521 | |||
| 522 | /** |
||
| 523 | * @return void |
||
| 524 | */ |
||
| 525 | public function syncContacts(): void { |
||
| 528 | |||
| 529 | |||
| 530 | /** |
||
| 531 | * @return void |
||
| 532 | */ |
||
| 533 | public function syncGlobalScale(): void { |
||
| 536 | |||
| 537 | |||
| 538 | /** |
||
| 539 | * @return void |
||
| 540 | */ |
||
| 541 | public function syncRemote(): void { |
||
| 544 | |||
| 545 | |||
| 546 | /** |
||
| 547 | * @param string $circleId |
||
| 548 | * |
||
| 549 | * @return void |
||
| 550 | */ |
||
| 551 | public function syncRemoteCircle(string $circleId): void { |
||
| 553 | |||
| 554 | |||
| 555 | /** |
||
| 556 | * @param bool $force |
||
| 557 | * |
||
| 558 | * @throws MigrationException |
||
| 559 | * @throws RequestBuilderException |
||
| 560 | */ |
||
| 561 | public function migration(bool $force = false): void { |
||
| 577 | |||
| 578 | /** |
||
| 579 | * @return void |
||
| 580 | * @throws RequestBuilderException |
||
| 581 | */ |
||
| 582 | private function migrationTo22(): void { |
||
| 602 | |||
| 603 | |||
| 604 | /** |
||
| 605 | * |
||
| 606 | * @throws RequestBuilderException |
||
| 607 | */ |
||
| 608 | private function migrationTo22_Circles(): void { |
||
| 677 | |||
| 678 | |||
| 679 | /** |
||
| 680 | * @throws ContactAddressBookNotFoundException |
||
| 681 | * @throws ContactFormatException |
||
| 682 | * @throws ContactNotFoundException |
||
| 683 | * @throws FederatedUserException |
||
| 684 | * @throws InvalidIdException |
||
| 685 | * @throws RequestBuilderException |
||
| 686 | * @throws SingleCircleNotFoundException |
||
| 687 | */ |
||
| 688 | private function migrationTo22_Members(): void { |
||
| 761 | |||
| 762 | /** |
||
| 763 | * @param string $message |
||
| 764 | * @param bool $advance |
||
| 765 | */ |
||
| 766 | private function output(string $message, bool $advance = false): void { |
||
| 779 | |||
| 780 | } |
||
| 781 | |||
| 782 |