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 { |
||
| 409 | |||
| 410 | |||
| 411 | /** |
||
| 412 | * Get the full FederatedUser for a local user. |
||
| 413 | * Will generate the SingleId if none exist |
||
| 414 | * |
||
| 415 | * @param string $appId |
||
| 416 | * @param int $appNumber |
||
| 417 | * |
||
| 418 | * @return FederatedUser |
||
| 419 | * @throws ContactAddressBookNotFoundException |
||
| 420 | * @throws ContactFormatException |
||
| 421 | * @throws ContactNotFoundException |
||
| 422 | * @throws FederatedUserException |
||
| 423 | * @throws InvalidIdException |
||
| 424 | * @throws RequestBuilderException |
||
| 425 | * @throws SingleCircleNotFoundException |
||
| 426 | */ |
||
| 427 | public function getAppInitiator( |
||
| 444 | |||
| 445 | |||
| 446 | /** |
||
| 447 | * some ./occ commands allows to add an Initiator, or force the PoV from the local circles' owner |
||
| 448 | * |
||
| 449 | * TODO: manage non-user type ? |
||
| 450 | * |
||
| 451 | * @param string $userId |
||
| 452 | * @param int $userType |
||
| 453 | * @param string $circleId |
||
| 454 | * @param bool $bypass |
||
| 455 | * |
||
| 456 | * @throws CircleNotFoundException |
||
| 457 | * @throws FederatedItemException |
||
| 458 | * @throws FederatedUserException |
||
| 459 | * @throws FederatedUserNotFoundException |
||
| 460 | * @throws InvalidIdException |
||
| 461 | * @throws MemberNotFoundException |
||
| 462 | * @throws OwnerNotFoundException |
||
| 463 | * @throws RemoteInstanceException |
||
| 464 | * @throws RemoteNotFoundException |
||
| 465 | * @throws RemoteResourceNotFoundException |
||
| 466 | * @throws RequestBuilderException |
||
| 467 | * @throws SingleCircleNotFoundException |
||
| 468 | * @throws UnknownRemoteException |
||
| 469 | * @throws UserTypeNotFoundException |
||
| 470 | */ |
||
| 471 | public function commandLineInitiator( |
||
| 500 | |||
| 501 | |||
| 502 | /** |
||
| 503 | * Works like getFederatedUser, but returns a member. |
||
| 504 | * Allow to specify a level: handle@instance,level |
||
| 505 | * |
||
| 506 | * Used for filters when searching for Circles |
||
| 507 | * TODO: Used outside of ./occ circles:manage:list ? |
||
| 508 | * |
||
| 509 | * @param string $userId |
||
| 510 | * @param int $level |
||
| 511 | * |
||
| 512 | * @return Member |
||
| 513 | * @throws CircleNotFoundException |
||
| 514 | * @throws FederatedItemException |
||
| 515 | * @throws FederatedUserException |
||
| 516 | * @throws FederatedUserNotFoundException |
||
| 517 | * @throws InvalidIdException |
||
| 518 | * @throws MemberNotFoundException |
||
| 519 | * @throws OwnerNotFoundException |
||
| 520 | * @throws RemoteInstanceException |
||
| 521 | * @throws RemoteNotFoundException |
||
| 522 | * @throws RemoteResourceNotFoundException |
||
| 523 | * @throws SingleCircleNotFoundException |
||
| 524 | * @throws UnknownRemoteException |
||
| 525 | * @throws UserTypeNotFoundException |
||
| 526 | */ |
||
| 527 | public function getFederatedMember(string $userId, int $level = Member::LEVEL_MEMBER): Member { |
||
| 540 | |||
| 541 | |||
| 542 | /** |
||
| 543 | * get a valid FederatedUser, based on the federatedId (userId@instance) its the type. |
||
| 544 | * If instance is local, get the local valid FederatedUser |
||
| 545 | * If instance is not local, get the remote valid FederatedUser |
||
| 546 | * |
||
| 547 | * @param string $federatedId |
||
| 548 | * @param int $type |
||
| 549 | * |
||
| 550 | * @return FederatedUser |
||
| 551 | * @throws CircleNotFoundException |
||
| 552 | * @throws FederatedItemException |
||
| 553 | * @throws FederatedUserException |
||
| 554 | * @throws FederatedUserNotFoundException |
||
| 555 | * @throws InvalidIdException |
||
| 556 | * @throws MemberNotFoundException |
||
| 557 | * @throws OwnerNotFoundException |
||
| 558 | * @throws RemoteInstanceException |
||
| 559 | * @throws RemoteNotFoundException |
||
| 560 | * @throws RemoteResourceNotFoundException |
||
| 561 | * @throws SingleCircleNotFoundException |
||
| 562 | * @throws UnknownRemoteException |
||
| 563 | * @throws UserTypeNotFoundException |
||
| 564 | * @throws RequestBuilderException |
||
| 565 | */ |
||
| 566 | public function getFederatedUser(string $federatedId, int $type = Member::TYPE_SINGLE): FederatedUser { |
||
| 591 | |||
| 592 | |||
| 593 | /** |
||
| 594 | * Generate a FederatedUser based on local data. |
||
| 595 | * WARNING: There is no confirmation that the returned FederatedUser exists or is valid at this point. |
||
| 596 | * Use getFederatedUser() instead if a valid and confirmed FederatedUser is needed. |
||
| 597 | * |
||
| 598 | * if $federatedId is a known SingleId, will returns data from the local database. |
||
| 599 | * if $federatedId is a local username, will returns data from the local database. |
||
| 600 | * Otherwise, the FederatedUser will not contains a SingleId. |
||
| 601 | * |
||
| 602 | * @param string $federatedId |
||
| 603 | * @param int $type |
||
| 604 | * |
||
| 605 | * @return FederatedUser |
||
| 606 | */ |
||
| 607 | public function generateFederatedUser(string $federatedId, int $type = 0): FederatedUser { |
||
| 619 | |||
| 620 | |||
| 621 | /** |
||
| 622 | * @param FederatedUser $federatedUser |
||
| 623 | */ |
||
| 624 | public function deleteFederatedUser(FederatedUser $federatedUser): void { |
||
| 629 | |||
| 630 | |||
| 631 | /** |
||
| 632 | * @param string $singleId |
||
| 633 | * @param string $instance |
||
| 634 | * |
||
| 635 | * @return FederatedUser |
||
| 636 | * @throws CircleNotFoundException |
||
| 637 | * @throws FederatedItemException |
||
| 638 | * @throws FederatedUserException |
||
| 639 | * @throws FederatedUserNotFoundException |
||
| 640 | * @throws MemberNotFoundException |
||
| 641 | * @throws OwnerNotFoundException |
||
| 642 | * @throws RemoteInstanceException |
||
| 643 | * @throws RemoteNotFoundException |
||
| 644 | * @throws RemoteResourceNotFoundException |
||
| 645 | * @throws UnknownRemoteException |
||
| 646 | * @throws RequestBuilderException |
||
| 647 | */ |
||
| 648 | public function getFederatedUser_SingleId(string $singleId, string $instance): FederatedUser { |
||
| 663 | |||
| 664 | |||
| 665 | /** |
||
| 666 | * @param string $userId |
||
| 667 | * @param string $instance |
||
| 668 | * |
||
| 669 | * @return FederatedUser |
||
| 670 | * @throws FederatedUserException |
||
| 671 | * @throws FederatedUserNotFoundException |
||
| 672 | * @throws InvalidIdException |
||
| 673 | * @throws RemoteInstanceException |
||
| 674 | * @throws RemoteNotFoundException |
||
| 675 | * @throws RemoteResourceNotFoundException |
||
| 676 | * @throws SingleCircleNotFoundException |
||
| 677 | * @throws UnknownRemoteException |
||
| 678 | * @throws FederatedItemException |
||
| 679 | * @throws RequestBuilderException |
||
| 680 | */ |
||
| 681 | private function getFederatedUser_User(string $userId, string $instance): FederatedUser { |
||
| 692 | |||
| 693 | |||
| 694 | /** |
||
| 695 | * @param string $groupName |
||
| 696 | * @param string $instance |
||
| 697 | * |
||
| 698 | * @return FederatedUser |
||
| 699 | * @throws FederatedEventException |
||
| 700 | * @throws FederatedItemException |
||
| 701 | * @throws FederatedUserException |
||
| 702 | * @throws GroupNotFoundException |
||
| 703 | * @throws InitiatorNotConfirmedException |
||
| 704 | * @throws InvalidIdException |
||
| 705 | * @throws OwnerNotFoundException |
||
| 706 | * @throws RemoteInstanceException |
||
| 707 | * @throws RemoteNotFoundException |
||
| 708 | * @throws RemoteResourceNotFoundException |
||
| 709 | * @throws SingleCircleNotFoundException |
||
| 710 | * @throws UnknownRemoteException |
||
| 711 | */ |
||
| 712 | public function getFederatedUser_Group(string $groupName, string $instance): FederatedUser { |
||
| 722 | |||
| 723 | |||
| 724 | /** |
||
| 725 | * @param string $mailAddress |
||
| 726 | * |
||
| 727 | * @return FederatedUser |
||
| 728 | * @throws ContactAddressBookNotFoundException |
||
| 729 | * @throws ContactFormatException |
||
| 730 | * @throws ContactNotFoundException |
||
| 731 | * @throws FederatedUserException |
||
| 732 | * @throws InvalidIdException |
||
| 733 | * @throws RequestBuilderException |
||
| 734 | * @throws SingleCircleNotFoundException |
||
| 735 | */ |
||
| 736 | public function getFederatedUser_Mail(string $mailAddress): FederatedUser { |
||
| 743 | |||
| 744 | |||
| 745 | /** |
||
| 746 | * @param string $contactPath |
||
| 747 | * |
||
| 748 | * @return FederatedUser |
||
| 749 | * @throws ContactAddressBookNotFoundException |
||
| 750 | * @throws ContactFormatException |
||
| 751 | * @throws ContactNotFoundException |
||
| 752 | * @throws FederatedUserException |
||
| 753 | * @throws InvalidIdException |
||
| 754 | * @throws RequestBuilderException |
||
| 755 | * @throws SingleCircleNotFoundException |
||
| 756 | */ |
||
| 757 | public function getFederatedUser_Contact(string $contactPath): FederatedUser { |
||
| 770 | |||
| 771 | |||
| 772 | /** |
||
| 773 | * extract userID and instance from a federatedId |
||
| 774 | * |
||
| 775 | * @param string $federatedId |
||
| 776 | * |
||
| 777 | * @return array |
||
| 778 | */ |
||
| 779 | public function extractIdAndInstance(string $federatedId): array { |
||
| 790 | |||
| 791 | |||
| 792 | /** |
||
| 793 | * @param FederatedUser $federatedUser |
||
| 794 | * @param bool $generate |
||
| 795 | * |
||
| 796 | * @throws ContactAddressBookNotFoundException |
||
| 797 | * @throws ContactFormatException |
||
| 798 | * @throws ContactNotFoundException |
||
| 799 | * @throws FederatedUserException |
||
| 800 | * @throws InvalidIdException |
||
| 801 | * @throws RequestBuilderException |
||
| 802 | * @throws SingleCircleNotFoundException |
||
| 803 | */ |
||
| 804 | private function fillSingleCircleId(FederatedUser $federatedUser, bool $generate = true): void { |
||
| 814 | |||
| 815 | |||
| 816 | /** |
||
| 817 | * get the Single Circle from a local user |
||
| 818 | * |
||
| 819 | * @param FederatedUser $federatedUser |
||
| 820 | * @param bool $generate |
||
| 821 | * |
||
| 822 | * @return Circle |
||
| 823 | * @throws ContactAddressBookNotFoundException |
||
| 824 | * @throws ContactFormatException |
||
| 825 | * @throws ContactNotFoundException |
||
| 826 | * @throws FederatedUserException |
||
| 827 | * @throws InvalidIdException |
||
| 828 | * @throws RequestBuilderException |
||
| 829 | * @throws SingleCircleNotFoundException |
||
| 830 | */ |
||
| 831 | private function getSingleCircle(FederatedUser $federatedUser, bool $generate = true): Circle { |
||
| 893 | |||
| 894 | |||
| 895 | /** |
||
| 896 | * Confirm that all field of a FederatedUser are filled. |
||
| 897 | * |
||
| 898 | * @param FederatedUser $federatedUser |
||
| 899 | * |
||
| 900 | * @throws FederatedUserException |
||
| 901 | */ |
||
| 902 | private function confirmFederatedUser(FederatedUser $federatedUser): void { |
||
| 911 | |||
| 912 | /** |
||
| 913 | * Confirm that the singleId of a FederatedUser is unique and not used to any other member of the |
||
| 914 | * database. |
||
| 915 | * |
||
| 916 | * @param FederatedUser $federatedUser |
||
| 917 | * |
||
| 918 | * @throws FederatedUserException |
||
| 919 | * @throws RequestBuilderException |
||
| 920 | */ |
||
| 921 | public function confirmLocalSingleId(IFederatedUser $federatedUser): void { |
||
| 934 | |||
| 935 | |||
| 936 | /** |
||
| 937 | * @param IFederatedUser $federatedUser |
||
| 938 | * |
||
| 939 | * @throws FederatedUserException |
||
| 940 | * @throws RequestBuilderException |
||
| 941 | */ |
||
| 942 | public function confirmSingleIdUniqueness(IFederatedUser $federatedUser): void { |
||
| 949 | |||
| 950 | |||
| 951 | /** |
||
| 952 | * @param string $groupId |
||
| 953 | * |
||
| 954 | * @return Circle |
||
| 955 | * @throws GroupNotFoundException |
||
| 956 | * @throws FederatedEventException |
||
| 957 | * @throws FederatedItemException |
||
| 958 | * @throws FederatedUserException |
||
| 959 | * @throws InitiatorNotConfirmedException |
||
| 960 | * @throws InvalidIdException |
||
| 961 | * @throws OwnerNotFoundException |
||
| 962 | * @throws RemoteInstanceException |
||
| 963 | * @throws RemoteNotFoundException |
||
| 964 | * @throws RemoteResourceNotFoundException |
||
| 965 | * @throws SingleCircleNotFoundException |
||
| 966 | * @throws UnknownRemoteException |
||
| 967 | * @throws RequestBuilderException |
||
| 968 | */ |
||
| 969 | public function getGroupCircle(string $groupId): Circle { |
||
| 1006 | |||
| 1007 | } |
||
| 1008 | |||
| 1009 |
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: