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 ConfigService */ |
||
| 115 | private $configService; |
||
| 116 | |||
| 117 | /** @var ContactService */ |
||
| 118 | private $contactService; |
||
| 119 | |||
| 120 | |||
| 121 | /** @var FederatedUser */ |
||
| 122 | private $currentUser = null; |
||
| 123 | |||
| 124 | /** @var FederatedUser */ |
||
| 125 | private $currentApp = null; |
||
| 126 | |||
| 127 | /** @var RemoteInstance */ |
||
| 128 | private $remoteInstance = null; |
||
| 129 | |||
| 130 | /** @var bool */ |
||
| 131 | private $bypass = false; |
||
| 132 | |||
| 133 | |||
| 134 | /** |
||
| 135 | * FederatedUserService constructor. |
||
| 136 | * |
||
| 137 | * @param IUserSession $userSession |
||
| 138 | * @param IUserManager $userManager |
||
| 139 | * @param IGroupManager $groupManager |
||
| 140 | * @param FederatedEventService $federatedEventService |
||
| 141 | * @param MembershipService $membershipService |
||
| 142 | * @param CircleRequest $circleRequest |
||
| 143 | * @param MemberRequest $memberRequest |
||
| 144 | * @param RemoteService $remoteService |
||
| 145 | * @param ContactService $contactService |
||
| 146 | * @param ConfigService $configService |
||
| 147 | */ |
||
| 148 | public function __construct( |
||
| 165 | |||
| 166 | |||
| 167 | /** |
||
| 168 | * @throws FederatedUserException |
||
| 169 | * @throws FederatedUserNotFoundException |
||
| 170 | * @throws InvalidIdException |
||
| 171 | * @throws SingleCircleNotFoundException |
||
| 172 | */ |
||
| 173 | public function initCurrentUser() { |
||
| 181 | |||
| 182 | |||
| 183 | /** |
||
| 184 | * @param IUser|null $user |
||
| 185 | * |
||
| 186 | * @throws FederatedUserException |
||
| 187 | * @throws FederatedUserNotFoundException |
||
| 188 | * @throws InvalidIdException |
||
| 189 | * @throws SingleCircleNotFoundException |
||
| 190 | */ |
||
| 191 | public function setLocalCurrentUser(?IUser $user): void { |
||
| 198 | |||
| 199 | |||
| 200 | /** |
||
| 201 | * @param string $appId |
||
| 202 | * @param int $appNumber |
||
| 203 | * |
||
| 204 | * @throws FederatedUserException |
||
| 205 | * @throws InvalidIdException |
||
| 206 | * @throws SingleCircleNotFoundException |
||
| 207 | */ |
||
| 208 | public function setLocalCurrentApp(string $appId, int $appNumber): void { |
||
| 211 | |||
| 212 | |||
| 213 | /** |
||
| 214 | * set a CurrentUser, based on a IFederatedUser. |
||
| 215 | * CurrentUser is mainly used to manage rights when requesting the database. |
||
| 216 | * |
||
| 217 | * @param IFederatedUser $federatedUser |
||
| 218 | * |
||
| 219 | * @throws FederatedUserException |
||
| 220 | */ |
||
| 221 | public function setCurrentUser(IFederatedUser $federatedUser): void { |
||
| 232 | |||
| 233 | /** |
||
| 234 | * @return FederatedUser|null |
||
| 235 | */ |
||
| 236 | public function getCurrentUser(): ?FederatedUser { |
||
| 239 | |||
| 240 | /** |
||
| 241 | * @return bool |
||
| 242 | */ |
||
| 243 | public function hasCurrentUser(): bool { |
||
| 246 | |||
| 247 | /** |
||
| 248 | * @throws InitiatorNotFoundException |
||
| 249 | */ |
||
| 250 | public function mustHaveCurrentUser(): void { |
||
| 258 | |||
| 259 | /** |
||
| 260 | * @param bool $bypass |
||
| 261 | */ |
||
| 262 | public function bypassCurrentUserCondition(bool $bypass): void { |
||
| 265 | |||
| 266 | |||
| 267 | /** |
||
| 268 | * @return FederatedUser|null |
||
| 269 | */ |
||
| 270 | public function getCurrentApp(): ?FederatedUser { |
||
| 273 | |||
| 274 | /** |
||
| 275 | * @return bool |
||
| 276 | */ |
||
| 277 | public function hasCurrentApp(): bool { |
||
| 280 | |||
| 281 | |||
| 282 | /** |
||
| 283 | * set a RemoteInstance, mostly from a remote request (RemoteController) |
||
| 284 | * Used to limit rights in some request in the local database. |
||
| 285 | * |
||
| 286 | * @param RemoteInstance $remoteInstance |
||
| 287 | */ |
||
| 288 | public function setRemoteInstance(RemoteInstance $remoteInstance): void { |
||
| 291 | |||
| 292 | /** |
||
| 293 | * @return RemoteInstance|null |
||
| 294 | */ |
||
| 295 | public function getRemoteInstance(): ?RemoteInstance { |
||
| 298 | |||
| 299 | /** |
||
| 300 | * @return bool |
||
| 301 | */ |
||
| 302 | public function hasRemoteInstance(): bool { |
||
| 305 | |||
| 306 | |||
| 307 | /** |
||
| 308 | * Get the full FederatedUser for a local user. |
||
| 309 | * Will generate the SingleId if none exist |
||
| 310 | * |
||
| 311 | * @param string $userId |
||
| 312 | * |
||
| 313 | * @return FederatedUser |
||
| 314 | * @throws FederatedUserNotFoundException |
||
| 315 | * @throws InvalidIdException |
||
| 316 | * @throws SingleCircleNotFoundException |
||
| 317 | * @throws FederatedUserException |
||
| 318 | * @throws RequestBuilderException |
||
| 319 | */ |
||
| 320 | public function getLocalFederatedUser(string $userId): FederatedUser { |
||
| 332 | |||
| 333 | |||
| 334 | /** |
||
| 335 | * Get the full FederatedUser for a local user. |
||
| 336 | * Will generate the SingleId if none exist |
||
| 337 | * |
||
| 338 | * @param string $appId |
||
| 339 | * @param int $appNumber |
||
| 340 | * |
||
| 341 | * @return FederatedUser |
||
| 342 | * @throws FederatedUserException |
||
| 343 | * @throws InvalidIdException |
||
| 344 | * @throws SingleCircleNotFoundException |
||
| 345 | * @throws RequestBuilderException |
||
| 346 | */ |
||
| 347 | public function getAppInitiator(string $appId, int $appNumber): FederatedUser { |
||
| 358 | |||
| 359 | |||
| 360 | /** |
||
| 361 | * some ./occ commands allows to add an Initiator, or force the PoV from the local circles' owner |
||
| 362 | * |
||
| 363 | * TODO: manage non-user type ? |
||
| 364 | * |
||
| 365 | * @param string $userId |
||
| 366 | * @param string $circleId |
||
| 367 | * @param bool $bypass |
||
| 368 | * |
||
| 369 | * @throws CircleNotFoundException |
||
| 370 | * @throws FederatedItemException |
||
| 371 | * @throws FederatedUserException |
||
| 372 | * @throws FederatedUserNotFoundException |
||
| 373 | * @throws InvalidIdException |
||
| 374 | * @throws MemberNotFoundException |
||
| 375 | * @throws OwnerNotFoundException |
||
| 376 | * @throws RemoteInstanceException |
||
| 377 | * @throws RemoteNotFoundException |
||
| 378 | * @throws RemoteResourceNotFoundException |
||
| 379 | * @throws SingleCircleNotFoundException |
||
| 380 | * @throws UnknownRemoteException |
||
| 381 | * @throws UserTypeNotFoundException |
||
| 382 | */ |
||
| 383 | public function commandLineInitiator(string $userId, string $circleId = '', bool $bypass = false): void { |
||
| 407 | |||
| 408 | |||
| 409 | /** |
||
| 410 | * Works like getFederatedUser, but returns a member. |
||
| 411 | * Allow to specify a level: handle@instance,level |
||
| 412 | * |
||
| 413 | * Used for filters when searching for Circles |
||
| 414 | * TODO: Used outside of ./occ circles:manage:list ? |
||
| 415 | * |
||
| 416 | * @param string $userId |
||
| 417 | * @param int $level |
||
| 418 | * |
||
| 419 | * @return Member |
||
| 420 | * @throws CircleNotFoundException |
||
| 421 | * @throws FederatedItemException |
||
| 422 | * @throws FederatedUserException |
||
| 423 | * @throws FederatedUserNotFoundException |
||
| 424 | * @throws InvalidIdException |
||
| 425 | * @throws MemberNotFoundException |
||
| 426 | * @throws OwnerNotFoundException |
||
| 427 | * @throws RemoteInstanceException |
||
| 428 | * @throws RemoteNotFoundException |
||
| 429 | * @throws RemoteResourceNotFoundException |
||
| 430 | * @throws SingleCircleNotFoundException |
||
| 431 | * @throws UnknownRemoteException |
||
| 432 | * @throws UserTypeNotFoundException |
||
| 433 | */ |
||
| 434 | public function getFederatedMember(string $userId, int $level = Member::LEVEL_MEMBER): Member { |
||
| 447 | |||
| 448 | |||
| 449 | /** |
||
| 450 | * get a valid FederatedUser, based on the federatedId (userId@instance) its the type. |
||
| 451 | * If instance is local, get the local valid FederatedUser |
||
| 452 | * If instance is not local, get the remote valid FederatedUser |
||
| 453 | * |
||
| 454 | * @param string $federatedId |
||
| 455 | * @param int $userType |
||
| 456 | * |
||
| 457 | * @return FederatedUser |
||
| 458 | * @throws CircleNotFoundException |
||
| 459 | * @throws FederatedItemException |
||
| 460 | * @throws FederatedUserException |
||
| 461 | * @throws FederatedUserNotFoundException |
||
| 462 | * @throws InvalidIdException |
||
| 463 | * @throws MemberNotFoundException |
||
| 464 | * @throws OwnerNotFoundException |
||
| 465 | * @throws RemoteInstanceException |
||
| 466 | * @throws RemoteNotFoundException |
||
| 467 | * @throws RemoteResourceNotFoundException |
||
| 468 | * @throws SingleCircleNotFoundException |
||
| 469 | * @throws UnknownRemoteException |
||
| 470 | * @throws UserTypeNotFoundException |
||
| 471 | * @throws RequestBuilderException |
||
| 472 | */ |
||
| 473 | public function getFederatedUser(string $federatedId, int $userType = Member::TYPE_USER): FederatedUser { |
||
| 497 | |||
| 498 | |||
| 499 | /** |
||
| 500 | * Generate a FederatedUser based on local data. |
||
| 501 | * WARNING: There is no confirmation that the returned FederatedUser exists or is valid at this point. |
||
| 502 | * Use getFederatedUser() instead if a valid and confirmed FederatedUser is needed. |
||
| 503 | * |
||
| 504 | * if $federatedId is a known SingleId, will returns data from the local database. |
||
| 505 | * if $federatedId is a local username, will returns data from the local database. |
||
| 506 | * Otherwise, the FederatedUser will not contains a SingleId. |
||
| 507 | * |
||
| 508 | * @param string $federatedId |
||
| 509 | * @param int $type |
||
| 510 | * |
||
| 511 | * @return FederatedUser |
||
| 512 | */ |
||
| 513 | public function generateFederatedUser(string $federatedId, int $type = 0): FederatedUser { |
||
| 525 | |||
| 526 | |||
| 527 | /** |
||
| 528 | * @param string $singleId |
||
| 529 | * @param string $instance |
||
| 530 | * |
||
| 531 | * @return FederatedUser |
||
| 532 | * @throws CircleNotFoundException |
||
| 533 | * @throws FederatedItemException |
||
| 534 | * @throws FederatedUserException |
||
| 535 | * @throws FederatedUserNotFoundException |
||
| 536 | * @throws MemberNotFoundException |
||
| 537 | * @throws OwnerNotFoundException |
||
| 538 | * @throws RemoteInstanceException |
||
| 539 | * @throws RemoteNotFoundException |
||
| 540 | * @throws RemoteResourceNotFoundException |
||
| 541 | * @throws UnknownRemoteException |
||
| 542 | */ |
||
| 543 | public function getFederatedUser_SingleId(string $singleId, string $instance): FederatedUser { |
||
| 558 | |||
| 559 | |||
| 560 | /** |
||
| 561 | * @param string $userId |
||
| 562 | * @param string $instance |
||
| 563 | * |
||
| 564 | * @return FederatedUser |
||
| 565 | * @throws FederatedUserException |
||
| 566 | * @throws FederatedUserNotFoundException |
||
| 567 | * @throws InvalidIdException |
||
| 568 | * @throws RemoteInstanceException |
||
| 569 | * @throws RemoteNotFoundException |
||
| 570 | * @throws RemoteResourceNotFoundException |
||
| 571 | * @throws SingleCircleNotFoundException |
||
| 572 | * @throws UnknownRemoteException |
||
| 573 | * @throws FederatedItemException |
||
| 574 | */ |
||
| 575 | private function getFederatedUser_User(string $userId, string $instance): FederatedUser { |
||
| 586 | |||
| 587 | |||
| 588 | /** |
||
| 589 | * @param string $groupName |
||
| 590 | * @param string $instance |
||
| 591 | * |
||
| 592 | * @return FederatedUser |
||
| 593 | * @throws FederatedEventException |
||
| 594 | * @throws FederatedItemException |
||
| 595 | * @throws FederatedUserException |
||
| 596 | * @throws GroupNotFoundException |
||
| 597 | * @throws InitiatorNotConfirmedException |
||
| 598 | * @throws InvalidIdException |
||
| 599 | * @throws OwnerNotFoundException |
||
| 600 | * @throws RemoteInstanceException |
||
| 601 | * @throws RemoteNotFoundException |
||
| 602 | * @throws RemoteResourceNotFoundException |
||
| 603 | * @throws SingleCircleNotFoundException |
||
| 604 | * @throws UnknownRemoteException |
||
| 605 | */ |
||
| 606 | public function getFederatedUser_Group(string $groupName, string $instance): FederatedUser { |
||
| 616 | |||
| 617 | |||
| 618 | /** |
||
| 619 | * @param string $mailAddress |
||
| 620 | * |
||
| 621 | * @return FederatedUser |
||
| 622 | * @throws FederatedUserException |
||
| 623 | * @throws InvalidIdException |
||
| 624 | * @throws RequestBuilderException |
||
| 625 | * @throws SingleCircleNotFoundException |
||
| 626 | */ |
||
| 627 | public function getFederatedUser_Mail(string $mailAddress): FederatedUser { |
||
| 634 | |||
| 635 | |||
| 636 | /** |
||
| 637 | * @param string $contactPath |
||
| 638 | * |
||
| 639 | * @return FederatedUser |
||
| 640 | * @throws FederatedUserException |
||
| 641 | * @throws InvalidIdException |
||
| 642 | * @throws RequestBuilderException |
||
| 643 | * @throws SingleCircleNotFoundException |
||
| 644 | */ |
||
| 645 | public function getFederatedUser_Contact(string $contactPath): FederatedUser { |
||
| 655 | |||
| 656 | |||
| 657 | /** |
||
| 658 | * extract userID and instance from a federatedId |
||
| 659 | * |
||
| 660 | * @param string $federatedId |
||
| 661 | * |
||
| 662 | * @return array |
||
| 663 | */ |
||
| 664 | public function extractIdAndInstance(string $federatedId): array { |
||
| 675 | |||
| 676 | |||
| 677 | /** |
||
| 678 | * @param FederatedUser $federatedUser |
||
| 679 | * |
||
| 680 | * @throws FederatedUserException |
||
| 681 | * @throws InvalidIdException |
||
| 682 | * @throws SingleCircleNotFoundException |
||
| 683 | * @throws RequestBuilderException |
||
| 684 | */ |
||
| 685 | private function fillSingleCircleId(FederatedUser $federatedUser): void { |
||
| 694 | |||
| 695 | |||
| 696 | /** |
||
| 697 | * get the Single Circle from a local user |
||
| 698 | * |
||
| 699 | * @param FederatedUser $federatedUser |
||
| 700 | * |
||
| 701 | * @return Circle |
||
| 702 | * @throws InvalidIdException |
||
| 703 | * @throws FederatedUserException |
||
| 704 | * @throws SingleCircleNotFoundException |
||
| 705 | * @throws RequestBuilderException |
||
| 706 | */ |
||
| 707 | private function getSingleCircle(FederatedUser $federatedUser): Circle { |
||
| 752 | |||
| 753 | |||
| 754 | /** |
||
| 755 | * @param FederatedUser $federatedUser |
||
| 756 | * |
||
| 757 | * @return string |
||
| 758 | * @throws ContactAddressBookNotFoundException |
||
| 759 | * @throws ContactFormatException |
||
| 760 | * @throws ContactNotFoundException |
||
| 761 | */ |
||
| 762 | private function getLocalDisplayName(FederatedUser $federatedUser): string { |
||
| 769 | |||
| 770 | |||
| 771 | /** |
||
| 772 | * Confirm that all field of a FederatedUser are filled. |
||
| 773 | * |
||
| 774 | * @param FederatedUser $federatedUser |
||
| 775 | * |
||
| 776 | * @throws FederatedUserException |
||
| 777 | */ |
||
| 778 | private function confirmFederatedUser(FederatedUser $federatedUser): void { |
||
| 787 | |||
| 788 | /** |
||
| 789 | * Confirm that the singleId of a FederatedUser is unique and not used to any other member of the |
||
| 790 | * database. |
||
| 791 | * |
||
| 792 | * @param FederatedUser $federatedUser |
||
| 793 | * |
||
| 794 | * @throws FederatedUserException |
||
| 795 | */ |
||
| 796 | public function confirmLocalSingleId(IFederatedUser $federatedUser): void { |
||
| 809 | |||
| 810 | |||
| 811 | /** |
||
| 812 | * @param IFederatedUser $federatedUser |
||
| 813 | * |
||
| 814 | * @throws FederatedUserException |
||
| 815 | * @throws RequestBuilderException |
||
| 816 | */ |
||
| 817 | public function confirmSingleIdUniqueness(IFederatedUser $federatedUser): void { |
||
| 824 | |||
| 825 | |||
| 826 | /** |
||
| 827 | * @param string $groupId |
||
| 828 | * |
||
| 829 | * @return Circle |
||
| 830 | * @throws GroupNotFoundException |
||
| 831 | * @throws FederatedEventException |
||
| 832 | * @throws FederatedItemException |
||
| 833 | * @throws FederatedUserException |
||
| 834 | * @throws InitiatorNotConfirmedException |
||
| 835 | * @throws InvalidIdException |
||
| 836 | * @throws OwnerNotFoundException |
||
| 837 | * @throws RemoteInstanceException |
||
| 838 | * @throws RemoteNotFoundException |
||
| 839 | * @throws RemoteResourceNotFoundException |
||
| 840 | * @throws SingleCircleNotFoundException |
||
| 841 | * @throws UnknownRemoteException |
||
| 842 | * @throws RequestBuilderException |
||
| 843 | */ |
||
| 844 | public function getGroupCircle(string $groupId): Circle { |
||
| 881 | |||
| 882 | } |
||
| 883 | |||
| 884 |