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 |
||
| 83 | class FederatedUserService { |
||
| 84 | |||
| 85 | |||
| 86 | use TArrayTools; |
||
| 87 | use TStringTools; |
||
| 88 | use TNC22Logger; |
||
| 89 | |||
| 90 | |||
| 91 | /** @var IUserSession */ |
||
| 92 | private $userSession; |
||
| 93 | |||
| 94 | /** @var IUserManager */ |
||
| 95 | private $userManager; |
||
| 96 | |||
| 97 | /** @var IGroupManager */ |
||
| 98 | private $groupManager; |
||
| 99 | |||
| 100 | /** @var FederatedEventService */ |
||
| 101 | private $federatedEventService; |
||
| 102 | |||
| 103 | /** @var MembershipService */ |
||
| 104 | private $membershipService; |
||
| 105 | |||
| 106 | /** @var CircleRequest */ |
||
| 107 | private $circleRequest; |
||
| 108 | |||
| 109 | /** @var MemberRequest */ |
||
| 110 | private $memberRequest; |
||
| 111 | |||
| 112 | /** @var RemoteService */ |
||
| 113 | private $remoteService; |
||
| 114 | |||
| 115 | /** @var ContactService */ |
||
| 116 | private $contactService; |
||
| 117 | |||
| 118 | /** @var InterfaceService */ |
||
| 119 | private $interfaceService; |
||
| 120 | |||
| 121 | /** @var ConfigService */ |
||
| 122 | private $configService; |
||
| 123 | |||
| 124 | |||
| 125 | /** @var FederatedUser */ |
||
| 126 | private $currentUser = null; |
||
| 127 | |||
| 128 | /** @var FederatedUser */ |
||
| 129 | private $currentApp = null; |
||
| 130 | |||
| 131 | /** @var RemoteInstance */ |
||
| 132 | private $remoteInstance = null; |
||
| 133 | |||
| 134 | /** @var bool */ |
||
| 135 | private $bypass = false; |
||
| 136 | |||
| 137 | /** @var bool */ |
||
| 138 | private $initiatedByOcc = false; |
||
| 139 | |||
| 140 | |||
| 141 | /** |
||
| 142 | * FederatedUserService constructor. |
||
| 143 | * |
||
| 144 | * @param IUserSession $userSession |
||
| 145 | * @param IUserManager $userManager |
||
| 146 | * @param IGroupManager $groupManager |
||
| 147 | * @param FederatedEventService $federatedEventService |
||
| 148 | * @param MembershipService $membershipService |
||
| 149 | * @param CircleRequest $circleRequest |
||
| 150 | * @param MemberRequest $memberRequest |
||
| 151 | * @param RemoteService $remoteService |
||
| 152 | * @param ContactService $contactService |
||
| 153 | * @param InterfaceService $interfaceService |
||
| 154 | * @param ConfigService $configService |
||
| 155 | */ |
||
| 156 | public function __construct( |
||
| 178 | |||
| 179 | |||
| 180 | /** |
||
| 181 | * @throws FederatedUserException |
||
| 182 | * @throws FederatedUserNotFoundException |
||
| 183 | * @throws InvalidIdException |
||
| 184 | * @throws SingleCircleNotFoundException |
||
| 185 | * @throws RequestBuilderException |
||
| 186 | */ |
||
| 187 | public function initCurrentUser() { |
||
| 195 | |||
| 196 | |||
| 197 | /** |
||
| 198 | * @param IUser|null $user |
||
| 199 | * |
||
| 200 | * @throws FederatedUserException |
||
| 201 | * @throws FederatedUserNotFoundException |
||
| 202 | * @throws InvalidIdException |
||
| 203 | * @throws SingleCircleNotFoundException |
||
| 204 | * @throws RequestBuilderException |
||
| 205 | */ |
||
| 206 | public function setLocalCurrentUser(?IUser $user): void { |
||
| 213 | |||
| 214 | /** |
||
| 215 | * @param string $userId |
||
| 216 | * |
||
| 217 | * @throws FederatedUserException |
||
| 218 | * @throws FederatedUserNotFoundException |
||
| 219 | * @throws InvalidIdException |
||
| 220 | * @throws RequestBuilderException |
||
| 221 | * @throws SingleCircleNotFoundException |
||
| 222 | */ |
||
| 223 | public function setLocalCurrentUserId(string $userId): void { |
||
| 226 | |||
| 227 | /** |
||
| 228 | * @param string $appId |
||
| 229 | * @param int $appNumber |
||
| 230 | * |
||
| 231 | * @throws FederatedUserException |
||
| 232 | * @throws InvalidIdException |
||
| 233 | * @throws SingleCircleNotFoundException |
||
| 234 | */ |
||
| 235 | public function setLocalCurrentApp(string $appId, int $appNumber): void { |
||
| 238 | |||
| 239 | |||
| 240 | /** |
||
| 241 | * set a CurrentUser, based on a IFederatedUser. |
||
| 242 | * CurrentUser is mainly used to manage rights when requesting the database. |
||
| 243 | * |
||
| 244 | * @param IFederatedUser $federatedUser |
||
| 245 | * |
||
| 246 | * @throws FederatedUserException |
||
| 247 | */ |
||
| 248 | public function setCurrentUser(IFederatedUser $federatedUser): void { |
||
| 259 | |||
| 260 | /** |
||
| 261 | * |
||
| 262 | */ |
||
| 263 | public function unsetCurrentUser(): void { |
||
| 266 | |||
| 267 | /** |
||
| 268 | * @return FederatedUser|null |
||
| 269 | */ |
||
| 270 | public function getCurrentUser(): ?FederatedUser { |
||
| 273 | |||
| 274 | /** |
||
| 275 | * @return bool |
||
| 276 | */ |
||
| 277 | public function hasCurrentUser(): bool { |
||
| 280 | |||
| 281 | /** |
||
| 282 | * @throws InitiatorNotFoundException |
||
| 283 | */ |
||
| 284 | public function mustHaveCurrentUser(): void { |
||
| 292 | |||
| 293 | /** |
||
| 294 | * @param bool $bypass |
||
| 295 | */ |
||
| 296 | public function bypassCurrentUserCondition(bool $bypass): void { |
||
| 299 | |||
| 300 | |||
| 301 | /** |
||
| 302 | * @param bool $initiatedByOcc |
||
| 303 | */ |
||
| 304 | public function setInitiatedByOcc(bool $initiatedByOcc): void { |
||
| 307 | |||
| 308 | /** |
||
| 309 | * @return bool |
||
| 310 | */ |
||
| 311 | public function isInitiatedByOcc(): bool { |
||
| 314 | |||
| 315 | /** |
||
| 316 | * @param Member $member |
||
| 317 | * |
||
| 318 | * @throws ContactAddressBookNotFoundException |
||
| 319 | * @throws ContactFormatException |
||
| 320 | * @throws ContactNotFoundException |
||
| 321 | * @throws FederatedUserException |
||
| 322 | * @throws InvalidIdException |
||
| 323 | * @throws RequestBuilderException |
||
| 324 | * @throws SingleCircleNotFoundException |
||
| 325 | */ |
||
| 326 | public function setMemberPatron(Member $member): void { |
||
| 333 | |||
| 334 | |||
| 335 | /** |
||
| 336 | * @return FederatedUser|null |
||
| 337 | */ |
||
| 338 | public function getCurrentApp(): ?FederatedUser { |
||
| 341 | |||
| 342 | /** |
||
| 343 | * @return bool |
||
| 344 | */ |
||
| 345 | public function hasCurrentApp(): bool { |
||
| 348 | |||
| 349 | |||
| 350 | /** |
||
| 351 | * set a RemoteInstance, mostly from a remote request (RemoteController) |
||
| 352 | * Used to limit rights in some request in the local database. |
||
| 353 | * |
||
| 354 | * @param RemoteInstance $remoteInstance |
||
| 355 | */ |
||
| 356 | public function setRemoteInstance(RemoteInstance $remoteInstance): void { |
||
| 359 | |||
| 360 | /** |
||
| 361 | * @return RemoteInstance|null |
||
| 362 | */ |
||
| 363 | public function getRemoteInstance(): ?RemoteInstance { |
||
| 366 | |||
| 367 | /** |
||
| 368 | * @return bool |
||
| 369 | */ |
||
| 370 | public function hasRemoteInstance(): bool { |
||
| 373 | |||
| 374 | |||
| 375 | /** |
||
| 376 | * Get the full FederatedUser for a local user. |
||
| 377 | * Will generate the SingleId if none exist |
||
| 378 | * |
||
| 379 | * @param string $userId |
||
| 380 | * @param bool $check |
||
| 381 | * |
||
| 382 | * @return FederatedUser |
||
| 383 | * @throws ContactAddressBookNotFoundException |
||
| 384 | * @throws ContactFormatException |
||
| 385 | * @throws ContactNotFoundException |
||
| 386 | * @throws FederatedUserException |
||
| 387 | * @throws FederatedUserNotFoundException |
||
| 388 | * @throws InvalidIdException |
||
| 389 | * @throws RequestBuilderException |
||
| 390 | * @throws SingleCircleNotFoundException |
||
| 391 | */ |
||
| 392 | public function getLocalFederatedUser(string $userId, bool $check = true): FederatedUser { |
||
| 407 | |||
| 408 | |||
| 409 | /** |
||
| 410 | * Get the full FederatedUser for a local user. |
||
| 411 | * Will generate the SingleId if none exist |
||
| 412 | * |
||
| 413 | * @param string $appId |
||
| 414 | * @param int $appNumber |
||
| 415 | * |
||
| 416 | * @return FederatedUser |
||
| 417 | * @throws ContactAddressBookNotFoundException |
||
| 418 | * @throws ContactFormatException |
||
| 419 | * @throws ContactNotFoundException |
||
| 420 | * @throws FederatedUserException |
||
| 421 | * @throws InvalidIdException |
||
| 422 | * @throws RequestBuilderException |
||
| 423 | * @throws SingleCircleNotFoundException |
||
| 424 | */ |
||
| 425 | public function getAppInitiator(string $appId, int $appNumber): FederatedUser { |
||
| 436 | |||
| 437 | |||
| 438 | /** |
||
| 439 | * some ./occ commands allows to add an Initiator, or force the PoV from the local circles' owner |
||
| 440 | * |
||
| 441 | * TODO: manage non-user type ? |
||
| 442 | * |
||
| 443 | * @param string $userId |
||
| 444 | * @param int $userType |
||
| 445 | * @param string $circleId |
||
| 446 | * @param bool $bypass |
||
| 447 | * |
||
| 448 | * @throws CircleNotFoundException |
||
| 449 | * @throws FederatedItemException |
||
| 450 | * @throws FederatedUserException |
||
| 451 | * @throws FederatedUserNotFoundException |
||
| 452 | * @throws InvalidIdException |
||
| 453 | * @throws MemberNotFoundException |
||
| 454 | * @throws OwnerNotFoundException |
||
| 455 | * @throws RemoteInstanceException |
||
| 456 | * @throws RemoteNotFoundException |
||
| 457 | * @throws RemoteResourceNotFoundException |
||
| 458 | * @throws RequestBuilderException |
||
| 459 | * @throws SingleCircleNotFoundException |
||
| 460 | * @throws UnknownRemoteException |
||
| 461 | * @throws UserTypeNotFoundException |
||
| 462 | */ |
||
| 463 | public function commandLineInitiator( |
||
| 492 | |||
| 493 | |||
| 494 | /** |
||
| 495 | * Works like getFederatedUser, but returns a member. |
||
| 496 | * Allow to specify a level: handle@instance,level |
||
| 497 | * |
||
| 498 | * Used for filters when searching for Circles |
||
| 499 | * TODO: Used outside of ./occ circles:manage:list ? |
||
| 500 | * |
||
| 501 | * @param string $userId |
||
| 502 | * @param int $level |
||
| 503 | * |
||
| 504 | * @return Member |
||
| 505 | * @throws CircleNotFoundException |
||
| 506 | * @throws FederatedItemException |
||
| 507 | * @throws FederatedUserException |
||
| 508 | * @throws FederatedUserNotFoundException |
||
| 509 | * @throws InvalidIdException |
||
| 510 | * @throws MemberNotFoundException |
||
| 511 | * @throws OwnerNotFoundException |
||
| 512 | * @throws RemoteInstanceException |
||
| 513 | * @throws RemoteNotFoundException |
||
| 514 | * @throws RemoteResourceNotFoundException |
||
| 515 | * @throws SingleCircleNotFoundException |
||
| 516 | * @throws UnknownRemoteException |
||
| 517 | * @throws UserTypeNotFoundException |
||
| 518 | */ |
||
| 519 | public function getFederatedMember(string $userId, int $level = Member::LEVEL_MEMBER): Member { |
||
| 532 | |||
| 533 | |||
| 534 | /** |
||
| 535 | * get a valid FederatedUser, based on the federatedId (userId@instance) its the type. |
||
| 536 | * If instance is local, get the local valid FederatedUser |
||
| 537 | * If instance is not local, get the remote valid FederatedUser |
||
| 538 | * |
||
| 539 | * @param string $federatedId |
||
| 540 | * @param int $type |
||
| 541 | * |
||
| 542 | * @return FederatedUser |
||
| 543 | * @throws CircleNotFoundException |
||
| 544 | * @throws FederatedItemException |
||
| 545 | * @throws FederatedUserException |
||
| 546 | * @throws FederatedUserNotFoundException |
||
| 547 | * @throws InvalidIdException |
||
| 548 | * @throws MemberNotFoundException |
||
| 549 | * @throws OwnerNotFoundException |
||
| 550 | * @throws RemoteInstanceException |
||
| 551 | * @throws RemoteNotFoundException |
||
| 552 | * @throws RemoteResourceNotFoundException |
||
| 553 | * @throws SingleCircleNotFoundException |
||
| 554 | * @throws UnknownRemoteException |
||
| 555 | * @throws UserTypeNotFoundException |
||
| 556 | * @throws RequestBuilderException |
||
| 557 | */ |
||
| 558 | public function getFederatedUser(string $federatedId, int $type = Member::TYPE_SINGLE): FederatedUser { |
||
| 583 | |||
| 584 | |||
| 585 | /** |
||
| 586 | * Generate a FederatedUser based on local data. |
||
| 587 | * WARNING: There is no confirmation that the returned FederatedUser exists or is valid at this point. |
||
| 588 | * Use getFederatedUser() instead if a valid and confirmed FederatedUser is needed. |
||
| 589 | * |
||
| 590 | * if $federatedId is a known SingleId, will returns data from the local database. |
||
| 591 | * if $federatedId is a local username, will returns data from the local database. |
||
| 592 | * Otherwise, the FederatedUser will not contains a SingleId. |
||
| 593 | * |
||
| 594 | * @param string $federatedId |
||
| 595 | * @param int $type |
||
| 596 | * |
||
| 597 | * @return FederatedUser |
||
| 598 | */ |
||
| 599 | public function generateFederatedUser(string $federatedId, int $type = 0): FederatedUser { |
||
| 611 | |||
| 612 | |||
| 613 | /** |
||
| 614 | * @param FederatedUser $federatedUser |
||
| 615 | */ |
||
| 616 | public function deleteFederatedUser(FederatedUser $federatedUser): void { |
||
| 620 | |||
| 621 | |||
| 622 | /** |
||
| 623 | * @param string $singleId |
||
| 624 | * @param string $instance |
||
| 625 | * |
||
| 626 | * @return FederatedUser |
||
| 627 | * @throws CircleNotFoundException |
||
| 628 | * @throws FederatedItemException |
||
| 629 | * @throws FederatedUserException |
||
| 630 | * @throws FederatedUserNotFoundException |
||
| 631 | * @throws MemberNotFoundException |
||
| 632 | * @throws OwnerNotFoundException |
||
| 633 | * @throws RemoteInstanceException |
||
| 634 | * @throws RemoteNotFoundException |
||
| 635 | * @throws RemoteResourceNotFoundException |
||
| 636 | * @throws UnknownRemoteException |
||
| 637 | */ |
||
| 638 | public function getFederatedUser_SingleId(string $singleId, string $instance): FederatedUser { |
||
| 653 | |||
| 654 | |||
| 655 | /** |
||
| 656 | * @param string $userId |
||
| 657 | * @param string $instance |
||
| 658 | * |
||
| 659 | * @return FederatedUser |
||
| 660 | * @throws FederatedUserException |
||
| 661 | * @throws FederatedUserNotFoundException |
||
| 662 | * @throws InvalidIdException |
||
| 663 | * @throws RemoteInstanceException |
||
| 664 | * @throws RemoteNotFoundException |
||
| 665 | * @throws RemoteResourceNotFoundException |
||
| 666 | * @throws SingleCircleNotFoundException |
||
| 667 | * @throws UnknownRemoteException |
||
| 668 | * @throws FederatedItemException |
||
| 669 | */ |
||
| 670 | private function getFederatedUser_User(string $userId, string $instance): FederatedUser { |
||
| 681 | |||
| 682 | |||
| 683 | /** |
||
| 684 | * @param string $groupName |
||
| 685 | * @param string $instance |
||
| 686 | * |
||
| 687 | * @return FederatedUser |
||
| 688 | * @throws FederatedEventException |
||
| 689 | * @throws FederatedItemException |
||
| 690 | * @throws FederatedUserException |
||
| 691 | * @throws GroupNotFoundException |
||
| 692 | * @throws InitiatorNotConfirmedException |
||
| 693 | * @throws InvalidIdException |
||
| 694 | * @throws OwnerNotFoundException |
||
| 695 | * @throws RemoteInstanceException |
||
| 696 | * @throws RemoteNotFoundException |
||
| 697 | * @throws RemoteResourceNotFoundException |
||
| 698 | * @throws SingleCircleNotFoundException |
||
| 699 | * @throws UnknownRemoteException |
||
| 700 | */ |
||
| 701 | public function getFederatedUser_Group(string $groupName, string $instance): FederatedUser { |
||
| 711 | |||
| 712 | |||
| 713 | /** |
||
| 714 | * @param string $mailAddress |
||
| 715 | * |
||
| 716 | * @return FederatedUser |
||
| 717 | * @throws FederatedUserException |
||
| 718 | * @throws InvalidIdException |
||
| 719 | * @throws RequestBuilderException |
||
| 720 | * @throws SingleCircleNotFoundException |
||
| 721 | */ |
||
| 722 | public function getFederatedUser_Mail(string $mailAddress): FederatedUser { |
||
| 729 | |||
| 730 | |||
| 731 | /** |
||
| 732 | * @param string $contactPath |
||
| 733 | * |
||
| 734 | * @return FederatedUser |
||
| 735 | * @throws FederatedUserException |
||
| 736 | * @throws InvalidIdException |
||
| 737 | * @throws RequestBuilderException |
||
| 738 | * @throws SingleCircleNotFoundException |
||
| 739 | */ |
||
| 740 | public function getFederatedUser_Contact(string $contactPath): FederatedUser { |
||
| 750 | |||
| 751 | |||
| 752 | /** |
||
| 753 | * extract userID and instance from a federatedId |
||
| 754 | * |
||
| 755 | * @param string $federatedId |
||
| 756 | * |
||
| 757 | * @return array |
||
| 758 | */ |
||
| 759 | public function extractIdAndInstance(string $federatedId): array { |
||
| 770 | |||
| 771 | |||
| 772 | /** |
||
| 773 | * @param FederatedUser $federatedUser |
||
| 774 | * @param bool $generate |
||
| 775 | * |
||
| 776 | * @throws ContactAddressBookNotFoundException |
||
| 777 | * @throws ContactFormatException |
||
| 778 | * @throws ContactNotFoundException |
||
| 779 | * @throws FederatedUserException |
||
| 780 | * @throws InvalidIdException |
||
| 781 | * @throws RequestBuilderException |
||
| 782 | * @throws SingleCircleNotFoundException |
||
| 783 | */ |
||
| 784 | private function fillSingleCircleId(FederatedUser $federatedUser, bool $generate = true): void { |
||
| 793 | |||
| 794 | |||
| 795 | /** |
||
| 796 | * get the Single Circle from a local user |
||
| 797 | * |
||
| 798 | * @param FederatedUser $federatedUser |
||
| 799 | * @param bool $generate |
||
| 800 | * |
||
| 801 | * @return Circle |
||
| 802 | * @throws ContactAddressBookNotFoundException |
||
| 803 | * @throws ContactFormatException |
||
| 804 | * @throws ContactNotFoundException |
||
| 805 | * @throws FederatedUserException |
||
| 806 | * @throws InvalidIdException |
||
| 807 | * @throws RequestBuilderException |
||
| 808 | * @throws SingleCircleNotFoundException |
||
| 809 | */ |
||
| 810 | private function getSingleCircle(FederatedUser $federatedUser, bool $generate = true): Circle { |
||
| 866 | |||
| 867 | |||
| 868 | /** |
||
| 869 | * @param FederatedUser $federatedUser |
||
| 870 | * |
||
| 871 | * @return string |
||
| 872 | * @throws ContactAddressBookNotFoundException |
||
| 873 | * @throws ContactFormatException |
||
| 874 | * @throws ContactNotFoundException |
||
| 875 | */ |
||
| 876 | private function getLocalDisplayName(FederatedUser $federatedUser): string { |
||
| 883 | |||
| 884 | |||
| 885 | /** |
||
| 886 | * Confirm that all field of a FederatedUser are filled. |
||
| 887 | * |
||
| 888 | * @param FederatedUser $federatedUser |
||
| 889 | * |
||
| 890 | * @throws FederatedUserException |
||
| 891 | */ |
||
| 892 | private function confirmFederatedUser(FederatedUser $federatedUser): void { |
||
| 901 | |||
| 902 | /** |
||
| 903 | * Confirm that the singleId of a FederatedUser is unique and not used to any other member of the |
||
| 904 | * database. |
||
| 905 | * |
||
| 906 | * @param FederatedUser $federatedUser |
||
| 907 | * |
||
| 908 | * @throws FederatedUserException |
||
| 909 | * @throws RequestBuilderException |
||
| 910 | */ |
||
| 911 | public function confirmLocalSingleId(IFederatedUser $federatedUser): void { |
||
| 924 | |||
| 925 | |||
| 926 | /** |
||
| 927 | * @param IFederatedUser $federatedUser |
||
| 928 | * |
||
| 929 | * @throws FederatedUserException |
||
| 930 | * @throws RequestBuilderException |
||
| 931 | */ |
||
| 932 | public function confirmSingleIdUniqueness(IFederatedUser $federatedUser): void { |
||
| 939 | |||
| 940 | |||
| 941 | /** |
||
| 942 | * @param string $groupId |
||
| 943 | * |
||
| 944 | * @return Circle |
||
| 945 | * @throws GroupNotFoundException |
||
| 946 | * @throws FederatedEventException |
||
| 947 | * @throws FederatedItemException |
||
| 948 | * @throws FederatedUserException |
||
| 949 | * @throws InitiatorNotConfirmedException |
||
| 950 | * @throws InvalidIdException |
||
| 951 | * @throws OwnerNotFoundException |
||
| 952 | * @throws RemoteInstanceException |
||
| 953 | * @throws RemoteNotFoundException |
||
| 954 | * @throws RemoteResourceNotFoundException |
||
| 955 | * @throws SingleCircleNotFoundException |
||
| 956 | * @throws UnknownRemoteException |
||
| 957 | * @throws RequestBuilderException |
||
| 958 | */ |
||
| 959 | public function getGroupCircle(string $groupId): Circle { |
||
| 996 | |||
| 997 | } |
||
| 998 | |||
| 999 |
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: