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