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 RemoteStreamService */ |
||
| 116 | private $remoteStreamService; |
||
| 117 | |||
| 118 | /** @var ContactService */ |
||
| 119 | private $contactService; |
||
| 120 | |||
| 121 | /** @var InterfaceService */ |
||
| 122 | private $interfaceService; |
||
| 123 | |||
| 124 | /** @var ConfigService */ |
||
| 125 | private $configService; |
||
| 126 | |||
| 127 | |||
| 128 | /** @var FederatedUser */ |
||
| 129 | private $currentUser = null; |
||
| 130 | |||
| 131 | /** @var FederatedUser */ |
||
| 132 | private $currentApp = null; |
||
| 133 | |||
| 134 | /** @var RemoteInstance */ |
||
| 135 | private $remoteInstance = null; |
||
| 136 | |||
| 137 | /** @var bool */ |
||
| 138 | private $bypass = false; |
||
| 139 | |||
| 140 | /** @var bool */ |
||
| 141 | private $initiatedByOcc = false; |
||
| 142 | |||
| 143 | |||
| 144 | /** |
||
| 145 | * FederatedUserService constructor. |
||
| 146 | * |
||
| 147 | * @param IUserSession $userSession |
||
| 148 | * @param IUserManager $userManager |
||
| 149 | * @param IGroupManager $groupManager |
||
| 150 | * @param FederatedEventService $federatedEventService |
||
| 151 | * @param MembershipService $membershipService |
||
| 152 | * @param CircleRequest $circleRequest |
||
| 153 | * @param MemberRequest $memberRequest |
||
| 154 | * @param RemoteService $remoteService |
||
| 155 | * @param RemoteStreamService $remoteStreamService |
||
| 156 | * @param ContactService $contactService |
||
| 157 | * @param InterfaceService $interfaceService |
||
| 158 | * @param ConfigService $configService |
||
| 159 | */ |
||
| 160 | public function __construct( |
||
| 191 | |||
| 192 | |||
| 193 | /** |
||
| 194 | * @throws FederatedUserException |
||
| 195 | * @throws FederatedUserNotFoundException |
||
| 196 | * @throws InvalidIdException |
||
| 197 | * @throws SingleCircleNotFoundException |
||
| 198 | * @throws RequestBuilderException |
||
| 199 | */ |
||
| 200 | public function initCurrentUser() { |
||
| 208 | |||
| 209 | |||
| 210 | /** |
||
| 211 | * @param IUser|null $user |
||
| 212 | * |
||
| 213 | * @throws FederatedUserException |
||
| 214 | * @throws FederatedUserNotFoundException |
||
| 215 | * @throws InvalidIdException |
||
| 216 | * @throws SingleCircleNotFoundException |
||
| 217 | * @throws RequestBuilderException |
||
| 218 | */ |
||
| 219 | public function setLocalCurrentUser(?IUser $user): void { |
||
| 226 | |||
| 227 | /** |
||
| 228 | * @param string $userId |
||
| 229 | * |
||
| 230 | * @throws FederatedUserException |
||
| 231 | * @throws FederatedUserNotFoundException |
||
| 232 | * @throws InvalidIdException |
||
| 233 | * @throws RequestBuilderException |
||
| 234 | * @throws SingleCircleNotFoundException |
||
| 235 | */ |
||
| 236 | public function setLocalCurrentUserId(string $userId): void { |
||
| 239 | |||
| 240 | /** |
||
| 241 | * @param string $appId |
||
| 242 | * @param int $appNumber |
||
| 243 | * |
||
| 244 | * @throws FederatedUserException |
||
| 245 | * @throws InvalidIdException |
||
| 246 | * @throws SingleCircleNotFoundException |
||
| 247 | */ |
||
| 248 | public function setLocalCurrentApp(string $appId, int $appNumber): void { |
||
| 251 | |||
| 252 | |||
| 253 | /** |
||
| 254 | * set a CurrentUser, based on a IFederatedUser. |
||
| 255 | * CurrentUser is mainly used to manage rights when requesting the database. |
||
| 256 | * |
||
| 257 | * @param IFederatedUser $federatedUser |
||
| 258 | * |
||
| 259 | * @throws FederatedUserException |
||
| 260 | */ |
||
| 261 | public function setCurrentUser(IFederatedUser $federatedUser): void { |
||
| 272 | |||
| 273 | /** |
||
| 274 | * |
||
| 275 | */ |
||
| 276 | public function unsetCurrentUser(): void { |
||
| 279 | |||
| 280 | /** |
||
| 281 | * @return FederatedUser|null |
||
| 282 | */ |
||
| 283 | public function getCurrentUser(): ?FederatedUser { |
||
| 286 | |||
| 287 | /** |
||
| 288 | * @return bool |
||
| 289 | */ |
||
| 290 | public function hasCurrentUser(): bool { |
||
| 293 | |||
| 294 | /** |
||
| 295 | * @throws InitiatorNotFoundException |
||
| 296 | */ |
||
| 297 | public function mustHaveCurrentUser(): void { |
||
| 305 | |||
| 306 | /** |
||
| 307 | * @param bool $bypass |
||
| 308 | */ |
||
| 309 | public function bypassCurrentUserCondition(bool $bypass): void { |
||
| 312 | |||
| 313 | |||
| 314 | /** |
||
| 315 | * @param bool $initiatedByOcc |
||
| 316 | */ |
||
| 317 | public function setInitiatedByOcc(bool $initiatedByOcc): void { |
||
| 320 | |||
| 321 | /** |
||
| 322 | * @return bool |
||
| 323 | */ |
||
| 324 | public function isInitiatedByOcc(): bool { |
||
| 327 | |||
| 328 | /** |
||
| 329 | * @param Member $member |
||
| 330 | * |
||
| 331 | * @throws ContactAddressBookNotFoundException |
||
| 332 | * @throws ContactFormatException |
||
| 333 | * @throws ContactNotFoundException |
||
| 334 | * @throws FederatedUserException |
||
| 335 | * @throws InvalidIdException |
||
| 336 | * @throws RequestBuilderException |
||
| 337 | * @throws SingleCircleNotFoundException |
||
| 338 | */ |
||
| 339 | public function setMemberPatron(Member $member): void { |
||
| 346 | |||
| 347 | |||
| 348 | /** |
||
| 349 | * @return FederatedUser|null |
||
| 350 | */ |
||
| 351 | public function getCurrentApp(): ?FederatedUser { |
||
| 354 | |||
| 355 | /** |
||
| 356 | * @return bool |
||
| 357 | */ |
||
| 358 | public function hasCurrentApp(): bool { |
||
| 361 | |||
| 362 | |||
| 363 | /** |
||
| 364 | * set a RemoteInstance, mostly from a remote request (RemoteController) |
||
| 365 | * Used to limit rights in some request in the local database. |
||
| 366 | * |
||
| 367 | * @param RemoteInstance $remoteInstance |
||
| 368 | */ |
||
| 369 | public function setRemoteInstance(RemoteInstance $remoteInstance): void { |
||
| 372 | |||
| 373 | /** |
||
| 374 | * @return RemoteInstance|null |
||
| 375 | */ |
||
| 376 | public function getRemoteInstance(): ?RemoteInstance { |
||
| 379 | |||
| 380 | /** |
||
| 381 | * @return bool |
||
| 382 | */ |
||
| 383 | public function hasRemoteInstance(): bool { |
||
| 386 | |||
| 387 | |||
| 388 | /** |
||
| 389 | * Get the full FederatedUser for a local user. |
||
| 390 | * Will generate the SingleId if none exist |
||
| 391 | * |
||
| 392 | * @param string $userId |
||
| 393 | * @param bool $check |
||
| 394 | * |
||
| 395 | * @return FederatedUser |
||
| 396 | * @throws ContactAddressBookNotFoundException |
||
| 397 | * @throws ContactFormatException |
||
| 398 | * @throws ContactNotFoundException |
||
| 399 | * @throws FederatedUserException |
||
| 400 | * @throws FederatedUserNotFoundException |
||
| 401 | * @throws InvalidIdException |
||
| 402 | * @throws RequestBuilderException |
||
| 403 | * @throws SingleCircleNotFoundException |
||
| 404 | */ |
||
| 405 | public function getLocalFederatedUser(string $userId, bool $check = true): FederatedUser { |
||
| 422 | |||
| 423 | |||
| 424 | /** |
||
| 425 | * Get the full FederatedUser for a local user. |
||
| 426 | * Will generate the SingleId if none exist |
||
| 427 | * |
||
| 428 | * @param string $appId |
||
| 429 | * @param int $appNumber |
||
| 430 | * |
||
| 431 | * @return FederatedUser |
||
| 432 | * @throws ContactAddressBookNotFoundException |
||
| 433 | * @throws ContactFormatException |
||
| 434 | * @throws ContactNotFoundException |
||
| 435 | * @throws FederatedUserException |
||
| 436 | * @throws InvalidIdException |
||
| 437 | * @throws RequestBuilderException |
||
| 438 | * @throws SingleCircleNotFoundException |
||
| 439 | */ |
||
| 440 | public function getAppInitiator( |
||
| 457 | |||
| 458 | |||
| 459 | /** |
||
| 460 | * some ./occ commands allows to add an Initiator, or force the PoV from the local circles' owner |
||
| 461 | * |
||
| 462 | * TODO: manage non-user type ? |
||
| 463 | * |
||
| 464 | * @param string $userId |
||
| 465 | * @param int $userType |
||
| 466 | * @param string $circleId |
||
| 467 | * @param bool $bypass |
||
| 468 | * |
||
| 469 | * @throws CircleNotFoundException |
||
| 470 | * @throws FederatedItemException |
||
| 471 | * @throws FederatedUserException |
||
| 472 | * @throws FederatedUserNotFoundException |
||
| 473 | * @throws InvalidIdException |
||
| 474 | * @throws MemberNotFoundException |
||
| 475 | * @throws OwnerNotFoundException |
||
| 476 | * @throws RemoteInstanceException |
||
| 477 | * @throws RemoteNotFoundException |
||
| 478 | * @throws RemoteResourceNotFoundException |
||
| 479 | * @throws RequestBuilderException |
||
| 480 | * @throws SingleCircleNotFoundException |
||
| 481 | * @throws UnknownRemoteException |
||
| 482 | * @throws UserTypeNotFoundException |
||
| 483 | */ |
||
| 484 | public function commandLineInitiator( |
||
| 513 | |||
| 514 | |||
| 515 | /** |
||
| 516 | * Works like getFederatedUser, but returns a member. |
||
| 517 | * Allow to specify a level: handle@instance,level |
||
| 518 | * |
||
| 519 | * Used for filters when searching for Circles |
||
| 520 | * TODO: Used outside of ./occ circles:manage:list ? |
||
| 521 | * |
||
| 522 | * @param string $userId |
||
| 523 | * @param int $level |
||
| 524 | * |
||
| 525 | * @return Member |
||
| 526 | * @throws CircleNotFoundException |
||
| 527 | * @throws FederatedItemException |
||
| 528 | * @throws FederatedUserException |
||
| 529 | * @throws FederatedUserNotFoundException |
||
| 530 | * @throws InvalidIdException |
||
| 531 | * @throws MemberNotFoundException |
||
| 532 | * @throws OwnerNotFoundException |
||
| 533 | * @throws RemoteInstanceException |
||
| 534 | * @throws RemoteNotFoundException |
||
| 535 | * @throws RemoteResourceNotFoundException |
||
| 536 | * @throws SingleCircleNotFoundException |
||
| 537 | * @throws UnknownRemoteException |
||
| 538 | * @throws UserTypeNotFoundException |
||
| 539 | */ |
||
| 540 | public function getFederatedMember(string $userId, int $level = Member::LEVEL_MEMBER): Member { |
||
| 553 | |||
| 554 | |||
| 555 | /** |
||
| 556 | * get a valid FederatedUser, based on the federatedId (userId@instance) and its type. |
||
| 557 | * If instance is local, get the local valid FederatedUser |
||
| 558 | * If instance is not local, get the remote valid FederatedUser |
||
| 559 | * |
||
| 560 | * @param string $federatedId |
||
| 561 | * @param int $type |
||
| 562 | * |
||
| 563 | * @return FederatedUser |
||
| 564 | * @throws CircleNotFoundException |
||
| 565 | * @throws FederatedItemException |
||
| 566 | * @throws FederatedUserException |
||
| 567 | * @throws FederatedUserNotFoundException |
||
| 568 | * @throws InvalidIdException |
||
| 569 | * @throws MemberNotFoundException |
||
| 570 | * @throws OwnerNotFoundException |
||
| 571 | * @throws RemoteInstanceException |
||
| 572 | * @throws RemoteNotFoundException |
||
| 573 | * @throws RemoteResourceNotFoundException |
||
| 574 | * @throws SingleCircleNotFoundException |
||
| 575 | * @throws UnknownRemoteException |
||
| 576 | * @throws UserTypeNotFoundException |
||
| 577 | * @throws RequestBuilderException |
||
| 578 | */ |
||
| 579 | public function getFederatedUser(string $federatedId, int $type = Member::TYPE_SINGLE): FederatedUser { |
||
| 605 | |||
| 606 | |||
| 607 | /** |
||
| 608 | * Generate a FederatedUser based on local data. |
||
| 609 | * WARNING: There is no confirmation that the returned FederatedUser exists or is valid at this point. |
||
| 610 | * Use getFederatedUser() instead if a valid and confirmed FederatedUser is needed. |
||
| 611 | * |
||
| 612 | * if $federatedId is a known SingleId, will returns data from the local database. |
||
| 613 | * if $federatedId is a local username, will returns data from the local database. |
||
| 614 | * Otherwise, the FederatedUser will not contains a SingleId. |
||
| 615 | * |
||
| 616 | * @param string $federatedId |
||
| 617 | * @param int $type |
||
| 618 | * |
||
| 619 | * @return FederatedUser |
||
| 620 | */ |
||
| 621 | public function generateFederatedUser(string $federatedId, int $type = 0): FederatedUser { |
||
| 633 | |||
| 634 | |||
| 635 | /** |
||
| 636 | * @param FederatedUser $federatedUser |
||
| 637 | */ |
||
| 638 | public function deleteFederatedUser(FederatedUser $federatedUser): void { |
||
| 643 | |||
| 644 | |||
| 645 | /** |
||
| 646 | * @param string $singleId |
||
| 647 | * @param string $instance |
||
| 648 | * |
||
| 649 | * @return FederatedUser |
||
| 650 | * @throws CircleNotFoundException |
||
| 651 | * @throws FederatedItemException |
||
| 652 | * @throws FederatedUserException |
||
| 653 | * @throws FederatedUserNotFoundException |
||
| 654 | * @throws MemberNotFoundException |
||
| 655 | * @throws OwnerNotFoundException |
||
| 656 | * @throws RemoteInstanceException |
||
| 657 | * @throws RemoteNotFoundException |
||
| 658 | * @throws RemoteResourceNotFoundException |
||
| 659 | * @throws UnknownRemoteException |
||
| 660 | * @throws RequestBuilderException |
||
| 661 | */ |
||
| 662 | public function getFederatedUser_SingleId(string $singleId, string $instance): FederatedUser { |
||
| 677 | |||
| 678 | |||
| 679 | /** |
||
| 680 | * @param string $userId |
||
| 681 | * @param string $instance |
||
| 682 | * |
||
| 683 | * @return FederatedUser |
||
| 684 | * @throws FederatedUserException |
||
| 685 | * @throws FederatedUserNotFoundException |
||
| 686 | * @throws InvalidIdException |
||
| 687 | * @throws RemoteInstanceException |
||
| 688 | * @throws RemoteNotFoundException |
||
| 689 | * @throws RemoteResourceNotFoundException |
||
| 690 | * @throws SingleCircleNotFoundException |
||
| 691 | * @throws UnknownRemoteException |
||
| 692 | * @throws FederatedItemException |
||
| 693 | * @throws RequestBuilderException |
||
| 694 | */ |
||
| 695 | private function getFederatedUser_User(string $userId, string $instance): FederatedUser { |
||
| 710 | |||
| 711 | |||
| 712 | /** |
||
| 713 | * @param string $groupName |
||
| 714 | * @param string $instance |
||
| 715 | * |
||
| 716 | * @return FederatedUser |
||
| 717 | * @throws FederatedEventException |
||
| 718 | * @throws FederatedItemException |
||
| 719 | * @throws FederatedUserException |
||
| 720 | * @throws GroupNotFoundException |
||
| 721 | * @throws InitiatorNotConfirmedException |
||
| 722 | * @throws InvalidIdException |
||
| 723 | * @throws OwnerNotFoundException |
||
| 724 | * @throws RemoteInstanceException |
||
| 725 | * @throws RemoteNotFoundException |
||
| 726 | * @throws RemoteResourceNotFoundException |
||
| 727 | * @throws SingleCircleNotFoundException |
||
| 728 | * @throws UnknownRemoteException |
||
| 729 | * @throws RequestBuilderException |
||
| 730 | */ |
||
| 731 | public function getFederatedUser_Group(string $groupName, string $instance): FederatedUser { |
||
| 741 | |||
| 742 | |||
| 743 | /** |
||
| 744 | * @param string $mailAddress |
||
| 745 | * |
||
| 746 | * @return FederatedUser |
||
| 747 | * @throws ContactAddressBookNotFoundException |
||
| 748 | * @throws ContactFormatException |
||
| 749 | * @throws ContactNotFoundException |
||
| 750 | * @throws FederatedUserException |
||
| 751 | * @throws InvalidIdException |
||
| 752 | * @throws RequestBuilderException |
||
| 753 | * @throws SingleCircleNotFoundException |
||
| 754 | */ |
||
| 755 | public function getFederatedUser_Mail(string $mailAddress): FederatedUser { |
||
| 762 | |||
| 763 | |||
| 764 | /** |
||
| 765 | * @param string $contactPath |
||
| 766 | * |
||
| 767 | * @return FederatedUser |
||
| 768 | * @throws ContactAddressBookNotFoundException |
||
| 769 | * @throws ContactFormatException |
||
| 770 | * @throws ContactNotFoundException |
||
| 771 | * @throws FederatedUserException |
||
| 772 | * @throws InvalidIdException |
||
| 773 | * @throws RequestBuilderException |
||
| 774 | * @throws SingleCircleNotFoundException |
||
| 775 | */ |
||
| 776 | public function getFederatedUser_Contact(string $contactPath): FederatedUser { |
||
| 789 | |||
| 790 | |||
| 791 | /** |
||
| 792 | * extract userID and instance from a federatedId |
||
| 793 | * |
||
| 794 | * @param string $federatedId |
||
| 795 | * |
||
| 796 | * @return array |
||
| 797 | */ |
||
| 798 | public function extractIdAndInstance(string $federatedId): array { |
||
| 809 | |||
| 810 | |||
| 811 | /** |
||
| 812 | * @param FederatedUser $federatedUser |
||
| 813 | * @param bool $generate |
||
| 814 | * |
||
| 815 | * @throws ContactAddressBookNotFoundException |
||
| 816 | * @throws ContactFormatException |
||
| 817 | * @throws ContactNotFoundException |
||
| 818 | * @throws FederatedUserException |
||
| 819 | * @throws InvalidIdException |
||
| 820 | * @throws RequestBuilderException |
||
| 821 | * @throws SingleCircleNotFoundException |
||
| 822 | */ |
||
| 823 | private function fillSingleCircleId(FederatedUser $federatedUser, bool $generate = true): void { |
||
| 833 | |||
| 834 | |||
| 835 | /** |
||
| 836 | * get the Single Circle from a local user |
||
| 837 | * |
||
| 838 | * @param FederatedUser $federatedUser |
||
| 839 | * @param bool $generate |
||
| 840 | * |
||
| 841 | * @return Circle |
||
| 842 | * @throws ContactAddressBookNotFoundException |
||
| 843 | * @throws ContactFormatException |
||
| 844 | * @throws ContactNotFoundException |
||
| 845 | * @throws FederatedUserException |
||
| 846 | * @throws InvalidIdException |
||
| 847 | * @throws RequestBuilderException |
||
| 848 | * @throws SingleCircleNotFoundException |
||
| 849 | */ |
||
| 850 | private function getSingleCircle(FederatedUser $federatedUser, bool $generate = true): Circle { |
||
| 912 | |||
| 913 | |||
| 914 | /** |
||
| 915 | * Confirm that all field of a FederatedUser are filled. |
||
| 916 | * |
||
| 917 | * @param FederatedUser $federatedUser |
||
| 918 | * |
||
| 919 | * @throws FederatedUserException |
||
| 920 | */ |
||
| 921 | private function confirmFederatedUser(FederatedUser $federatedUser): void { |
||
| 930 | |||
| 931 | /** |
||
| 932 | * Confirm that the singleId of a FederatedUser is unique and not used to any other member of the |
||
| 933 | * database. |
||
| 934 | * |
||
| 935 | * @param FederatedUser $federatedUser |
||
| 936 | * |
||
| 937 | * @throws FederatedUserException |
||
| 938 | * @throws RequestBuilderException |
||
| 939 | * @deprecated: use confirmSingleIdUniqueness() |
||
| 940 | */ |
||
| 941 | public function confirmLocalSingleId(IFederatedUser $federatedUser): void { |
||
| 954 | |||
| 955 | |||
| 956 | /** |
||
| 957 | * // TODO: implement this check in a maintenance background job |
||
| 958 | * |
||
| 959 | * @param IFederatedUser $federatedUser |
||
| 960 | * |
||
| 961 | * @throws FederatedUserException |
||
| 962 | * @throws RequestBuilderException |
||
| 963 | */ |
||
| 964 | public function confirmSingleIdUniqueness(IFederatedUser $federatedUser): void { |
||
| 1004 | |||
| 1005 | |||
| 1006 | /** |
||
| 1007 | * @param string $groupId |
||
| 1008 | * |
||
| 1009 | * @return Circle |
||
| 1010 | * @throws GroupNotFoundException |
||
| 1011 | * @throws FederatedEventException |
||
| 1012 | * @throws FederatedItemException |
||
| 1013 | * @throws FederatedUserException |
||
| 1014 | * @throws InitiatorNotConfirmedException |
||
| 1015 | * @throws InvalidIdException |
||
| 1016 | * @throws OwnerNotFoundException |
||
| 1017 | * @throws RemoteInstanceException |
||
| 1018 | * @throws RemoteNotFoundException |
||
| 1019 | * @throws RemoteResourceNotFoundException |
||
| 1020 | * @throws SingleCircleNotFoundException |
||
| 1021 | * @throws UnknownRemoteException |
||
| 1022 | * @throws RequestBuilderException |
||
| 1023 | */ |
||
| 1024 | public function getGroupCircle(string $groupId): Circle { |
||
| 1061 | |||
| 1062 | } |
||
| 1063 | |||
| 1064 |
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: