Complex classes like FederatedUserService 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 FederatedUserService, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 82 | class FederatedUserService { |
||
| 83 | |||
| 84 | |||
| 85 | use TArrayTools; |
||
| 86 | use TStringTools; |
||
| 87 | use TNC22Logger; |
||
| 88 | |||
| 89 | |||
| 90 | /** @var IUserSession */ |
||
| 91 | private $userSession; |
||
| 92 | |||
| 93 | /** @var IUserManager */ |
||
| 94 | private $userManager; |
||
| 95 | |||
| 96 | /** @var IGroupManager */ |
||
| 97 | private $groupManager; |
||
| 98 | |||
| 99 | /** @var FederatedEventService */ |
||
| 100 | private $federatedEventService; |
||
| 101 | |||
| 102 | /** @var MembershipService */ |
||
| 103 | private $membershipService; |
||
| 104 | |||
| 105 | /** @var CircleRequest */ |
||
| 106 | private $circleRequest; |
||
| 107 | |||
| 108 | /** @var MemberRequest */ |
||
| 109 | private $memberRequest; |
||
| 110 | |||
| 111 | /** @var RemoteService */ |
||
| 112 | private $remoteService; |
||
| 113 | |||
| 114 | /** @var ContactService */ |
||
| 115 | private $contactService; |
||
| 116 | |||
| 117 | /** @var InterfaceService */ |
||
| 118 | private $interfaceService; |
||
| 119 | |||
| 120 | /** @var ConfigService */ |
||
| 121 | private $configService; |
||
| 122 | |||
| 123 | |||
| 124 | /** @var FederatedUser */ |
||
| 125 | private $currentUser = null; |
||
| 126 | |||
| 127 | /** @var FederatedUser */ |
||
| 128 | private $currentApp = null; |
||
| 129 | |||
| 130 | /** @var RemoteInstance */ |
||
| 131 | private $remoteInstance = null; |
||
| 132 | |||
| 133 | /** @var bool */ |
||
| 134 | private $bypass = false; |
||
| 135 | |||
| 136 | |||
| 137 | /** |
||
| 138 | * FederatedUserService constructor. |
||
| 139 | * |
||
| 140 | * @param IUserSession $userSession |
||
| 141 | * @param IUserManager $userManager |
||
| 142 | * @param IGroupManager $groupManager |
||
| 143 | * @param FederatedEventService $federatedEventService |
||
| 144 | * @param MembershipService $membershipService |
||
| 145 | * @param CircleRequest $circleRequest |
||
| 146 | * @param MemberRequest $memberRequest |
||
| 147 | * @param RemoteService $remoteService |
||
| 148 | * @param ContactService $contactService |
||
| 149 | * @param ConfigService $configService |
||
| 150 | */ |
||
| 151 | public function __construct( |
||
| 169 | |||
| 170 | |||
| 171 | /** |
||
| 172 | * @throws FederatedUserException |
||
| 173 | * @throws FederatedUserNotFoundException |
||
| 174 | * @throws InvalidIdException |
||
| 175 | * @throws SingleCircleNotFoundException |
||
| 176 | */ |
||
| 177 | public function initCurrentUser() { |
||
| 185 | |||
| 186 | |||
| 187 | /** |
||
| 188 | * @param IUser|null $user |
||
| 189 | * |
||
| 190 | * @throws FederatedUserException |
||
| 191 | * @throws FederatedUserNotFoundException |
||
| 192 | * @throws InvalidIdException |
||
| 193 | * @throws SingleCircleNotFoundException |
||
| 194 | * @throws RequestBuilderException |
||
| 195 | */ |
||
| 196 | public function setLocalCurrentUser(?IUser $user): void { |
||
| 203 | |||
| 204 | /** |
||
| 205 | * @param string $userId |
||
| 206 | * |
||
| 207 | * @throws FederatedUserException |
||
| 208 | * @throws FederatedUserNotFoundException |
||
| 209 | * @throws InvalidIdException |
||
| 210 | * @throws RequestBuilderException |
||
| 211 | * @throws SingleCircleNotFoundException |
||
| 212 | */ |
||
| 213 | public function setLocalCurrentUserId(string $userId): void { |
||
| 216 | |||
| 217 | /** |
||
| 218 | * @param string $appId |
||
| 219 | * @param int $appNumber |
||
| 220 | * |
||
| 221 | * @throws FederatedUserException |
||
| 222 | * @throws InvalidIdException |
||
| 223 | * @throws SingleCircleNotFoundException |
||
| 224 | */ |
||
| 225 | public function setLocalCurrentApp(string $appId, int $appNumber): void { |
||
| 228 | |||
| 229 | |||
| 230 | /** |
||
| 231 | * set a CurrentUser, based on a IFederatedUser. |
||
| 232 | * CurrentUser is mainly used to manage rights when requesting the database. |
||
| 233 | * |
||
| 234 | * @param IFederatedUser $federatedUser |
||
| 235 | * |
||
| 236 | * @throws FederatedUserException |
||
| 237 | */ |
||
| 238 | public function setCurrentUser(IFederatedUser $federatedUser): void { |
||
| 249 | |||
| 250 | /** |
||
| 251 | * @return FederatedUser|null |
||
| 252 | */ |
||
| 253 | public function getCurrentUser(): ?FederatedUser { |
||
| 256 | |||
| 257 | /** |
||
| 258 | * @return bool |
||
| 259 | */ |
||
| 260 | public function hasCurrentUser(): bool { |
||
| 263 | |||
| 264 | /** |
||
| 265 | * @throws InitiatorNotFoundException |
||
| 266 | */ |
||
| 267 | public function mustHaveCurrentUser(): void { |
||
| 275 | |||
| 276 | /** |
||
| 277 | * @param bool $bypass |
||
| 278 | */ |
||
| 279 | public function bypassCurrentUserCondition(bool $bypass): void { |
||
| 282 | |||
| 283 | |||
| 284 | /** |
||
| 285 | * @return FederatedUser|null |
||
| 286 | */ |
||
| 287 | public function getCurrentApp(): ?FederatedUser { |
||
| 290 | |||
| 291 | /** |
||
| 292 | * @return bool |
||
| 293 | */ |
||
| 294 | public function hasCurrentApp(): bool { |
||
| 297 | |||
| 298 | |||
| 299 | /** |
||
| 300 | * set a RemoteInstance, mostly from a remote request (RemoteController) |
||
| 301 | * Used to limit rights in some request in the local database. |
||
| 302 | * |
||
| 303 | * @param RemoteInstance $remoteInstance |
||
| 304 | */ |
||
| 305 | public function setRemoteInstance(RemoteInstance $remoteInstance): void { |
||
| 308 | |||
| 309 | /** |
||
| 310 | * @return RemoteInstance|null |
||
| 311 | */ |
||
| 312 | public function getRemoteInstance(): ?RemoteInstance { |
||
| 315 | |||
| 316 | /** |
||
| 317 | * @return bool |
||
| 318 | */ |
||
| 319 | public function hasRemoteInstance(): bool { |
||
| 322 | |||
| 323 | |||
| 324 | /** |
||
| 325 | * Get the full FederatedUser for a local user. |
||
| 326 | * Will generate the SingleId if none exist |
||
| 327 | * |
||
| 328 | * @param string $userId |
||
| 329 | * |
||
| 330 | * @return FederatedUser |
||
| 331 | * @throws FederatedUserNotFoundException |
||
| 332 | * @throws InvalidIdException |
||
| 333 | * @throws SingleCircleNotFoundException |
||
| 334 | * @throws FederatedUserException |
||
| 335 | * @throws RequestBuilderException |
||
| 336 | */ |
||
| 337 | public function getLocalFederatedUser(string $userId): FederatedUser { |
||
| 349 | |||
| 350 | |||
| 351 | /** |
||
| 352 | * Get the full FederatedUser for a local user. |
||
| 353 | * Will generate the SingleId if none exist |
||
| 354 | * |
||
| 355 | * @param string $appId |
||
| 356 | * @param int $appNumber |
||
| 357 | * |
||
| 358 | * @return FederatedUser |
||
| 359 | * @throws FederatedUserException |
||
| 360 | * @throws InvalidIdException |
||
| 361 | * @throws SingleCircleNotFoundException |
||
| 362 | * @throws RequestBuilderException |
||
| 363 | */ |
||
| 364 | public function getAppInitiator(string $appId, int $appNumber): FederatedUser { |
||
| 375 | |||
| 376 | |||
| 377 | /** |
||
| 378 | * some ./occ commands allows to add an Initiator, or force the PoV from the local circles' owner |
||
| 379 | * |
||
| 380 | * TODO: manage non-user type ? |
||
| 381 | * |
||
| 382 | * @param string $userId |
||
| 383 | * @param string $circleId |
||
| 384 | * @param bool $bypass |
||
| 385 | * |
||
| 386 | * @throws CircleNotFoundException |
||
| 387 | * @throws FederatedItemException |
||
| 388 | * @throws FederatedUserException |
||
| 389 | * @throws FederatedUserNotFoundException |
||
| 390 | * @throws InvalidIdException |
||
| 391 | * @throws MemberNotFoundException |
||
| 392 | * @throws OwnerNotFoundException |
||
| 393 | * @throws RemoteInstanceException |
||
| 394 | * @throws RemoteNotFoundException |
||
| 395 | * @throws RemoteResourceNotFoundException |
||
| 396 | * @throws SingleCircleNotFoundException |
||
| 397 | * @throws UnknownRemoteException |
||
| 398 | * @throws UserTypeNotFoundException |
||
| 399 | * @throws RequestBuilderException |
||
| 400 | */ |
||
| 401 | public function commandLineInitiator( |
||
| 430 | |||
| 431 | |||
| 432 | /** |
||
| 433 | * Works like getFederatedUser, but returns a member. |
||
| 434 | * Allow to specify a level: handle@instance,level |
||
| 435 | * |
||
| 436 | * Used for filters when searching for Circles |
||
| 437 | * TODO: Used outside of ./occ circles:manage:list ? |
||
| 438 | * |
||
| 439 | * @param string $userId |
||
| 440 | * @param int $level |
||
| 441 | * |
||
| 442 | * @return Member |
||
| 443 | * @throws CircleNotFoundException |
||
| 444 | * @throws FederatedItemException |
||
| 445 | * @throws FederatedUserException |
||
| 446 | * @throws FederatedUserNotFoundException |
||
| 447 | * @throws InvalidIdException |
||
| 448 | * @throws MemberNotFoundException |
||
| 449 | * @throws OwnerNotFoundException |
||
| 450 | * @throws RemoteInstanceException |
||
| 451 | * @throws RemoteNotFoundException |
||
| 452 | * @throws RemoteResourceNotFoundException |
||
| 453 | * @throws SingleCircleNotFoundException |
||
| 454 | * @throws UnknownRemoteException |
||
| 455 | * @throws UserTypeNotFoundException |
||
| 456 | */ |
||
| 457 | public function getFederatedMember(string $userId, int $level = Member::LEVEL_MEMBER): Member { |
||
| 470 | |||
| 471 | |||
| 472 | /** |
||
| 473 | * get a valid FederatedUser, based on the federatedId (userId@instance) its the type. |
||
| 474 | * If instance is local, get the local valid FederatedUser |
||
| 475 | * If instance is not local, get the remote valid FederatedUser |
||
| 476 | * |
||
| 477 | * @param string $federatedId |
||
| 478 | * @param int $type |
||
| 479 | * |
||
| 480 | * @return FederatedUser |
||
| 481 | * @throws CircleNotFoundException |
||
| 482 | * @throws FederatedItemException |
||
| 483 | * @throws FederatedUserException |
||
| 484 | * @throws FederatedUserNotFoundException |
||
| 485 | * @throws InvalidIdException |
||
| 486 | * @throws MemberNotFoundException |
||
| 487 | * @throws OwnerNotFoundException |
||
| 488 | * @throws RemoteInstanceException |
||
| 489 | * @throws RemoteNotFoundException |
||
| 490 | * @throws RemoteResourceNotFoundException |
||
| 491 | * @throws SingleCircleNotFoundException |
||
| 492 | * @throws UnknownRemoteException |
||
| 493 | * @throws UserTypeNotFoundException |
||
| 494 | * @throws RequestBuilderException |
||
| 495 | */ |
||
| 496 | public function getFederatedUser(string $federatedId, int $type = Member::TYPE_SINGLE): FederatedUser { |
||
| 521 | |||
| 522 | |||
| 523 | /** |
||
| 524 | * Generate a FederatedUser based on local data. |
||
| 525 | * WARNING: There is no confirmation that the returned FederatedUser exists or is valid at this point. |
||
| 526 | * Use getFederatedUser() instead if a valid and confirmed FederatedUser is needed. |
||
| 527 | * |
||
| 528 | * if $federatedId is a known SingleId, will returns data from the local database. |
||
| 529 | * if $federatedId is a local username, will returns data from the local database. |
||
| 530 | * Otherwise, the FederatedUser will not contains a SingleId. |
||
| 531 | * |
||
| 532 | * @param string $federatedId |
||
| 533 | * @param int $type |
||
| 534 | * |
||
| 535 | * @return FederatedUser |
||
| 536 | */ |
||
| 537 | public function generateFederatedUser(string $federatedId, int $type = 0): FederatedUser { |
||
| 549 | |||
| 550 | |||
| 551 | /** |
||
| 552 | * @param string $singleId |
||
| 553 | * @param string $instance |
||
| 554 | * |
||
| 555 | * @return FederatedUser |
||
| 556 | * @throws CircleNotFoundException |
||
| 557 | * @throws FederatedItemException |
||
| 558 | * @throws FederatedUserException |
||
| 559 | * @throws FederatedUserNotFoundException |
||
| 560 | * @throws MemberNotFoundException |
||
| 561 | * @throws OwnerNotFoundException |
||
| 562 | * @throws RemoteInstanceException |
||
| 563 | * @throws RemoteNotFoundException |
||
| 564 | * @throws RemoteResourceNotFoundException |
||
| 565 | * @throws UnknownRemoteException |
||
| 566 | */ |
||
| 567 | public function getFederatedUser_SingleId(string $singleId, string $instance): FederatedUser { |
||
| 582 | |||
| 583 | |||
| 584 | /** |
||
| 585 | * @param string $userId |
||
| 586 | * @param string $instance |
||
| 587 | * |
||
| 588 | * @return FederatedUser |
||
| 589 | * @throws FederatedUserException |
||
| 590 | * @throws FederatedUserNotFoundException |
||
| 591 | * @throws InvalidIdException |
||
| 592 | * @throws RemoteInstanceException |
||
| 593 | * @throws RemoteNotFoundException |
||
| 594 | * @throws RemoteResourceNotFoundException |
||
| 595 | * @throws SingleCircleNotFoundException |
||
| 596 | * @throws UnknownRemoteException |
||
| 597 | * @throws FederatedItemException |
||
| 598 | */ |
||
| 599 | private function getFederatedUser_User(string $userId, string $instance): FederatedUser { |
||
| 610 | |||
| 611 | |||
| 612 | /** |
||
| 613 | * @param string $groupName |
||
| 614 | * @param string $instance |
||
| 615 | * |
||
| 616 | * @return FederatedUser |
||
| 617 | * @throws FederatedEventException |
||
| 618 | * @throws FederatedItemException |
||
| 619 | * @throws FederatedUserException |
||
| 620 | * @throws GroupNotFoundException |
||
| 621 | * @throws InitiatorNotConfirmedException |
||
| 622 | * @throws InvalidIdException |
||
| 623 | * @throws OwnerNotFoundException |
||
| 624 | * @throws RemoteInstanceException |
||
| 625 | * @throws RemoteNotFoundException |
||
| 626 | * @throws RemoteResourceNotFoundException |
||
| 627 | * @throws SingleCircleNotFoundException |
||
| 628 | * @throws UnknownRemoteException |
||
| 629 | */ |
||
| 630 | public function getFederatedUser_Group(string $groupName, string $instance): FederatedUser { |
||
| 640 | |||
| 641 | |||
| 642 | /** |
||
| 643 | * @param string $mailAddress |
||
| 644 | * |
||
| 645 | * @return FederatedUser |
||
| 646 | * @throws FederatedUserException |
||
| 647 | * @throws InvalidIdException |
||
| 648 | * @throws RequestBuilderException |
||
| 649 | * @throws SingleCircleNotFoundException |
||
| 650 | */ |
||
| 651 | public function getFederatedUser_Mail(string $mailAddress): FederatedUser { |
||
| 658 | |||
| 659 | |||
| 660 | /** |
||
| 661 | * @param string $contactPath |
||
| 662 | * |
||
| 663 | * @return FederatedUser |
||
| 664 | * @throws FederatedUserException |
||
| 665 | * @throws InvalidIdException |
||
| 666 | * @throws RequestBuilderException |
||
| 667 | * @throws SingleCircleNotFoundException |
||
| 668 | */ |
||
| 669 | public function getFederatedUser_Contact(string $contactPath): FederatedUser { |
||
| 679 | |||
| 680 | |||
| 681 | /** |
||
| 682 | * extract userID and instance from a federatedId |
||
| 683 | * |
||
| 684 | * @param string $federatedId |
||
| 685 | * |
||
| 686 | * @return array |
||
| 687 | */ |
||
| 688 | public function extractIdAndInstance(string $federatedId): array { |
||
| 699 | |||
| 700 | |||
| 701 | /** |
||
| 702 | * @param FederatedUser $federatedUser |
||
| 703 | * |
||
| 704 | * @throws FederatedUserException |
||
| 705 | * @throws InvalidIdException |
||
| 706 | * @throws SingleCircleNotFoundException |
||
| 707 | * @throws RequestBuilderException |
||
| 708 | */ |
||
| 709 | private function fillSingleCircleId(FederatedUser $federatedUser): void { |
||
| 718 | |||
| 719 | |||
| 720 | /** |
||
| 721 | * get the Single Circle from a local user |
||
| 722 | * |
||
| 723 | * @param FederatedUser $federatedUser |
||
| 724 | * |
||
| 725 | * @return Circle |
||
| 726 | * @throws InvalidIdException |
||
| 727 | * @throws FederatedUserException |
||
| 728 | * @throws SingleCircleNotFoundException |
||
| 729 | * @throws RequestBuilderException |
||
| 730 | */ |
||
| 731 | private function getSingleCircle(FederatedUser $federatedUser): Circle { |
||
| 779 | |||
| 780 | |||
| 781 | /** |
||
| 782 | * @param FederatedUser $federatedUser |
||
| 783 | * |
||
| 784 | * @return string |
||
| 785 | * @throws ContactAddressBookNotFoundException |
||
| 786 | * @throws ContactFormatException |
||
| 787 | * @throws ContactNotFoundException |
||
| 788 | */ |
||
| 789 | private function getLocalDisplayName(FederatedUser $federatedUser): string { |
||
| 796 | |||
| 797 | |||
| 798 | /** |
||
| 799 | * Confirm that all field of a FederatedUser are filled. |
||
| 800 | * |
||
| 801 | * @param FederatedUser $federatedUser |
||
| 802 | * |
||
| 803 | * @throws FederatedUserException |
||
| 804 | */ |
||
| 805 | private function confirmFederatedUser(FederatedUser $federatedUser): void { |
||
| 814 | |||
| 815 | /** |
||
| 816 | * Confirm that the singleId of a FederatedUser is unique and not used to any other member of the |
||
| 817 | * database. |
||
| 818 | * |
||
| 819 | * @param FederatedUser $federatedUser |
||
| 820 | * |
||
| 821 | * @throws FederatedUserException |
||
| 822 | */ |
||
| 823 | public function confirmLocalSingleId(IFederatedUser $federatedUser): void { |
||
| 836 | |||
| 837 | |||
| 838 | /** |
||
| 839 | * @param IFederatedUser $federatedUser |
||
| 840 | * |
||
| 841 | * @throws FederatedUserException |
||
| 842 | * @throws RequestBuilderException |
||
| 843 | */ |
||
| 844 | public function confirmSingleIdUniqueness(IFederatedUser $federatedUser): void { |
||
| 851 | |||
| 852 | |||
| 853 | /** |
||
| 854 | * @param string $groupId |
||
| 855 | * |
||
| 856 | * @return Circle |
||
| 857 | * @throws GroupNotFoundException |
||
| 858 | * @throws FederatedEventException |
||
| 859 | * @throws FederatedItemException |
||
| 860 | * @throws FederatedUserException |
||
| 861 | * @throws InitiatorNotConfirmedException |
||
| 862 | * @throws InvalidIdException |
||
| 863 | * @throws OwnerNotFoundException |
||
| 864 | * @throws RemoteInstanceException |
||
| 865 | * @throws RemoteNotFoundException |
||
| 866 | * @throws RemoteResourceNotFoundException |
||
| 867 | * @throws SingleCircleNotFoundException |
||
| 868 | * @throws UnknownRemoteException |
||
| 869 | * @throws RequestBuilderException |
||
| 870 | */ |
||
| 871 | public function getGroupCircle(string $groupId): Circle { |
||
| 908 | |||
| 909 | } |
||
| 910 | |||
| 911 |