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 |
||
| 71 | class FederatedUserService { |
||
| 72 | |||
| 73 | |||
| 74 | use TArrayTools; |
||
| 75 | use TStringTools; |
||
| 76 | use TNC21Logger; |
||
| 77 | |||
| 78 | |||
| 79 | /** @var IUserManager */ |
||
| 80 | private $userManager; |
||
| 81 | |||
| 82 | /** @var MembershipRequest */ |
||
| 83 | private $membershipRequest; |
||
| 84 | |||
| 85 | /** @var CircleRequest */ |
||
| 86 | private $circleRequest; |
||
| 87 | |||
| 88 | /** @var MemberRequest */ |
||
| 89 | private $memberRequest; |
||
| 90 | |||
| 91 | /** @var RemoteService */ |
||
| 92 | private $remoteService; |
||
| 93 | |||
| 94 | /** @var ConfigService */ |
||
| 95 | private $configService; |
||
| 96 | |||
| 97 | |||
| 98 | /** @var FederatedUser */ |
||
| 99 | private $currentUser = null; |
||
| 100 | |||
| 101 | /** @var RemoteInstance */ |
||
| 102 | private $remoteInstance = null; |
||
| 103 | |||
| 104 | /** @var bool */ |
||
| 105 | private $bypass = false; |
||
| 106 | |||
| 107 | |||
| 108 | /** |
||
| 109 | * FederatedUserService constructor. |
||
| 110 | * |
||
| 111 | * @param IUserManager $userManager |
||
| 112 | * @param MembershipRequest $membershipRequest |
||
| 113 | * @param CircleRequest $circleRequest |
||
| 114 | * @param MemberRequest $memberRequest |
||
| 115 | * @param RemoteService $remoteService |
||
| 116 | * @param ConfigService $configService |
||
| 117 | */ |
||
| 118 | public function __construct( |
||
| 119 | IUserManager $userManager, MembershipRequest $membershipRequest, CircleRequest $circleRequest, |
||
| 120 | MemberRequest $memberRequest, RemoteService $remoteService, ConfigService $configService |
||
| 121 | ) { |
||
| 122 | $this->userManager = $userManager; |
||
| 123 | $this->membershipRequest = $membershipRequest; |
||
| 124 | $this->circleRequest = $circleRequest; |
||
| 125 | $this->memberRequest = $memberRequest; |
||
| 126 | $this->remoteService = $remoteService; |
||
| 127 | $this->configService = $configService; |
||
| 128 | } |
||
| 129 | |||
| 130 | |||
| 131 | // /** |
||
| 132 | // * @param string $userId |
||
| 133 | // * |
||
| 134 | // * @throws CircleNotFoundException |
||
| 135 | // * @throws NoUserException |
||
| 136 | // */ |
||
| 137 | // public function setLocalInitiator(string $userId): void { |
||
| 138 | // $this->currentUser = $this->createLocalFederatedUser($userId); |
||
| 139 | // } |
||
| 140 | |||
| 141 | /** |
||
| 142 | * set a CurrentUser, based on a IFederatedUser. |
||
| 143 | * CurrentUser is mainly used to manage rights when requesting the database. |
||
| 144 | * |
||
| 145 | * @param IFederatedUser $federatedUser |
||
| 146 | * |
||
| 147 | * @throws FederatedUserException |
||
| 148 | */ |
||
| 149 | public function setCurrentUser(IFederatedUser $federatedUser): void { |
||
| 150 | if (!($federatedUser instanceof FederatedUser)) { |
||
| 151 | $tmp = new FederatedUser(); |
||
| 152 | $tmp->importFromIFederatedUser($federatedUser); |
||
| 153 | $federatedUser = $tmp; |
||
| 154 | } |
||
| 155 | |||
| 156 | $this->confirmFederatedUser($federatedUser); |
||
| 157 | |||
| 158 | $this->currentUser = $federatedUser; |
||
| 159 | } |
||
| 160 | |||
| 161 | /** |
||
| 162 | * @return FederatedUser|null |
||
| 163 | */ |
||
| 164 | public function getCurrentUser(): ?FederatedUser { |
||
| 165 | return $this->currentUser; |
||
| 166 | } |
||
| 167 | |||
| 168 | /** |
||
| 169 | * @return bool |
||
| 170 | */ |
||
| 171 | public function hasCurrentUser(): bool { |
||
| 172 | return !is_null($this->currentUser); |
||
| 173 | } |
||
| 174 | |||
| 175 | /** |
||
| 176 | * @throws InitiatorNotFoundException |
||
| 177 | */ |
||
| 178 | public function mustHaveCurrentUser(): void { |
||
| 179 | if ($this->bypass) { |
||
| 180 | return; |
||
| 181 | } |
||
| 182 | if (!$this->hasCurrentUser() && !$this->hasRemoteInstance()) { |
||
| 183 | throw new InitiatorNotFoundException(); |
||
| 184 | } |
||
| 185 | } |
||
| 186 | |||
| 187 | /** |
||
| 188 | * @param bool $bypass |
||
| 189 | */ |
||
| 190 | public function bypassCurrentUserCondition(bool $bypass): void { |
||
| 191 | $this->bypass = $bypass; |
||
| 192 | } |
||
| 193 | |||
| 194 | |||
| 195 | /** |
||
| 196 | * set a RemoteInstance, mostly from a remote request (RemoteController) |
||
| 197 | * Used to limit rights in some request in the local database. |
||
| 198 | * |
||
| 199 | * @param RemoteInstance $remoteInstance |
||
| 200 | */ |
||
| 201 | public function setRemoteInstance(RemoteInstance $remoteInstance): void { |
||
| 202 | $this->remoteInstance = $remoteInstance; |
||
| 203 | } |
||
| 204 | |||
| 205 | /** |
||
| 206 | * @return RemoteInstance|null |
||
| 207 | */ |
||
| 208 | public function getRemoteInstance(): ?RemoteInstance { |
||
| 209 | return $this->remoteInstance; |
||
| 210 | } |
||
| 211 | |||
| 212 | /** |
||
| 213 | * @return bool |
||
| 214 | */ |
||
| 215 | public function hasRemoteInstance(): bool { |
||
| 216 | return !is_null($this->remoteInstance); |
||
| 217 | } |
||
| 218 | |||
| 219 | |||
| 220 | /** |
||
| 221 | * Get the full FederatedUser for a local user. |
||
| 222 | * Will generate the SingleId if none exist |
||
| 223 | * |
||
| 224 | * @param string $userId |
||
| 225 | * |
||
| 226 | * @return FederatedUser |
||
| 227 | * @throws CircleNotFoundException |
||
| 228 | * @throws FederatedUserNotFoundException |
||
| 229 | * @throws InvalidIdException |
||
| 230 | */ |
||
| 231 | public function getLocalFederatedUser(string $userId): FederatedUser { |
||
| 232 | $user = $this->userManager->get($userId); |
||
| 233 | if ($user === null) { |
||
| 234 | throw new FederatedUserNotFoundException('user ' . $userId . ' not found'); |
||
| 235 | } |
||
| 236 | |||
| 237 | $federatedUser = new FederatedUser(); |
||
| 238 | $federatedUser->set($user->getUID()); |
||
| 239 | $this->fillSingleCircleId($federatedUser); |
||
| 240 | |||
| 241 | return $federatedUser; |
||
| 242 | } |
||
| 243 | |||
| 244 | |||
| 245 | /** |
||
| 246 | * some ./occ commands allows to add an Initiator, or force the PoV from the local circles' owner |
||
| 247 | * |
||
| 248 | * TODO: manage non-user type ? |
||
| 249 | * |
||
| 250 | * @param string $userId |
||
| 251 | * @param string $circleId |
||
| 252 | * @param bool $bypass |
||
| 253 | * |
||
| 254 | * @throws CircleNotFoundException |
||
| 255 | * @throws FederatedUserException |
||
| 256 | * @throws FederatedUserNotFoundException |
||
| 257 | * @throws InvalidIdException |
||
| 258 | * @throws InvalidItemException |
||
| 259 | * @throws OwnerNotFoundException |
||
| 260 | * @throws RemoteInstanceException |
||
| 261 | * @throws RemoteNotFoundException |
||
| 262 | * @throws RemoteResourceNotFoundException |
||
| 263 | * @throws RequestNetworkException |
||
| 264 | * @throws SignatoryException |
||
| 265 | * @throws UnknownRemoteException |
||
| 266 | * @throws UserTypeNotFoundException |
||
| 267 | * @throws MemberNotFoundException |
||
| 268 | */ |
||
| 269 | public function commandLineInitiator(string $userId, string $circleId = '', bool $bypass = false): void { |
||
| 294 | |||
| 295 | |||
| 296 | /** |
||
| 297 | * Works like getFederatedUser, but returns a member. |
||
| 298 | * Allow to specify a level: handle@instance,level |
||
| 299 | * |
||
| 300 | * Used for filters when searching for Circles |
||
| 301 | * TODO: Used outside of ./occ circles:manage:list ? |
||
| 302 | * |
||
| 303 | * @param string $userId |
||
| 304 | * @param int $level |
||
| 305 | * |
||
| 306 | * @return Member |
||
| 307 | * @throws CircleNotFoundException |
||
| 308 | * @throws FederatedUserException |
||
| 309 | * @throws FederatedUserNotFoundException |
||
| 310 | * @throws InvalidItemException |
||
| 311 | * @throws MemberNotFoundException |
||
| 312 | * @throws OwnerNotFoundException |
||
| 313 | * @throws RemoteInstanceException |
||
| 314 | * @throws RemoteNotFoundException |
||
| 315 | * @throws RemoteResourceNotFoundException |
||
| 316 | * @throws RequestNetworkException |
||
| 317 | * @throws SignatoryException |
||
| 318 | * @throws UnknownRemoteException |
||
| 319 | * @throws UserTypeNotFoundException |
||
| 320 | */ |
||
| 321 | public function getFederatedMember(string $userId, int $level = Member::LEVEL_MEMBER): Member { |
||
| 334 | |||
| 335 | |||
| 336 | /** |
||
| 337 | * Generate a FederatedUser based on local data. |
||
| 338 | * WARNING: There is no confirmation that the returned FederatedUser exists or is valid at this point. |
||
| 339 | * Use getFederatedUser() instead if a valid and confirmed FederatedUser is needed. |
||
| 340 | * |
||
| 341 | * if $federatedId is a known SingleId, will returns data from the local database. |
||
| 342 | * if $federatedId is a local username, will returns data from the local database. |
||
| 343 | * Otherwise, the FederatedUser will not contains a SingleId. |
||
| 344 | * |
||
| 345 | * @param string $userId |
||
| 346 | * @param int $type |
||
| 347 | * |
||
| 348 | * @return FederatedUser |
||
| 349 | */ |
||
| 350 | public function generateFederatedUser(string $userId, int $type = 0): FederatedUser { |
||
| 397 | |||
| 398 | |||
| 399 | /** |
||
| 400 | * get a valid FederatedUser, based on the federatedId (userId@instance) its the type. |
||
| 401 | * If instance is local, get the local valid FederatedUser |
||
| 402 | * If instance is not local, get the remote valid FederatedUser |
||
| 403 | * |
||
| 404 | * @param string $federatedId |
||
| 405 | * @param int $userType |
||
| 406 | * |
||
| 407 | * @return FederatedUser |
||
| 408 | * @throws CircleNotFoundException |
||
| 409 | * @throws FederatedUserException |
||
| 410 | * @throws FederatedUserNotFoundException |
||
| 411 | * @throws InvalidItemException |
||
| 412 | * @throws MemberNotFoundException |
||
| 413 | * @throws OwnerNotFoundException |
||
| 414 | * @throws RemoteInstanceException |
||
| 415 | * @throws RemoteNotFoundException |
||
| 416 | * @throws RemoteResourceNotFoundException |
||
| 417 | * @throws RequestNetworkException |
||
| 418 | * @throws SignatoryException |
||
| 419 | * @throws UnknownRemoteException |
||
| 420 | * @throws UserTypeNotFoundException |
||
| 421 | */ |
||
| 422 | public function getFederatedUser(string $federatedId, int $userType = Member::TYPE_USER): FederatedUser { |
||
| 441 | |||
| 442 | |||
| 443 | /** |
||
| 444 | * @param string $singleId |
||
| 445 | * @param string $instance |
||
| 446 | * |
||
| 447 | * @return FederatedUser |
||
| 448 | * @throws FederatedUserException |
||
| 449 | * @throws FederatedUserNotFoundException |
||
| 450 | * @throws InvalidItemException |
||
| 451 | * @throws MemberNotFoundException |
||
| 452 | * @throws RemoteInstanceException |
||
| 453 | * @throws RemoteNotFoundException |
||
| 454 | * @throws RemoteResourceNotFoundException |
||
| 455 | * @throws RequestNetworkException |
||
| 456 | * @throws SignatoryException |
||
| 457 | * @throws UnknownRemoteException |
||
| 458 | */ |
||
| 459 | private function getFederatedUser_SingleId(string $singleId, string $instance): FederatedUser { |
||
| 474 | |||
| 475 | |||
| 476 | /** |
||
| 477 | * @param string $userId |
||
| 478 | * @param string $instance |
||
| 479 | * |
||
| 480 | * @return FederatedUser |
||
| 481 | * @throws CircleNotFoundException |
||
| 482 | * @throws FederatedUserException |
||
| 483 | * @throws FederatedUserNotFoundException |
||
| 484 | * @throws InvalidIdException |
||
| 485 | * @throws InvalidItemException |
||
| 486 | * @throws RemoteInstanceException |
||
| 487 | * @throws RemoteNotFoundException |
||
| 488 | * @throws RemoteResourceNotFoundException |
||
| 489 | * @throws RequestNetworkException |
||
| 490 | * @throws SignatoryException |
||
| 491 | * @throws UnknownRemoteException |
||
| 492 | */ |
||
| 493 | private function getFederatedUser_User(string $userId, string $instance): FederatedUser { |
||
| 504 | |||
| 505 | |||
| 506 | /** |
||
| 507 | * // TODO: do we need to have this working on remote instance ? |
||
| 508 | * |
||
| 509 | * @param string $circleId |
||
| 510 | * @param string $instance |
||
| 511 | * |
||
| 512 | * @return FederatedUser |
||
| 513 | * @throws CircleNotFoundException |
||
| 514 | * @throws OwnerNotFoundException |
||
| 515 | */ |
||
| 516 | private function getFederatedUser_Circle(string $circleId, string $instance): FederatedUser { |
||
| 528 | |||
| 529 | |||
| 530 | /** |
||
| 531 | * extract userID and instance from a federatedId |
||
| 532 | * |
||
| 533 | * @param string $federatedId |
||
| 534 | * |
||
| 535 | * @return array |
||
| 536 | */ |
||
| 537 | private function extractIdAndInstance(string $federatedId): array { |
||
| 548 | |||
| 549 | |||
| 550 | /** |
||
| 551 | * @param FederatedUser $federatedUser |
||
| 552 | * |
||
| 553 | * @throws CircleNotFoundException |
||
| 554 | * @throws InvalidIdException |
||
| 555 | */ |
||
| 556 | private function fillSingleCircleId(FederatedUser $federatedUser): void { |
||
| 564 | |||
| 565 | |||
| 566 | /** |
||
| 567 | * get the Single Circle from a local user |
||
| 568 | * |
||
| 569 | * @param FederatedUser $federatedUser |
||
| 570 | * |
||
| 571 | * @return Circle |
||
| 572 | * @throws CircleNotFoundException |
||
| 573 | * @throws InvalidIdException |
||
| 574 | * @throws FederatedUserException |
||
| 575 | */ |
||
| 576 | private function getSingleCircle(FederatedUser $federatedUser): Circle { |
||
| 605 | |||
| 606 | |||
| 607 | /** |
||
| 608 | * Confirm that all field of a FederatedUser are filled. |
||
| 609 | * |
||
| 610 | * @param FederatedUser $federatedUser |
||
| 611 | * |
||
| 612 | * @throws FederatedUserException |
||
| 613 | */ |
||
| 614 | private function confirmFederatedUser(FederatedUser $federatedUser): void { |
||
| 623 | |||
| 624 | /** |
||
| 625 | * Confirm that the singleId of a FederatedUser is unique and not used to any other member of the |
||
| 626 | * database. |
||
| 627 | * |
||
| 628 | * @param FederatedUser $federatedUser |
||
| 629 | * |
||
| 630 | * @throws FederatedUserException |
||
| 631 | */ |
||
| 632 | public function confirmLocalSingleId(FederatedUser $federatedUser): void { |
||
| 645 | |||
| 646 | |||
| 647 | // /** |
||
| 648 | // * @param FederatedUser $federatedUser |
||
| 649 | // * |
||
| 650 | // * @return Membership[] |
||
| 651 | // */ |
||
| 652 | // public function generateMemberships(FederatedUser $federatedUser): array { |
||
| 653 | // $circles = $this->circleRequest->getCircles(null, $federatedUser); |
||
| 654 | // $memberships = []; |
||
| 655 | // foreach ($circles as $circle) { |
||
| 656 | // $initiator = $circle->getInitiator(); |
||
| 657 | // if (!$initiator->isMember()) { |
||
| 658 | // continue; |
||
| 659 | // } |
||
| 660 | // |
||
| 661 | // $memberships[] = new Membership( |
||
| 662 | // $initiator->getId(), $circle->getId(), $federatedUser->getSingleId(), $initiator->getLevel() |
||
| 663 | // ); |
||
| 664 | // |
||
| 665 | //// $newUser = new CurrentUser($circle->getId(), Member::TYPE_CIRCLE, ''); |
||
| 666 | //// $circles = $this->circleRequest->getCircles(null, $currentUser); |
||
| 667 | // } |
||
| 668 | // |
||
| 669 | // return $memberships; |
||
| 670 | // } |
||
| 671 | // |
||
| 672 | // |
||
| 673 | // /** |
||
| 674 | // * @param FederatedUser|null $federatedUser |
||
| 675 | // */ |
||
| 676 | // public function updateMemberships(?FederatedUser $federatedUser = null) { |
||
| 677 | // if (is_null($federatedUser)) { |
||
| 678 | // $federatedUser = $this->getCurrentUser(); |
||
| 679 | // } else { |
||
| 680 | // $federatedUser->setMemberships($this->membershipRequest->getMemberships($federatedUser)); |
||
| 681 | // } |
||
| 682 | // |
||
| 683 | // if (is_null($federatedUser)) { |
||
| 684 | // return; |
||
| 685 | // } |
||
| 686 | // |
||
| 687 | // $last = $this->generateMemberships($federatedUser); |
||
| 688 | // |
||
| 689 | // echo 'known: ' . json_encode($federatedUser->getMemberships()) . "\n"; |
||
| 690 | // echo 'last: ' . json_encode($last) . "\n"; |
||
| 691 | // |
||
| 692 | //// |
||
| 693 | //// $circles = $this->circleRequest->getCircles(null, $viewer); |
||
| 694 | //// foreach ($circles as $circle) { |
||
| 695 | //// $viewer = $circle->getViewer(); |
||
| 696 | //// if (!$viewer->isMember()) { |
||
| 697 | //// continue; |
||
| 698 | //// } |
||
| 699 | //// |
||
| 700 | //// echo 'new member: ' . json_encode($viewer) . "\n"; |
||
| 701 | ////// $this->federatedUserService->updateMembership($circle); |
||
| 702 | //// } |
||
| 703 | // |
||
| 704 | // |
||
| 705 | // } |
||
| 706 | |||
| 707 | |||
| 708 | } |
||
| 709 | |||
| 710 |