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 |
||
| 72 | class FederatedUserService { |
||
| 73 | |||
| 74 | |||
| 75 | use TArrayTools; |
||
| 76 | use TStringTools; |
||
| 77 | use TNC21Logger; |
||
| 78 | |||
| 79 | |||
| 80 | /** @var IUserManager */ |
||
| 81 | private $userManager; |
||
| 82 | |||
| 83 | /** @var MembershipRequest */ |
||
| 84 | private $membershipRequest; |
||
| 85 | |||
| 86 | /** @var CircleRequest */ |
||
| 87 | private $circleRequest; |
||
| 88 | |||
| 89 | /** @var MemberRequest */ |
||
| 90 | private $memberRequest; |
||
| 91 | |||
| 92 | /** @var RemoteService */ |
||
| 93 | private $remoteService; |
||
| 94 | |||
| 95 | /** @var ConfigService */ |
||
| 96 | private $configService; |
||
| 97 | |||
| 98 | |||
| 99 | /** @var FederatedUser */ |
||
| 100 | private $currentUser = null; |
||
| 101 | |||
| 102 | /** @var RemoteInstance */ |
||
| 103 | private $remoteInstance = null; |
||
| 104 | |||
| 105 | /** @var bool */ |
||
| 106 | private $bypass = false; |
||
| 107 | |||
| 108 | |||
| 109 | /** |
||
| 110 | * FederatedUserService constructor. |
||
| 111 | * |
||
| 112 | * @param IUserManager $userManager |
||
| 113 | * @param MembershipRequest $membershipRequest |
||
| 114 | * @param CircleRequest $circleRequest |
||
| 115 | * @param MemberRequest $memberRequest |
||
| 116 | * @param RemoteService $remoteService |
||
| 117 | * @param ConfigService $configService |
||
| 118 | */ |
||
| 119 | public function __construct( |
||
| 130 | |||
| 131 | |||
| 132 | /** |
||
| 133 | * @param IUser|null $user |
||
| 134 | * |
||
| 135 | * @throws CircleNotFoundException |
||
| 136 | * @throws FederatedUserNotFoundException |
||
| 137 | * @throws InvalidIdException |
||
| 138 | */ |
||
| 139 | public function setLocalCurrentUser(?IUser $user) { |
||
| 146 | |||
| 147 | |||
| 148 | /** |
||
| 149 | * set a CurrentUser, based on a IFederatedUser. |
||
| 150 | * CurrentUser is mainly used to manage rights when requesting the database. |
||
| 151 | * |
||
| 152 | * @param IFederatedUser $federatedUser |
||
| 153 | * |
||
| 154 | * @throws FederatedUserException |
||
| 155 | */ |
||
| 156 | public function setCurrentUser(IFederatedUser $federatedUser): void { |
||
| 167 | |||
| 168 | /** |
||
| 169 | * @return FederatedUser|null |
||
| 170 | */ |
||
| 171 | public function getCurrentUser(): ?FederatedUser { |
||
| 174 | |||
| 175 | /** |
||
| 176 | * @return bool |
||
| 177 | */ |
||
| 178 | public function hasCurrentUser(): bool { |
||
| 181 | |||
| 182 | /** |
||
| 183 | * @throws InitiatorNotFoundException |
||
| 184 | */ |
||
| 185 | public function mustHaveCurrentUser(): void { |
||
| 193 | |||
| 194 | /** |
||
| 195 | * @param bool $bypass |
||
| 196 | */ |
||
| 197 | public function bypassCurrentUserCondition(bool $bypass): void { |
||
| 200 | |||
| 201 | |||
| 202 | /** |
||
| 203 | * set a RemoteInstance, mostly from a remote request (RemoteController) |
||
| 204 | * Used to limit rights in some request in the local database. |
||
| 205 | * |
||
| 206 | * @param RemoteInstance $remoteInstance |
||
| 207 | */ |
||
| 208 | public function setRemoteInstance(RemoteInstance $remoteInstance): void { |
||
| 211 | |||
| 212 | /** |
||
| 213 | * @return RemoteInstance|null |
||
| 214 | */ |
||
| 215 | public function getRemoteInstance(): ?RemoteInstance { |
||
| 218 | |||
| 219 | /** |
||
| 220 | * @return bool |
||
| 221 | */ |
||
| 222 | public function hasRemoteInstance(): bool { |
||
| 225 | |||
| 226 | |||
| 227 | /** |
||
| 228 | * Get the full FederatedUser for a local user. |
||
| 229 | * Will generate the SingleId if none exist |
||
| 230 | * |
||
| 231 | * @param string $userId |
||
| 232 | * |
||
| 233 | * @return FederatedUser |
||
| 234 | * @throws CircleNotFoundException |
||
| 235 | * @throws FederatedUserNotFoundException |
||
| 236 | * @throws InvalidIdException |
||
| 237 | */ |
||
| 238 | public function getLocalFederatedUser(string $userId): FederatedUser { |
||
| 250 | |||
| 251 | |||
| 252 | /** |
||
| 253 | * some ./occ commands allows to add an Initiator, or force the PoV from the local circles' owner |
||
| 254 | * |
||
| 255 | * TODO: manage non-user type ? |
||
| 256 | * |
||
| 257 | * @param string $userId |
||
| 258 | * @param string $circleId |
||
| 259 | * @param bool $bypass |
||
| 260 | * |
||
| 261 | * @throws CircleNotFoundException |
||
| 262 | * @throws FederatedUserException |
||
| 263 | * @throws FederatedUserNotFoundException |
||
| 264 | * @throws InvalidIdException |
||
| 265 | * @throws InvalidItemException |
||
| 266 | * @throws OwnerNotFoundException |
||
| 267 | * @throws RemoteInstanceException |
||
| 268 | * @throws RemoteNotFoundException |
||
| 269 | * @throws RemoteResourceNotFoundException |
||
| 270 | * @throws RequestNetworkException |
||
| 271 | * @throws SignatoryException |
||
| 272 | * @throws UnknownRemoteException |
||
| 273 | * @throws UserTypeNotFoundException |
||
| 274 | * @throws MemberNotFoundException |
||
| 275 | */ |
||
| 276 | public function commandLineInitiator(string $userId, string $circleId = '', bool $bypass = false): void { |
||
| 301 | |||
| 302 | |||
| 303 | /** |
||
| 304 | * Works like getFederatedUser, but returns a member. |
||
| 305 | * Allow to specify a level: handle@instance,level |
||
| 306 | * |
||
| 307 | * Used for filters when searching for Circles |
||
| 308 | * TODO: Used outside of ./occ circles:manage:list ? |
||
| 309 | * |
||
| 310 | * @param string $userId |
||
| 311 | * @param int $level |
||
| 312 | * |
||
| 313 | * @return Member |
||
| 314 | * @throws CircleNotFoundException |
||
| 315 | * @throws FederatedUserException |
||
| 316 | * @throws FederatedUserNotFoundException |
||
| 317 | * @throws InvalidItemException |
||
| 318 | * @throws MemberNotFoundException |
||
| 319 | * @throws OwnerNotFoundException |
||
| 320 | * @throws RemoteInstanceException |
||
| 321 | * @throws RemoteNotFoundException |
||
| 322 | * @throws RemoteResourceNotFoundException |
||
| 323 | * @throws RequestNetworkException |
||
| 324 | * @throws SignatoryException |
||
| 325 | * @throws UnknownRemoteException |
||
| 326 | * @throws UserTypeNotFoundException |
||
| 327 | */ |
||
| 328 | public function getFederatedMember(string $userId, int $level = Member::LEVEL_MEMBER): Member { |
||
| 341 | |||
| 342 | |||
| 343 | /** |
||
| 344 | * Generate a FederatedUser based on local data. |
||
| 345 | * WARNING: There is no confirmation that the returned FederatedUser exists or is valid at this point. |
||
| 346 | * Use getFederatedUser() instead if a valid and confirmed FederatedUser is needed. |
||
| 347 | * |
||
| 348 | * if $federatedId is a known SingleId, will returns data from the local database. |
||
| 349 | * if $federatedId is a local username, will returns data from the local database. |
||
| 350 | * Otherwise, the FederatedUser will not contains a SingleId. |
||
| 351 | * |
||
| 352 | * @param string $userId |
||
| 353 | * @param int $type |
||
| 354 | * |
||
| 355 | * @return FederatedUser |
||
| 356 | */ |
||
| 357 | public function generateFederatedUser(string $userId, int $type = 0): FederatedUser { |
||
| 404 | |||
| 405 | |||
| 406 | /** |
||
| 407 | * get a valid FederatedUser, based on the federatedId (userId@instance) its the type. |
||
| 408 | * If instance is local, get the local valid FederatedUser |
||
| 409 | * If instance is not local, get the remote valid FederatedUser |
||
| 410 | * |
||
| 411 | * @param string $federatedId |
||
| 412 | * @param int $userType |
||
| 413 | * |
||
| 414 | * @return FederatedUser |
||
| 415 | * @throws CircleNotFoundException |
||
| 416 | * @throws FederatedUserException |
||
| 417 | * @throws FederatedUserNotFoundException |
||
| 418 | * @throws InvalidItemException |
||
| 419 | * @throws MemberNotFoundException |
||
| 420 | * @throws OwnerNotFoundException |
||
| 421 | * @throws RemoteInstanceException |
||
| 422 | * @throws RemoteNotFoundException |
||
| 423 | * @throws RemoteResourceNotFoundException |
||
| 424 | * @throws RequestNetworkException |
||
| 425 | * @throws SignatoryException |
||
| 426 | * @throws UnknownRemoteException |
||
| 427 | * @throws UserTypeNotFoundException |
||
| 428 | */ |
||
| 429 | public function getFederatedUser(string $federatedId, int $userType = Member::TYPE_USER): FederatedUser { |
||
| 450 | |||
| 451 | |||
| 452 | /** |
||
| 453 | * @param string $singleId |
||
| 454 | * @param string $instance |
||
| 455 | * |
||
| 456 | * @return FederatedUser |
||
| 457 | * @throws FederatedUserException |
||
| 458 | * @throws FederatedUserNotFoundException |
||
| 459 | * @throws InvalidItemException |
||
| 460 | * @throws MemberNotFoundException |
||
| 461 | * @throws RemoteInstanceException |
||
| 462 | * @throws RemoteNotFoundException |
||
| 463 | * @throws RemoteResourceNotFoundException |
||
| 464 | * @throws RequestNetworkException |
||
| 465 | * @throws SignatoryException |
||
| 466 | * @throws UnknownRemoteException |
||
| 467 | */ |
||
| 468 | private function getFederatedUser_SingleId(string $singleId, string $instance): FederatedUser { |
||
| 483 | |||
| 484 | |||
| 485 | /** |
||
| 486 | * @param string $userId |
||
| 487 | * @param string $instance |
||
| 488 | * |
||
| 489 | * @return FederatedUser |
||
| 490 | * @throws CircleNotFoundException |
||
| 491 | * @throws FederatedUserException |
||
| 492 | * @throws FederatedUserNotFoundException |
||
| 493 | * @throws InvalidIdException |
||
| 494 | * @throws InvalidItemException |
||
| 495 | * @throws RemoteInstanceException |
||
| 496 | * @throws RemoteNotFoundException |
||
| 497 | * @throws RemoteResourceNotFoundException |
||
| 498 | * @throws RequestNetworkException |
||
| 499 | * @throws SignatoryException |
||
| 500 | * @throws UnknownRemoteException |
||
| 501 | */ |
||
| 502 | private function getFederatedUser_User(string $userId, string $instance): FederatedUser { |
||
| 513 | |||
| 514 | |||
| 515 | /** |
||
| 516 | * // TODO: do we need to have this working on remote instance ? |
||
| 517 | * |
||
| 518 | * @param string $circleId |
||
| 519 | * @param string $instance |
||
| 520 | * |
||
| 521 | * @return FederatedUser |
||
| 522 | * @throws CircleNotFoundException |
||
| 523 | * @throws FederatedUserException |
||
| 524 | * @throws FederatedUserNotFoundException |
||
| 525 | * @throws InvalidItemException |
||
| 526 | * @throws OwnerNotFoundException |
||
| 527 | * @throws RemoteInstanceException |
||
| 528 | * @throws RemoteNotFoundException |
||
| 529 | * @throws RemoteResourceNotFoundException |
||
| 530 | * @throws RequestNetworkException |
||
| 531 | * @throws SignatoryException |
||
| 532 | * @throws UnknownRemoteException |
||
| 533 | */ |
||
| 534 | private function getFederatedUser_Circle(string $circleId, string $instance): FederatedUser { |
||
| 549 | |||
| 550 | |||
| 551 | /** |
||
| 552 | * extract userID and instance from a federatedId |
||
| 553 | * |
||
| 554 | * @param string $federatedId |
||
| 555 | * |
||
| 556 | * @return array |
||
| 557 | */ |
||
| 558 | private function extractIdAndInstance(string $federatedId): array { |
||
| 569 | |||
| 570 | |||
| 571 | /** |
||
| 572 | * @param FederatedUser $federatedUser |
||
| 573 | * |
||
| 574 | * @throws CircleNotFoundException |
||
| 575 | * @throws InvalidIdException |
||
| 576 | */ |
||
| 577 | private function fillSingleCircleId(FederatedUser $federatedUser): void { |
||
| 585 | |||
| 586 | |||
| 587 | /** |
||
| 588 | * get the Single Circle from a local user |
||
| 589 | * |
||
| 590 | * @param FederatedUser $federatedUser |
||
| 591 | * |
||
| 592 | * @return Circle |
||
| 593 | * @throws CircleNotFoundException |
||
| 594 | * @throws InvalidIdException |
||
| 595 | * @throws FederatedUserException |
||
| 596 | */ |
||
| 597 | private function getSingleCircle(FederatedUser $federatedUser): Circle { |
||
| 626 | |||
| 627 | |||
| 628 | /** |
||
| 629 | * Confirm that all field of a FederatedUser are filled. |
||
| 630 | * |
||
| 631 | * @param FederatedUser $federatedUser |
||
| 632 | * |
||
| 633 | * @throws FederatedUserException |
||
| 634 | */ |
||
| 635 | private function confirmFederatedUser(FederatedUser $federatedUser): void { |
||
| 644 | |||
| 645 | /** |
||
| 646 | * Confirm that the singleId of a FederatedUser is unique and not used to any other member of the |
||
| 647 | * database. |
||
| 648 | * |
||
| 649 | * @param FederatedUser $federatedUser |
||
| 650 | * |
||
| 651 | * @throws FederatedUserException |
||
| 652 | */ |
||
| 653 | public function confirmLocalSingleId(FederatedUser $federatedUser): void { |
||
| 666 | |||
| 667 | |||
| 668 | // /** |
||
| 669 | // * @param FederatedUser $federatedUser |
||
| 670 | // * |
||
| 671 | // * @return Membership[] |
||
| 672 | // */ |
||
| 673 | // public function generateMemberships(FederatedUser $federatedUser): array { |
||
| 674 | // $circles = $this->circleRequest->getCircles(null, $federatedUser); |
||
| 675 | // $memberships = []; |
||
| 676 | // foreach ($circles as $circle) { |
||
| 677 | // $initiator = $circle->getInitiator(); |
||
| 678 | // if (!$initiator->isMember()) { |
||
| 679 | // continue; |
||
| 680 | // } |
||
| 681 | // |
||
| 682 | // $memberships[] = new Membership( |
||
| 683 | // $initiator->getId(), $circle->getId(), $federatedUser->getSingleId(), $initiator->getLevel() |
||
| 684 | // ); |
||
| 685 | // |
||
| 686 | //// $newUser = new CurrentUser($circle->getId(), Member::TYPE_CIRCLE, ''); |
||
| 687 | //// $circles = $this->circleRequest->getCircles(null, $currentUser); |
||
| 688 | // } |
||
| 689 | // |
||
| 690 | // return $memberships; |
||
| 691 | // } |
||
| 692 | // |
||
| 693 | // |
||
| 694 | // /** |
||
| 695 | // * @param FederatedUser|null $federatedUser |
||
| 696 | // */ |
||
| 697 | // public function updateMemberships(?FederatedUser $federatedUser = null) { |
||
| 698 | // if (is_null($federatedUser)) { |
||
| 699 | // $federatedUser = $this->getCurrentUser(); |
||
| 700 | // } else { |
||
| 701 | // $federatedUser->setMemberships($this->membershipRequest->getMemberships($federatedUser)); |
||
| 702 | // } |
||
| 703 | // |
||
| 704 | // if (is_null($federatedUser)) { |
||
| 705 | // return; |
||
| 706 | // } |
||
| 707 | // |
||
| 708 | // $last = $this->generateMemberships($federatedUser); |
||
| 709 | // |
||
| 710 | // echo 'known: ' . json_encode($federatedUser->getMemberships()) . "\n"; |
||
| 711 | // echo 'last: ' . json_encode($last) . "\n"; |
||
| 712 | // |
||
| 713 | //// |
||
| 714 | //// $circles = $this->circleRequest->getCircles(null, $viewer); |
||
| 715 | //// foreach ($circles as $circle) { |
||
| 716 | //// $viewer = $circle->getViewer(); |
||
| 717 | //// if (!$viewer->isMember()) { |
||
| 718 | //// continue; |
||
| 719 | //// } |
||
| 720 | //// |
||
| 721 | //// echo 'new member: ' . json_encode($viewer) . "\n"; |
||
| 722 | ////// $this->federatedUserService->updateMembership($circle); |
||
| 723 | //// } |
||
| 724 | // |
||
| 725 | // |
||
| 726 | // } |
||
| 727 | |||
| 728 | |||
| 729 | } |
||
| 730 | |||
| 731 |