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 | */ |
||
| 186 | public function initCurrentUser() { |
||
| 194 | |||
| 195 | |||
| 196 | /** |
||
| 197 | * @param IUser|null $user |
||
| 198 | * |
||
| 199 | * @throws FederatedUserException |
||
| 200 | * @throws FederatedUserNotFoundException |
||
| 201 | * @throws InvalidIdException |
||
| 202 | * @throws SingleCircleNotFoundException |
||
| 203 | * @throws RequestBuilderException |
||
| 204 | */ |
||
| 205 | public function setLocalCurrentUser(?IUser $user): void { |
||
| 212 | |||
| 213 | /** |
||
| 214 | * @param string $userId |
||
| 215 | * |
||
| 216 | * @throws FederatedUserException |
||
| 217 | * @throws FederatedUserNotFoundException |
||
| 218 | * @throws InvalidIdException |
||
| 219 | * @throws RequestBuilderException |
||
| 220 | * @throws SingleCircleNotFoundException |
||
| 221 | */ |
||
| 222 | public function setLocalCurrentUserId(string $userId): void { |
||
| 225 | |||
| 226 | /** |
||
| 227 | * @param string $appId |
||
| 228 | * @param int $appNumber |
||
| 229 | * |
||
| 230 | * @throws FederatedUserException |
||
| 231 | * @throws InvalidIdException |
||
| 232 | * @throws SingleCircleNotFoundException |
||
| 233 | */ |
||
| 234 | public function setLocalCurrentApp(string $appId, int $appNumber): void { |
||
| 237 | |||
| 238 | |||
| 239 | /** |
||
| 240 | * set a CurrentUser, based on a IFederatedUser. |
||
| 241 | * CurrentUser is mainly used to manage rights when requesting the database. |
||
| 242 | * |
||
| 243 | * @param IFederatedUser $federatedUser |
||
| 244 | * |
||
| 245 | * @throws FederatedUserException |
||
| 246 | */ |
||
| 247 | public function setCurrentUser(IFederatedUser $federatedUser): void { |
||
| 258 | |||
| 259 | /** |
||
| 260 | * @return FederatedUser|null |
||
| 261 | */ |
||
| 262 | public function getCurrentUser(): ?FederatedUser { |
||
| 265 | |||
| 266 | /** |
||
| 267 | * @return bool |
||
| 268 | */ |
||
| 269 | public function hasCurrentUser(): bool { |
||
| 272 | |||
| 273 | /** |
||
| 274 | * @throws InitiatorNotFoundException |
||
| 275 | */ |
||
| 276 | public function mustHaveCurrentUser(): void { |
||
| 284 | |||
| 285 | /** |
||
| 286 | * @param bool $bypass |
||
| 287 | */ |
||
| 288 | public function bypassCurrentUserCondition(bool $bypass): void { |
||
| 291 | |||
| 292 | |||
| 293 | /** |
||
| 294 | * @param bool $initiatedByOcc |
||
| 295 | */ |
||
| 296 | public function setInitiatedByOcc(bool $initiatedByOcc): void { |
||
| 299 | |||
| 300 | /** |
||
| 301 | * @return bool |
||
| 302 | */ |
||
| 303 | public function isInitiatedByOcc(): bool { |
||
| 306 | |||
| 307 | /** |
||
| 308 | * @param Member $member |
||
| 309 | * |
||
| 310 | * @throws ContactAddressBookNotFoundException |
||
| 311 | * @throws ContactFormatException |
||
| 312 | * @throws ContactNotFoundException |
||
| 313 | * @throws FederatedUserException |
||
| 314 | * @throws InvalidIdException |
||
| 315 | * @throws RequestBuilderException |
||
| 316 | * @throws SingleCircleNotFoundException |
||
| 317 | */ |
||
| 318 | public function setMemberPatron(Member $member): void { |
||
| 325 | |||
| 326 | |||
| 327 | /** |
||
| 328 | * @return FederatedUser|null |
||
| 329 | */ |
||
| 330 | public function getCurrentApp(): ?FederatedUser { |
||
| 333 | |||
| 334 | /** |
||
| 335 | * @return bool |
||
| 336 | */ |
||
| 337 | public function hasCurrentApp(): bool { |
||
| 340 | |||
| 341 | |||
| 342 | /** |
||
| 343 | * set a RemoteInstance, mostly from a remote request (RemoteController) |
||
| 344 | * Used to limit rights in some request in the local database. |
||
| 345 | * |
||
| 346 | * @param RemoteInstance $remoteInstance |
||
| 347 | */ |
||
| 348 | public function setRemoteInstance(RemoteInstance $remoteInstance): void { |
||
| 351 | |||
| 352 | /** |
||
| 353 | * @return RemoteInstance|null |
||
| 354 | */ |
||
| 355 | public function getRemoteInstance(): ?RemoteInstance { |
||
| 358 | |||
| 359 | /** |
||
| 360 | * @return bool |
||
| 361 | */ |
||
| 362 | public function hasRemoteInstance(): bool { |
||
| 365 | |||
| 366 | |||
| 367 | /** |
||
| 368 | * Get the full FederatedUser for a local user. |
||
| 369 | * Will generate the SingleId if none exist |
||
| 370 | * |
||
| 371 | * @param string $userId |
||
| 372 | * |
||
| 373 | * @return FederatedUser |
||
| 374 | * @throws FederatedUserNotFoundException |
||
| 375 | * @throws InvalidIdException |
||
| 376 | * @throws SingleCircleNotFoundException |
||
| 377 | * @throws FederatedUserException |
||
| 378 | * @throws RequestBuilderException |
||
| 379 | */ |
||
| 380 | public function getLocalFederatedUser(string $userId): FederatedUser { |
||
| 392 | |||
| 393 | |||
| 394 | /** |
||
| 395 | * Get the full FederatedUser for a local user. |
||
| 396 | * Will generate the SingleId if none exist |
||
| 397 | * |
||
| 398 | * @param string $appId |
||
| 399 | * @param int $appNumber |
||
| 400 | * |
||
| 401 | * @return FederatedUser |
||
| 402 | * @throws ContactAddressBookNotFoundException |
||
| 403 | * @throws ContactFormatException |
||
| 404 | * @throws ContactNotFoundException |
||
| 405 | * @throws FederatedUserException |
||
| 406 | * @throws InvalidIdException |
||
| 407 | * @throws RequestBuilderException |
||
| 408 | * @throws SingleCircleNotFoundException |
||
| 409 | */ |
||
| 410 | public function getAppInitiator(string $appId, int $appNumber): FederatedUser { |
||
| 421 | |||
| 422 | |||
| 423 | /** |
||
| 424 | * some ./occ commands allows to add an Initiator, or force the PoV from the local circles' owner |
||
| 425 | * |
||
| 426 | * TODO: manage non-user type ? |
||
| 427 | * |
||
| 428 | * @param string $userId |
||
| 429 | * @param int $userType |
||
| 430 | * @param string $circleId |
||
| 431 | * @param bool $bypass |
||
| 432 | * |
||
| 433 | * @throws CircleNotFoundException |
||
| 434 | * @throws FederatedItemException |
||
| 435 | * @throws FederatedUserException |
||
| 436 | * @throws FederatedUserNotFoundException |
||
| 437 | * @throws InvalidIdException |
||
| 438 | * @throws MemberNotFoundException |
||
| 439 | * @throws OwnerNotFoundException |
||
| 440 | * @throws RemoteInstanceException |
||
| 441 | * @throws RemoteNotFoundException |
||
| 442 | * @throws RemoteResourceNotFoundException |
||
| 443 | * @throws RequestBuilderException |
||
| 444 | * @throws SingleCircleNotFoundException |
||
| 445 | * @throws UnknownRemoteException |
||
| 446 | * @throws UserTypeNotFoundException |
||
| 447 | */ |
||
| 448 | public function commandLineInitiator( |
||
| 477 | |||
| 478 | |||
| 479 | /** |
||
| 480 | * Works like getFederatedUser, but returns a member. |
||
| 481 | * Allow to specify a level: handle@instance,level |
||
| 482 | * |
||
| 483 | * Used for filters when searching for Circles |
||
| 484 | * TODO: Used outside of ./occ circles:manage:list ? |
||
| 485 | * |
||
| 486 | * @param string $userId |
||
| 487 | * @param int $level |
||
| 488 | * |
||
| 489 | * @return Member |
||
| 490 | * @throws CircleNotFoundException |
||
| 491 | * @throws FederatedItemException |
||
| 492 | * @throws FederatedUserException |
||
| 493 | * @throws FederatedUserNotFoundException |
||
| 494 | * @throws InvalidIdException |
||
| 495 | * @throws MemberNotFoundException |
||
| 496 | * @throws OwnerNotFoundException |
||
| 497 | * @throws RemoteInstanceException |
||
| 498 | * @throws RemoteNotFoundException |
||
| 499 | * @throws RemoteResourceNotFoundException |
||
| 500 | * @throws SingleCircleNotFoundException |
||
| 501 | * @throws UnknownRemoteException |
||
| 502 | * @throws UserTypeNotFoundException |
||
| 503 | */ |
||
| 504 | public function getFederatedMember(string $userId, int $level = Member::LEVEL_MEMBER): Member { |
||
| 517 | |||
| 518 | |||
| 519 | /** |
||
| 520 | * get a valid FederatedUser, based on the federatedId (userId@instance) its the type. |
||
| 521 | * If instance is local, get the local valid FederatedUser |
||
| 522 | * If instance is not local, get the remote valid FederatedUser |
||
| 523 | * |
||
| 524 | * @param string $federatedId |
||
| 525 | * @param int $type |
||
| 526 | * |
||
| 527 | * @return FederatedUser |
||
| 528 | * @throws CircleNotFoundException |
||
| 529 | * @throws FederatedItemException |
||
| 530 | * @throws FederatedUserException |
||
| 531 | * @throws FederatedUserNotFoundException |
||
| 532 | * @throws InvalidIdException |
||
| 533 | * @throws MemberNotFoundException |
||
| 534 | * @throws OwnerNotFoundException |
||
| 535 | * @throws RemoteInstanceException |
||
| 536 | * @throws RemoteNotFoundException |
||
| 537 | * @throws RemoteResourceNotFoundException |
||
| 538 | * @throws SingleCircleNotFoundException |
||
| 539 | * @throws UnknownRemoteException |
||
| 540 | * @throws UserTypeNotFoundException |
||
| 541 | * @throws RequestBuilderException |
||
| 542 | */ |
||
| 543 | public function getFederatedUser(string $federatedId, int $type = Member::TYPE_SINGLE): FederatedUser { |
||
| 568 | |||
| 569 | |||
| 570 | /** |
||
| 571 | * Generate a FederatedUser based on local data. |
||
| 572 | * WARNING: There is no confirmation that the returned FederatedUser exists or is valid at this point. |
||
| 573 | * Use getFederatedUser() instead if a valid and confirmed FederatedUser is needed. |
||
| 574 | * |
||
| 575 | * if $federatedId is a known SingleId, will returns data from the local database. |
||
| 576 | * if $federatedId is a local username, will returns data from the local database. |
||
| 577 | * Otherwise, the FederatedUser will not contains a SingleId. |
||
| 578 | * |
||
| 579 | * @param string $federatedId |
||
| 580 | * @param int $type |
||
| 581 | * |
||
| 582 | * @return FederatedUser |
||
| 583 | */ |
||
| 584 | public function generateFederatedUser(string $federatedId, int $type = 0): FederatedUser { |
||
| 596 | |||
| 597 | |||
| 598 | /** |
||
| 599 | * @param string $singleId |
||
| 600 | * @param string $instance |
||
| 601 | * |
||
| 602 | * @return FederatedUser |
||
| 603 | * @throws CircleNotFoundException |
||
| 604 | * @throws FederatedItemException |
||
| 605 | * @throws FederatedUserException |
||
| 606 | * @throws FederatedUserNotFoundException |
||
| 607 | * @throws MemberNotFoundException |
||
| 608 | * @throws OwnerNotFoundException |
||
| 609 | * @throws RemoteInstanceException |
||
| 610 | * @throws RemoteNotFoundException |
||
| 611 | * @throws RemoteResourceNotFoundException |
||
| 612 | * @throws UnknownRemoteException |
||
| 613 | */ |
||
| 614 | public function getFederatedUser_SingleId(string $singleId, string $instance): FederatedUser { |
||
| 629 | |||
| 630 | |||
| 631 | /** |
||
| 632 | * @param string $userId |
||
| 633 | * @param string $instance |
||
| 634 | * |
||
| 635 | * @return FederatedUser |
||
| 636 | * @throws FederatedUserException |
||
| 637 | * @throws FederatedUserNotFoundException |
||
| 638 | * @throws InvalidIdException |
||
| 639 | * @throws RemoteInstanceException |
||
| 640 | * @throws RemoteNotFoundException |
||
| 641 | * @throws RemoteResourceNotFoundException |
||
| 642 | * @throws SingleCircleNotFoundException |
||
| 643 | * @throws UnknownRemoteException |
||
| 644 | * @throws FederatedItemException |
||
| 645 | */ |
||
| 646 | private function getFederatedUser_User(string $userId, string $instance): FederatedUser { |
||
| 657 | |||
| 658 | |||
| 659 | /** |
||
| 660 | * @param string $groupName |
||
| 661 | * @param string $instance |
||
| 662 | * |
||
| 663 | * @return FederatedUser |
||
| 664 | * @throws FederatedEventException |
||
| 665 | * @throws FederatedItemException |
||
| 666 | * @throws FederatedUserException |
||
| 667 | * @throws GroupNotFoundException |
||
| 668 | * @throws InitiatorNotConfirmedException |
||
| 669 | * @throws InvalidIdException |
||
| 670 | * @throws OwnerNotFoundException |
||
| 671 | * @throws RemoteInstanceException |
||
| 672 | * @throws RemoteNotFoundException |
||
| 673 | * @throws RemoteResourceNotFoundException |
||
| 674 | * @throws SingleCircleNotFoundException |
||
| 675 | * @throws UnknownRemoteException |
||
| 676 | */ |
||
| 677 | public function getFederatedUser_Group(string $groupName, string $instance): FederatedUser { |
||
| 687 | |||
| 688 | |||
| 689 | /** |
||
| 690 | * @param string $mailAddress |
||
| 691 | * |
||
| 692 | * @return FederatedUser |
||
| 693 | * @throws FederatedUserException |
||
| 694 | * @throws InvalidIdException |
||
| 695 | * @throws RequestBuilderException |
||
| 696 | * @throws SingleCircleNotFoundException |
||
| 697 | */ |
||
| 698 | public function getFederatedUser_Mail(string $mailAddress): FederatedUser { |
||
| 705 | |||
| 706 | |||
| 707 | /** |
||
| 708 | * @param string $contactPath |
||
| 709 | * |
||
| 710 | * @return FederatedUser |
||
| 711 | * @throws FederatedUserException |
||
| 712 | * @throws InvalidIdException |
||
| 713 | * @throws RequestBuilderException |
||
| 714 | * @throws SingleCircleNotFoundException |
||
| 715 | */ |
||
| 716 | public function getFederatedUser_Contact(string $contactPath): FederatedUser { |
||
| 726 | |||
| 727 | |||
| 728 | /** |
||
| 729 | * extract userID and instance from a federatedId |
||
| 730 | * |
||
| 731 | * @param string $federatedId |
||
| 732 | * |
||
| 733 | * @return array |
||
| 734 | */ |
||
| 735 | public function extractIdAndInstance(string $federatedId): array { |
||
| 746 | |||
| 747 | |||
| 748 | /** |
||
| 749 | * @param FederatedUser $federatedUser |
||
| 750 | * |
||
| 751 | * @throws ContactAddressBookNotFoundException |
||
| 752 | * @throws ContactFormatException |
||
| 753 | * @throws ContactNotFoundException |
||
| 754 | * @throws FederatedUserException |
||
| 755 | * @throws InvalidIdException |
||
| 756 | * @throws RequestBuilderException |
||
| 757 | * @throws SingleCircleNotFoundException |
||
| 758 | */ |
||
| 759 | private function fillSingleCircleId(FederatedUser $federatedUser): void { |
||
| 768 | |||
| 769 | |||
| 770 | /** |
||
| 771 | * get the Single Circle from a local user |
||
| 772 | * |
||
| 773 | * @param FederatedUser $federatedUser |
||
| 774 | * |
||
| 775 | * @return Circle |
||
| 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 getSingleCircle(FederatedUser $federatedUser): Circle { |
||
| 836 | |||
| 837 | |||
| 838 | /** |
||
| 839 | * @param FederatedUser $federatedUser |
||
| 840 | * |
||
| 841 | * @return string |
||
| 842 | * @throws ContactAddressBookNotFoundException |
||
| 843 | * @throws ContactFormatException |
||
| 844 | * @throws ContactNotFoundException |
||
| 845 | */ |
||
| 846 | private function getLocalDisplayName(FederatedUser $federatedUser): string { |
||
| 853 | |||
| 854 | |||
| 855 | /** |
||
| 856 | * Confirm that all field of a FederatedUser are filled. |
||
| 857 | * |
||
| 858 | * @param FederatedUser $federatedUser |
||
| 859 | * |
||
| 860 | * @throws FederatedUserException |
||
| 861 | */ |
||
| 862 | private function confirmFederatedUser(FederatedUser $federatedUser): void { |
||
| 871 | |||
| 872 | /** |
||
| 873 | * Confirm that the singleId of a FederatedUser is unique and not used to any other member of the |
||
| 874 | * database. |
||
| 875 | * |
||
| 876 | * @param FederatedUser $federatedUser |
||
| 877 | * |
||
| 878 | * @throws FederatedUserException |
||
| 879 | */ |
||
| 880 | public function confirmLocalSingleId(IFederatedUser $federatedUser): void { |
||
| 893 | |||
| 894 | |||
| 895 | /** |
||
| 896 | * @param IFederatedUser $federatedUser |
||
| 897 | * |
||
| 898 | * @throws FederatedUserException |
||
| 899 | * @throws RequestBuilderException |
||
| 900 | */ |
||
| 901 | public function confirmSingleIdUniqueness(IFederatedUser $federatedUser): void { |
||
| 908 | |||
| 909 | |||
| 910 | /** |
||
| 911 | * @param string $groupId |
||
| 912 | * |
||
| 913 | * @return Circle |
||
| 914 | * @throws GroupNotFoundException |
||
| 915 | * @throws FederatedEventException |
||
| 916 | * @throws FederatedItemException |
||
| 917 | * @throws FederatedUserException |
||
| 918 | * @throws InitiatorNotConfirmedException |
||
| 919 | * @throws InvalidIdException |
||
| 920 | * @throws OwnerNotFoundException |
||
| 921 | * @throws RemoteInstanceException |
||
| 922 | * @throws RemoteNotFoundException |
||
| 923 | * @throws RemoteResourceNotFoundException |
||
| 924 | * @throws SingleCircleNotFoundException |
||
| 925 | * @throws UnknownRemoteException |
||
| 926 | * @throws RequestBuilderException |
||
| 927 | */ |
||
| 928 | public function getGroupCircle(string $groupId): Circle { |
||
| 965 | |||
| 966 | } |
||
| 967 | |||
| 968 |
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: