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( |
||
| 129 | |||
| 130 | |||
| 131 | /** |
||
| 132 | * @param string $userId |
||
| 133 | * |
||
| 134 | * @throws CircleNotFoundException |
||
| 135 | * @throws NoUserException |
||
| 136 | */ |
||
| 137 | public function setLocalInitiator(string $userId): void { |
||
| 140 | |||
| 141 | /** |
||
| 142 | * @param IFederatedUser $federatedUser |
||
| 143 | * |
||
| 144 | * @throws FederatedUserNotFoundException |
||
| 145 | * @throws FederatedUserException |
||
| 146 | */ |
||
| 147 | public function setCurrentUser(IFederatedUser $federatedUser): void { |
||
| 158 | |||
| 159 | /** |
||
| 160 | * @return FederatedUser|null |
||
| 161 | */ |
||
| 162 | public function getCurrentUser(): ?FederatedUser { |
||
| 165 | |||
| 166 | /** |
||
| 167 | * @return bool |
||
| 168 | */ |
||
| 169 | public function hasCurrentUser(): bool { |
||
| 172 | |||
| 173 | /** |
||
| 174 | * @throws InitiatorNotFoundException |
||
| 175 | */ |
||
| 176 | public function mustHaveCurrentUser(): void { |
||
| 184 | |||
| 185 | /** |
||
| 186 | * @param bool $bypass |
||
| 187 | */ |
||
| 188 | public function bypassCurrentUserCondition(bool $bypass): void { |
||
| 191 | |||
| 192 | |||
| 193 | /** |
||
| 194 | * @param RemoteInstance $remoteInstance |
||
| 195 | */ |
||
| 196 | public function setRemoteInstance(RemoteInstance $remoteInstance): void { |
||
| 199 | |||
| 200 | /** |
||
| 201 | * @return RemoteInstance|null |
||
| 202 | */ |
||
| 203 | public function getRemoteInstance(): ?RemoteInstance { |
||
| 206 | |||
| 207 | |||
| 208 | /** |
||
| 209 | * @param string $userId |
||
| 210 | * |
||
| 211 | * @return FederatedUser |
||
| 212 | * @throws CircleNotFoundException |
||
| 213 | * @throws FederatedUserNotFoundException |
||
| 214 | * @throws InvalidIdException |
||
| 215 | */ |
||
| 216 | public function createLocalFederatedUser(string $userId): FederatedUser { |
||
| 228 | |||
| 229 | |||
| 230 | /** |
||
| 231 | * @param string $federatedId |
||
| 232 | * @param int $userType |
||
| 233 | * |
||
| 234 | * @return FederatedUser |
||
| 235 | * @throws CircleNotFoundException |
||
| 236 | * @throws FederatedUserException |
||
| 237 | * @throws FederatedUserNotFoundException |
||
| 238 | * @throws InvalidIdException |
||
| 239 | * @throws InvalidItemException |
||
| 240 | * @throws RemoteInstanceException |
||
| 241 | * @throws RemoteNotFoundException |
||
| 242 | * @throws RemoteResourceNotFoundException |
||
| 243 | * @throws RequestNetworkException |
||
| 244 | * @throws SignatoryException |
||
| 245 | * @throws UnknownRemoteException |
||
| 246 | * @throws UserTypeNotFoundException |
||
| 247 | */ |
||
| 248 | public function createFederatedUser(string $federatedId, int $userType = Member::TYPE_USER |
||
| 257 | |||
| 258 | /** |
||
| 259 | * @param string $userId |
||
| 260 | * |
||
| 261 | * @return FederatedUser |
||
| 262 | * @throws CircleNotFoundException |
||
| 263 | * @throws FederatedUserNotFoundException |
||
| 264 | * @throws InvalidIdException |
||
| 265 | * @throws RemoteInstanceException |
||
| 266 | * @throws RemoteNotFoundException |
||
| 267 | * @throws RemoteResourceNotFoundException |
||
| 268 | * @throws UnknownRemoteException |
||
| 269 | * @throws InvalidItemException |
||
| 270 | * @throws RequestNetworkException |
||
| 271 | * @throws SignatoryException |
||
| 272 | * @throws FederatedUserException |
||
| 273 | */ |
||
| 274 | public function createFederatedUserTypeUser(string $userId): FederatedUser { |
||
| 292 | |||
| 293 | |||
| 294 | /** |
||
| 295 | * some ./occ commands allows to add an Initiator |
||
| 296 | * TODO: manage non-user type |
||
| 297 | * |
||
| 298 | * @param string $userId |
||
| 299 | * @param string $circleId |
||
| 300 | * @param bool $bypass |
||
| 301 | * |
||
| 302 | * @throws CircleNotFoundException |
||
| 303 | * @throws FederatedUserException |
||
| 304 | * @throws FederatedUserNotFoundException |
||
| 305 | * @throws InvalidIdException |
||
| 306 | * @throws InvalidItemException |
||
| 307 | * @throws OwnerNotFoundException |
||
| 308 | * @throws RemoteInstanceException |
||
| 309 | * @throws RemoteNotFoundException |
||
| 310 | * @throws RemoteResourceNotFoundException |
||
| 311 | * @throws RequestNetworkException |
||
| 312 | * @throws SignatoryException |
||
| 313 | * @throws UnknownRemoteException |
||
| 314 | */ |
||
| 315 | public function commandLineInitiator(string $userId, string $circleId = '', bool $bypass = false): void { |
||
| 341 | |||
| 342 | |||
| 343 | /** |
||
| 344 | * TODO: Is it needed outside of CirclesList ? |
||
| 345 | * |
||
| 346 | * @param string $userId |
||
| 347 | * @param int $level |
||
| 348 | * |
||
| 349 | * @return Member |
||
| 350 | * @throws CircleNotFoundException |
||
| 351 | * @throws NoUserException |
||
| 352 | */ |
||
| 353 | public function createFilterMember(string $userId, int $level = Member::LEVEL_MEMBER): Member { |
||
| 366 | |||
| 367 | |||
| 368 | /** |
||
| 369 | * @param FederatedUser $federatedUser |
||
| 370 | * |
||
| 371 | * @throws CircleNotFoundException |
||
| 372 | * @throws InvalidIdException |
||
| 373 | */ |
||
| 374 | private function fillSingleCircleId(FederatedUser $federatedUser): void { |
||
| 382 | |||
| 383 | |||
| 384 | /** |
||
| 385 | * @param FederatedUser $federatedUser |
||
| 386 | * |
||
| 387 | * @return Circle |
||
| 388 | * @throws CircleNotFoundException |
||
| 389 | * @throws InvalidIdException |
||
| 390 | */ |
||
| 391 | public function getSingleCircle(FederatedUser $federatedUser): Circle { |
||
| 415 | |||
| 416 | |||
| 417 | /** |
||
| 418 | * @param FederatedUser $federatedUser |
||
| 419 | * |
||
| 420 | * @throws FederatedUserException |
||
| 421 | */ |
||
| 422 | public function confirmFederatedUser(FederatedUser $federatedUser): void { |
||
| 431 | |||
| 432 | /** |
||
| 433 | * @param FederatedUser $federatedUser |
||
| 434 | * |
||
| 435 | * @throws FederatedUserException |
||
| 436 | */ |
||
| 437 | public function confirmSingleId(FederatedUser $federatedUser): void { |
||
| 450 | |||
| 451 | |||
| 452 | /** |
||
| 453 | * @param FederatedUser $federatedUser |
||
| 454 | * |
||
| 455 | * @return Membership[] |
||
| 456 | */ |
||
| 457 | public function generateMemberships(FederatedUser $federatedUser): array { |
||
| 476 | |||
| 477 | |||
| 478 | /** |
||
| 479 | * @param FederatedUser|null $federatedUser |
||
| 480 | */ |
||
| 481 | public function updateMemberships(?FederatedUser $federatedUser = null) { |
||
| 511 | |||
| 512 | } |
||
| 513 | |||
| 514 |