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 |
||
| 70 | class FederatedUserService { |
||
| 71 | |||
| 72 | |||
| 73 | use TArrayTools; |
||
| 74 | use TStringTools; |
||
| 75 | use TNC21Logger; |
||
| 76 | |||
| 77 | |||
| 78 | /** @var IUserManager */ |
||
| 79 | private $userManager; |
||
| 80 | |||
| 81 | /** @var MembershipRequest */ |
||
| 82 | private $membershipRequest; |
||
| 83 | |||
| 84 | /** @var CircleRequest */ |
||
| 85 | private $circleRequest; |
||
| 86 | |||
| 87 | /** @var MemberRequest */ |
||
| 88 | private $memberRequest; |
||
| 89 | |||
| 90 | /** @var RemoteService */ |
||
| 91 | private $remoteService; |
||
| 92 | |||
| 93 | /** @var ConfigService */ |
||
| 94 | private $configService; |
||
| 95 | |||
| 96 | |||
| 97 | /** @var FederatedUser */ |
||
| 98 | private $currentUser = null; |
||
| 99 | |||
| 100 | /** @var RemoteInstance */ |
||
| 101 | private $remoteInstance = null; |
||
| 102 | |||
| 103 | /** @var bool */ |
||
| 104 | private $bypass = false; |
||
| 105 | |||
| 106 | |||
| 107 | /** |
||
| 108 | * FederatedUserService constructor. |
||
| 109 | * |
||
| 110 | * @param IUserManager $userManager |
||
| 111 | * @param MembershipRequest $membershipRequest |
||
| 112 | * @param CircleRequest $circleRequest |
||
| 113 | * @param MemberRequest $memberRequest |
||
| 114 | * @param RemoteService $remoteService |
||
| 115 | * @param ConfigService $configService |
||
| 116 | */ |
||
| 117 | public function __construct( |
||
| 128 | |||
| 129 | |||
| 130 | // /** |
||
| 131 | // * @param string $userId |
||
| 132 | // * |
||
| 133 | // * @throws CircleNotFoundException |
||
| 134 | // * @throws NoUserException |
||
| 135 | // */ |
||
| 136 | // public function setLocalInitiator(string $userId): void { |
||
| 137 | // $this->currentUser = $this->createLocalFederatedUser($userId); |
||
| 138 | // } |
||
| 139 | |||
| 140 | /** |
||
| 141 | * set a CurrentUser, based on a IFederatedUser. |
||
| 142 | * CurrentUser is mainly used to manage rights when requesting the database. |
||
| 143 | * |
||
| 144 | * @param IFederatedUser $federatedUser |
||
| 145 | * |
||
| 146 | * @throws FederatedUserException |
||
| 147 | */ |
||
| 148 | public function setCurrentUser(IFederatedUser $federatedUser): void { |
||
| 159 | |||
| 160 | /** |
||
| 161 | * @return FederatedUser|null |
||
| 162 | */ |
||
| 163 | public function getCurrentUser(): ?FederatedUser { |
||
| 166 | |||
| 167 | /** |
||
| 168 | * @return bool |
||
| 169 | */ |
||
| 170 | public function hasCurrentUser(): bool { |
||
| 173 | |||
| 174 | /** |
||
| 175 | * @throws InitiatorNotFoundException |
||
| 176 | */ |
||
| 177 | public function mustHaveCurrentUser(): void { |
||
| 185 | |||
| 186 | /** |
||
| 187 | * @param bool $bypass |
||
| 188 | */ |
||
| 189 | public function bypassCurrentUserCondition(bool $bypass): void { |
||
| 192 | |||
| 193 | |||
| 194 | /** |
||
| 195 | * set a RemoteInstance, mostly from a remote request (RemoteController) |
||
| 196 | * Used to limit rights in some request in the local database. |
||
| 197 | * |
||
| 198 | * @param RemoteInstance $remoteInstance |
||
| 199 | */ |
||
| 200 | public function setRemoteInstance(RemoteInstance $remoteInstance): void { |
||
| 203 | |||
| 204 | /** |
||
| 205 | * @return RemoteInstance|null |
||
| 206 | */ |
||
| 207 | public function getRemoteInstance(): ?RemoteInstance { |
||
| 210 | |||
| 211 | /** |
||
| 212 | * @return bool |
||
| 213 | */ |
||
| 214 | public function hasRemoteInstance(): bool { |
||
| 217 | |||
| 218 | |||
| 219 | /** |
||
| 220 | * Get the full FederatedUser for a local user. |
||
| 221 | * Will generate the SingleId if none exist |
||
| 222 | * |
||
| 223 | * @param string $userId |
||
| 224 | * |
||
| 225 | * @return FederatedUser |
||
| 226 | * @throws CircleNotFoundException |
||
| 227 | * @throws FederatedUserNotFoundException |
||
| 228 | * @throws InvalidIdException |
||
| 229 | */ |
||
| 230 | public function getLocalFederatedUser(string $userId): FederatedUser { |
||
| 242 | |||
| 243 | |||
| 244 | /** |
||
| 245 | * some ./occ commands allows to add an Initiator, or force the PoV from the local circles' owner |
||
| 246 | * |
||
| 247 | * TODO: manage non-user type ? |
||
| 248 | * |
||
| 249 | * @param string $userId |
||
| 250 | * @param string $circleId |
||
| 251 | * @param bool $bypass |
||
| 252 | * |
||
| 253 | * @throws CircleNotFoundException |
||
| 254 | * @throws FederatedUserException |
||
| 255 | * @throws FederatedUserNotFoundException |
||
| 256 | * @throws InvalidIdException |
||
| 257 | * @throws InvalidItemException |
||
| 258 | * @throws OwnerNotFoundException |
||
| 259 | * @throws RemoteInstanceException |
||
| 260 | * @throws RemoteNotFoundException |
||
| 261 | * @throws RemoteResourceNotFoundException |
||
| 262 | * @throws RequestNetworkException |
||
| 263 | * @throws SignatoryException |
||
| 264 | * @throws UnknownRemoteException |
||
| 265 | * @throws UserTypeNotFoundException |
||
| 266 | */ |
||
| 267 | public function commandLineInitiator(string $userId, string $circleId = '', bool $bypass = false): void { |
||
| 292 | |||
| 293 | |||
| 294 | /** |
||
| 295 | * Works like getFederatedUser, but returns a member. |
||
| 296 | * Allow to specify a level: handle@instance,level |
||
| 297 | * |
||
| 298 | * Used for filters when searching for Circles |
||
| 299 | * TODO: Used outside of ./occ circles:manage:list ? |
||
| 300 | * |
||
| 301 | * @param string $userId |
||
| 302 | * @param int $level |
||
| 303 | * |
||
| 304 | * @return Member |
||
| 305 | * @throws CircleNotFoundException |
||
| 306 | * @throws FederatedUserException |
||
| 307 | * @throws FederatedUserNotFoundException |
||
| 308 | * @throws InvalidIdException |
||
| 309 | * @throws InvalidItemException |
||
| 310 | * @throws OwnerNotFoundException |
||
| 311 | * @throws RemoteInstanceException |
||
| 312 | * @throws RemoteNotFoundException |
||
| 313 | * @throws RemoteResourceNotFoundException |
||
| 314 | * @throws RequestNetworkException |
||
| 315 | * @throws SignatoryException |
||
| 316 | * @throws UnknownRemoteException |
||
| 317 | * @throws UserTypeNotFoundException |
||
| 318 | */ |
||
| 319 | public function getFederatedMember(string $userId, int $level = Member::LEVEL_MEMBER): Member { |
||
| 332 | |||
| 333 | |||
| 334 | /** |
||
| 335 | * Generate a FederatedUser based on local data. |
||
| 336 | * WARNING: There is no confirmation that the returned FederatedUser exists or is valid at this point. |
||
| 337 | * Use getFederatedUser() instead if a valid and confirmed FederatedUser is needed. |
||
| 338 | * |
||
| 339 | * if $federatedId is a known SingleId, will returns data from the local database. |
||
| 340 | * if $federatedId is a local username, will returns data from the local database. |
||
| 341 | * Otherwise, the FederatedUser will not contains a SingleId. |
||
| 342 | * |
||
| 343 | * @param string $federatedId |
||
| 344 | * @param int $type |
||
| 345 | * |
||
| 346 | * @return FederatedUser |
||
| 347 | */ |
||
| 348 | public function generateFederatedUser(string $federatedId, int $type = Member::TYPE_USER): FederatedUser { |
||
| 366 | |||
| 367 | |||
| 368 | /** |
||
| 369 | * get a valid FederatedUser, based on the federatedId (userId@instance) its the type. |
||
| 370 | * If instance is local, get the local valid FederatedUser |
||
| 371 | * If instance is not local, get the remote valid FederatedUser |
||
| 372 | * |
||
| 373 | * @param string $federatedId |
||
| 374 | * @param int $userType |
||
| 375 | * |
||
| 376 | * @return FederatedUser |
||
| 377 | * @throws CircleNotFoundException |
||
| 378 | * @throws FederatedUserException |
||
| 379 | * @throws FederatedUserNotFoundException |
||
| 380 | * @throws InvalidIdException |
||
| 381 | * @throws InvalidItemException |
||
| 382 | * @throws RemoteInstanceException |
||
| 383 | * @throws RemoteNotFoundException |
||
| 384 | * @throws RemoteResourceNotFoundException |
||
| 385 | * @throws RequestNetworkException |
||
| 386 | * @throws SignatoryException |
||
| 387 | * @throws UnknownRemoteException |
||
| 388 | * @throws UserTypeNotFoundException |
||
| 389 | * @throws OwnerNotFoundException |
||
| 390 | */ |
||
| 391 | public function getFederatedUser(string $federatedId, int $userType = Member::TYPE_USER): FederatedUser { |
||
| 401 | |||
| 402 | |||
| 403 | /** |
||
| 404 | * @param string $federatedId |
||
| 405 | * |
||
| 406 | * @return FederatedUser |
||
| 407 | * @throws CircleNotFoundException |
||
| 408 | * @throws FederatedUserNotFoundException |
||
| 409 | * @throws InvalidIdException |
||
| 410 | * @throws RemoteInstanceException |
||
| 411 | * @throws RemoteNotFoundException |
||
| 412 | * @throws RemoteResourceNotFoundException |
||
| 413 | * @throws UnknownRemoteException |
||
| 414 | * @throws InvalidItemException |
||
| 415 | * @throws RequestNetworkException |
||
| 416 | * @throws SignatoryException |
||
| 417 | * @throws FederatedUserException |
||
| 418 | */ |
||
| 419 | private function getFederatedUser_User(string $federatedId): FederatedUser { |
||
| 432 | |||
| 433 | |||
| 434 | /** |
||
| 435 | * // TODO: do we need to have this working on remote instance ? |
||
| 436 | * |
||
| 437 | * @param string $federatedId |
||
| 438 | * |
||
| 439 | * @return FederatedUser |
||
| 440 | * @throws CircleNotFoundException |
||
| 441 | * @throws OwnerNotFoundException |
||
| 442 | */ |
||
| 443 | private function getFederatedUser_Circle(string $federatedId): FederatedUser { |
||
| 457 | |||
| 458 | |||
| 459 | /** |
||
| 460 | * extract userID and instance from a federatedId |
||
| 461 | * |
||
| 462 | * @param string $federatedId |
||
| 463 | * |
||
| 464 | * @return array |
||
| 465 | */ |
||
| 466 | private function extractIdAndInstance(string $federatedId): array { |
||
| 477 | |||
| 478 | |||
| 479 | /** |
||
| 480 | * @param FederatedUser $federatedUser |
||
| 481 | * |
||
| 482 | * @throws CircleNotFoundException |
||
| 483 | * @throws InvalidIdException |
||
| 484 | */ |
||
| 485 | private function fillSingleCircleId(FederatedUser $federatedUser): void { |
||
| 493 | |||
| 494 | |||
| 495 | /** |
||
| 496 | * get the Single Circle from a local user |
||
| 497 | * |
||
| 498 | * @param FederatedUser $federatedUser |
||
| 499 | * |
||
| 500 | * @return Circle |
||
| 501 | * @throws CircleNotFoundException |
||
| 502 | * @throws InvalidIdException |
||
| 503 | * @throws FederatedUserException |
||
| 504 | */ |
||
| 505 | private function getSingleCircle(FederatedUser $federatedUser): Circle { |
||
| 533 | |||
| 534 | |||
| 535 | /** |
||
| 536 | * Confirm that all field of a FederatedUser are filled. |
||
| 537 | * |
||
| 538 | * @param FederatedUser $federatedUser |
||
| 539 | * |
||
| 540 | * @throws FederatedUserException |
||
| 541 | */ |
||
| 542 | private function confirmFederatedUser(FederatedUser $federatedUser): void { |
||
| 551 | |||
| 552 | /** |
||
| 553 | * Confirm that the singleId of a FederatedUser is unique and not used to any other member of the |
||
| 554 | * database. |
||
| 555 | * |
||
| 556 | * @param FederatedUser $federatedUser |
||
| 557 | * |
||
| 558 | * @throws FederatedUserException |
||
| 559 | */ |
||
| 560 | public function confirmLocalSingleId(FederatedUser $federatedUser): void { |
||
| 573 | |||
| 574 | |||
| 575 | // /** |
||
| 576 | // * @param FederatedUser $federatedUser |
||
| 577 | // * |
||
| 578 | // * @return Membership[] |
||
| 579 | // */ |
||
| 580 | // public function generateMemberships(FederatedUser $federatedUser): array { |
||
| 581 | // $circles = $this->circleRequest->getCircles(null, $federatedUser); |
||
| 582 | // $memberships = []; |
||
| 583 | // foreach ($circles as $circle) { |
||
| 584 | // $initiator = $circle->getInitiator(); |
||
| 585 | // if (!$initiator->isMember()) { |
||
| 586 | // continue; |
||
| 587 | // } |
||
| 588 | // |
||
| 589 | // $memberships[] = new Membership( |
||
| 590 | // $initiator->getId(), $circle->getId(), $federatedUser->getSingleId(), $initiator->getLevel() |
||
| 591 | // ); |
||
| 592 | // |
||
| 593 | //// $newUser = new CurrentUser($circle->getId(), Member::TYPE_CIRCLE, ''); |
||
| 594 | //// $circles = $this->circleRequest->getCircles(null, $currentUser); |
||
| 595 | // } |
||
| 596 | // |
||
| 597 | // return $memberships; |
||
| 598 | // } |
||
| 599 | // |
||
| 600 | // |
||
| 601 | // /** |
||
| 602 | // * @param FederatedUser|null $federatedUser |
||
| 603 | // */ |
||
| 604 | // public function updateMemberships(?FederatedUser $federatedUser = null) { |
||
| 605 | // if (is_null($federatedUser)) { |
||
| 606 | // $federatedUser = $this->getCurrentUser(); |
||
| 607 | // } else { |
||
| 608 | // $federatedUser->setMemberships($this->membershipRequest->getMemberships($federatedUser)); |
||
| 609 | // } |
||
| 610 | // |
||
| 611 | // if (is_null($federatedUser)) { |
||
| 612 | // return; |
||
| 613 | // } |
||
| 614 | // |
||
| 615 | // $last = $this->generateMemberships($federatedUser); |
||
| 616 | // |
||
| 617 | // echo 'known: ' . json_encode($federatedUser->getMemberships()) . "\n"; |
||
| 618 | // echo 'last: ' . json_encode($last) . "\n"; |
||
| 619 | // |
||
| 620 | //// |
||
| 621 | //// $circles = $this->circleRequest->getCircles(null, $viewer); |
||
| 622 | //// foreach ($circles as $circle) { |
||
| 623 | //// $viewer = $circle->getViewer(); |
||
| 624 | //// if (!$viewer->isMember()) { |
||
| 625 | //// continue; |
||
| 626 | //// } |
||
| 627 | //// |
||
| 628 | //// echo 'new member: ' . json_encode($viewer) . "\n"; |
||
| 629 | ////// $this->federatedUserService->updateMembership($circle); |
||
| 630 | //// } |
||
| 631 | // |
||
| 632 | // |
||
| 633 | // } |
||
| 634 | |||
| 635 | |||
| 636 | } |
||
| 637 | |||
| 638 |