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 |
||
83 | class FederatedUserService { |
||
84 | |||
85 | |||
86 | use TArrayTools; |
||
87 | use TStringTools; |
||
88 | use TNC22Logger; |
||
89 | |||
90 | |||
91 | const CONFLICT_001 = 1; |
||
92 | const CONFLICT_002 = 2; |
||
93 | const CONFLICT_003 = 3; |
||
94 | const CONFLICT_004 = 4; |
||
95 | const CONFLICT_005 = 5; |
||
96 | |||
97 | |||
98 | /** @var IUserSession */ |
||
99 | private $userSession; |
||
100 | |||
101 | /** @var IUserManager */ |
||
102 | private $userManager; |
||
103 | |||
104 | /** @var IGroupManager */ |
||
105 | private $groupManager; |
||
106 | |||
107 | /** @var FederatedEventService */ |
||
108 | private $federatedEventService; |
||
109 | |||
110 | /** @var MembershipService */ |
||
111 | private $membershipService; |
||
112 | |||
113 | /** @var CircleRequest */ |
||
114 | private $circleRequest; |
||
115 | |||
116 | /** @var MemberRequest */ |
||
117 | private $memberRequest; |
||
118 | |||
119 | /** @var RemoteService */ |
||
120 | private $remoteService; |
||
121 | |||
122 | /** @var RemoteStreamService */ |
||
123 | private $remoteStreamService; |
||
124 | |||
125 | /** @var ContactService */ |
||
126 | private $contactService; |
||
127 | |||
128 | /** @var InterfaceService */ |
||
129 | private $interfaceService; |
||
130 | |||
131 | /** @var ConfigService */ |
||
132 | private $configService; |
||
133 | |||
134 | |||
135 | /** @var FederatedUser */ |
||
136 | private $currentUser = null; |
||
137 | |||
138 | /** @var FederatedUser */ |
||
139 | private $currentApp = null; |
||
140 | |||
141 | /** @var RemoteInstance */ |
||
142 | private $remoteInstance = null; |
||
143 | |||
144 | /** @var bool */ |
||
145 | private $bypass = false; |
||
146 | |||
147 | /** @var bool */ |
||
148 | private $initiatedByOcc = false; |
||
149 | |||
150 | /** @var FederatedUser */ |
||
151 | private $initiatedByAdmin = null; |
||
152 | |||
153 | |||
154 | /** |
||
155 | * FederatedUserService constructor. |
||
156 | * |
||
157 | * @param IUserSession $userSession |
||
158 | * @param IUserManager $userManager |
||
159 | * @param IGroupManager $groupManager |
||
160 | * @param FederatedEventService $federatedEventService |
||
161 | * @param MembershipService $membershipService |
||
162 | * @param CircleRequest $circleRequest |
||
163 | * @param MemberRequest $memberRequest |
||
164 | * @param RemoteService $remoteService |
||
165 | * @param RemoteStreamService $remoteStreamService |
||
166 | * @param ContactService $contactService |
||
167 | * @param InterfaceService $interfaceService |
||
168 | * @param ConfigService $configService |
||
169 | */ |
||
170 | public function __construct( |
||
201 | |||
202 | |||
203 | /** |
||
204 | * @throws FederatedUserException |
||
205 | * @throws FederatedUserNotFoundException |
||
206 | * @throws InvalidIdException |
||
207 | * @throws SingleCircleNotFoundException |
||
208 | * @throws RequestBuilderException |
||
209 | */ |
||
210 | public function initCurrentUser() { |
||
218 | |||
219 | |||
220 | /** |
||
221 | * @param IUser|null $user |
||
222 | * |
||
223 | * @throws FederatedUserException |
||
224 | * @throws FederatedUserNotFoundException |
||
225 | * @throws InvalidIdException |
||
226 | * @throws SingleCircleNotFoundException |
||
227 | * @throws RequestBuilderException |
||
228 | */ |
||
229 | public function setLocalCurrentUser(?IUser $user): void { |
||
236 | |||
237 | /** |
||
238 | * @param string $userId |
||
239 | * |
||
240 | * @throws FederatedUserException |
||
241 | * @throws FederatedUserNotFoundException |
||
242 | * @throws InvalidIdException |
||
243 | * @throws RequestBuilderException |
||
244 | * @throws SingleCircleNotFoundException |
||
245 | */ |
||
246 | public function setLocalCurrentUserId(string $userId): void { |
||
249 | |||
250 | /** |
||
251 | * @param string $appId |
||
252 | * @param int $appNumber |
||
253 | * |
||
254 | * @throws FederatedUserException |
||
255 | * @throws InvalidIdException |
||
256 | * @throws SingleCircleNotFoundException |
||
257 | */ |
||
258 | public function setLocalCurrentApp(string $appId, int $appNumber): void { |
||
261 | |||
262 | |||
263 | /** |
||
264 | * set a CurrentUser, based on a IFederatedUser. |
||
265 | * CurrentUser is mainly used to manage rights when requesting the database. |
||
266 | * |
||
267 | * @param IFederatedUser $federatedUser |
||
268 | * |
||
269 | * @throws FederatedUserException |
||
270 | */ |
||
271 | public function setCurrentUser(IFederatedUser $federatedUser): void { |
||
282 | |||
283 | /** |
||
284 | * |
||
285 | */ |
||
286 | public function unsetCurrentUser(): void { |
||
289 | |||
290 | /** |
||
291 | * @return FederatedUser|null |
||
292 | */ |
||
293 | public function getCurrentUser(): ?FederatedUser { |
||
296 | |||
297 | /** |
||
298 | * @return bool |
||
299 | */ |
||
300 | public function hasCurrentUser(): bool { |
||
303 | |||
304 | /** |
||
305 | * @throws InitiatorNotFoundException |
||
306 | */ |
||
307 | public function mustHaveCurrentUser(): void { |
||
315 | |||
316 | /** |
||
317 | * @param bool $bypass |
||
318 | */ |
||
319 | public function bypassCurrentUserCondition(bool $bypass): void { |
||
322 | |||
323 | |||
324 | /** |
||
325 | * @param bool $initiatedByOcc |
||
326 | */ |
||
327 | public function setInitiatedByOcc(bool $initiatedByOcc): void { |
||
330 | |||
331 | /** |
||
332 | * @return bool |
||
333 | */ |
||
334 | public function isInitiatedByOcc(): bool { |
||
337 | |||
338 | /** |
||
339 | * @return bool |
||
340 | */ |
||
341 | public function isInitiatedByAdmin(): bool { |
||
344 | |||
345 | /** |
||
346 | * @param FederatedUser $patron |
||
347 | */ |
||
348 | public function setInitiatedByAdmin(FederatedUser $patron): void { |
||
351 | |||
352 | /** |
||
353 | * @return FederatedUser |
||
354 | */ |
||
355 | public function getInitiatedByAdmin(): FederatedUser { |
||
358 | |||
359 | /** |
||
360 | * @param IUser $user |
||
|
|||
361 | * |
||
362 | * @throws ContactAddressBookNotFoundException |
||
363 | * @throws ContactFormatException |
||
364 | * @throws ContactNotFoundException |
||
365 | * @throws FederatedUserException |
||
366 | * @throws FederatedUserNotFoundException |
||
367 | * @throws InvalidIdException |
||
368 | * @throws RequestBuilderException |
||
369 | * @throws SingleCircleNotFoundException |
||
370 | */ |
||
371 | public function setCurrentPatron(string $userId): void { |
||
376 | |||
377 | |||
378 | /** |
||
379 | * @param Member $member |
||
380 | * |
||
381 | * @throws ContactAddressBookNotFoundException |
||
382 | * @throws ContactFormatException |
||
383 | * @throws ContactNotFoundException |
||
384 | * @throws FederatedUserException |
||
385 | * @throws InvalidIdException |
||
386 | * @throws RequestBuilderException |
||
387 | * @throws SingleCircleNotFoundException |
||
388 | */ |
||
389 | public function setMemberPatron(Member $member): void { |
||
398 | |||
399 | |||
400 | /** |
||
401 | * @return FederatedUser|null |
||
402 | */ |
||
403 | public function getCurrentApp(): ?FederatedUser { |
||
406 | |||
407 | /** |
||
408 | * @return bool |
||
409 | */ |
||
410 | public function hasCurrentApp(): bool { |
||
413 | |||
414 | |||
415 | /** |
||
416 | * set a RemoteInstance, mostly from a remote request (RemoteController) |
||
417 | * Used to limit rights in some request in the local database. |
||
418 | * |
||
419 | * @param RemoteInstance $remoteInstance |
||
420 | */ |
||
421 | public function setRemoteInstance(RemoteInstance $remoteInstance): void { |
||
424 | |||
425 | /** |
||
426 | * @return RemoteInstance|null |
||
427 | */ |
||
428 | public function getRemoteInstance(): ?RemoteInstance { |
||
431 | |||
432 | /** |
||
433 | * @return bool |
||
434 | */ |
||
435 | public function hasRemoteInstance(): bool { |
||
438 | |||
439 | |||
440 | /** |
||
441 | * Get the full FederatedUser for a local user. |
||
442 | * Will generate the SingleId if none exist |
||
443 | * |
||
444 | * @param string $userId |
||
445 | * @param bool $check |
||
446 | * |
||
447 | * @return FederatedUser |
||
448 | * @throws ContactAddressBookNotFoundException |
||
449 | * @throws ContactFormatException |
||
450 | * @throws ContactNotFoundException |
||
451 | * @throws FederatedUserException |
||
452 | * @throws FederatedUserNotFoundException |
||
453 | * @throws InvalidIdException |
||
454 | * @throws RequestBuilderException |
||
455 | * @throws SingleCircleNotFoundException |
||
456 | */ |
||
457 | public function getLocalFederatedUser(string $userId, bool $check = true): FederatedUser { |
||
474 | |||
475 | |||
476 | /** |
||
477 | * Get the full FederatedUser for a local user. |
||
478 | * Will generate the SingleId if none exist |
||
479 | * |
||
480 | * @param string $appId |
||
481 | * @param int $appNumber |
||
482 | * |
||
483 | * @return FederatedUser |
||
484 | * @throws ContactAddressBookNotFoundException |
||
485 | * @throws ContactFormatException |
||
486 | * @throws ContactNotFoundException |
||
487 | * @throws FederatedUserException |
||
488 | * @throws InvalidIdException |
||
489 | * @throws RequestBuilderException |
||
490 | * @throws SingleCircleNotFoundException |
||
491 | */ |
||
492 | public function getAppInitiator( |
||
511 | |||
512 | |||
513 | /** |
||
514 | * some ./occ commands allows to add an Initiator, or force the PoV from the local circles' owner |
||
515 | * |
||
516 | * TODO: manage non-user type ? |
||
517 | * |
||
518 | * @param string $userId |
||
519 | * @param int $userType |
||
520 | * @param string $circleId |
||
521 | * @param bool $bypass |
||
522 | * |
||
523 | * @throws CircleNotFoundException |
||
524 | * @throws FederatedItemException |
||
525 | * @throws FederatedUserException |
||
526 | * @throws FederatedUserNotFoundException |
||
527 | * @throws InvalidIdException |
||
528 | * @throws MemberNotFoundException |
||
529 | * @throws OwnerNotFoundException |
||
530 | * @throws RemoteInstanceException |
||
531 | * @throws RemoteNotFoundException |
||
532 | * @throws RemoteResourceNotFoundException |
||
533 | * @throws RequestBuilderException |
||
534 | * @throws SingleCircleNotFoundException |
||
535 | * @throws UnknownRemoteException |
||
536 | * @throws UserTypeNotFoundException |
||
537 | */ |
||
538 | public function commandLineInitiator( |
||
567 | |||
568 | |||
569 | /** |
||
570 | * Works like getFederatedUser, but returns a member. |
||
571 | * Allow to specify a level: handle@instance,level |
||
572 | * |
||
573 | * Used for filters when searching for Circles |
||
574 | * TODO: Used outside of ./occ circles:manage:list ? |
||
575 | * |
||
576 | * @param string $userId |
||
577 | * @param int $level |
||
578 | * |
||
579 | * @return Member |
||
580 | * @throws CircleNotFoundException |
||
581 | * @throws FederatedItemException |
||
582 | * @throws FederatedUserException |
||
583 | * @throws FederatedUserNotFoundException |
||
584 | * @throws InvalidIdException |
||
585 | * @throws MemberNotFoundException |
||
586 | * @throws OwnerNotFoundException |
||
587 | * @throws RemoteInstanceException |
||
588 | * @throws RemoteNotFoundException |
||
589 | * @throws RemoteResourceNotFoundException |
||
590 | * @throws SingleCircleNotFoundException |
||
591 | * @throws UnknownRemoteException |
||
592 | * @throws UserTypeNotFoundException |
||
593 | */ |
||
594 | public function getFederatedMember(string $userId, int $level = Member::LEVEL_MEMBER): Member { |
||
607 | |||
608 | |||
609 | /** |
||
610 | * get a valid FederatedUser, based on the federatedId (userId@instance) and its type. |
||
611 | * If instance is local, get the local valid FederatedUser |
||
612 | * If instance is not local, get the remote valid FederatedUser |
||
613 | * |
||
614 | * @param string $federatedId |
||
615 | * @param int $type |
||
616 | * |
||
617 | * @return FederatedUser |
||
618 | * @throws CircleNotFoundException |
||
619 | * @throws FederatedItemException |
||
620 | * @throws FederatedUserException |
||
621 | * @throws FederatedUserNotFoundException |
||
622 | * @throws InvalidIdException |
||
623 | * @throws MemberNotFoundException |
||
624 | * @throws OwnerNotFoundException |
||
625 | * @throws RemoteInstanceException |
||
626 | * @throws RemoteNotFoundException |
||
627 | * @throws RemoteResourceNotFoundException |
||
628 | * @throws SingleCircleNotFoundException |
||
629 | * @throws UnknownRemoteException |
||
630 | * @throws UserTypeNotFoundException |
||
631 | * @throws RequestBuilderException |
||
632 | */ |
||
633 | public function getFederatedUser(string $federatedId, int $type = Member::TYPE_SINGLE): FederatedUser { |
||
660 | |||
661 | |||
662 | /** |
||
663 | * Generate a FederatedUser based on local data. |
||
664 | * WARNING: There is no confirmation that the returned FederatedUser exists or is valid at this point. |
||
665 | * Use getFederatedUser() instead if a valid and confirmed FederatedUser is needed. |
||
666 | * |
||
667 | * if $federatedId is a known SingleId, will returns data from the local database. |
||
668 | * if $federatedId is a local username, will returns data from the local database. |
||
669 | * Otherwise, the FederatedUser will not contains a SingleId. |
||
670 | * |
||
671 | * @param string $federatedId |
||
672 | * @param int $type |
||
673 | * |
||
674 | * @return FederatedUser |
||
675 | */ |
||
676 | public function generateFederatedUser(string $federatedId, int $type = 0): FederatedUser { |
||
688 | |||
689 | |||
690 | /** |
||
691 | * @param FederatedUser $federatedUser |
||
692 | */ |
||
693 | public function deleteFederatedUser(FederatedUser $federatedUser): void { |
||
698 | |||
699 | |||
700 | /** |
||
701 | * @param string $singleId |
||
702 | * @param string $instance |
||
703 | * |
||
704 | * @return FederatedUser |
||
705 | * @throws CircleNotFoundException |
||
706 | * @throws FederatedItemException |
||
707 | * @throws FederatedUserException |
||
708 | * @throws FederatedUserNotFoundException |
||
709 | * @throws MemberNotFoundException |
||
710 | * @throws OwnerNotFoundException |
||
711 | * @throws RemoteInstanceException |
||
712 | * @throws RemoteNotFoundException |
||
713 | * @throws RemoteResourceNotFoundException |
||
714 | * @throws UnknownRemoteException |
||
715 | * @throws RequestBuilderException |
||
716 | */ |
||
717 | public function getFederatedUser_SingleId(string $singleId, string $instance): FederatedUser { |
||
732 | |||
733 | |||
734 | /** |
||
735 | * @param string $userId |
||
736 | * @param string $instance |
||
737 | * |
||
738 | * @return FederatedUser |
||
739 | * @throws FederatedUserException |
||
740 | * @throws FederatedUserNotFoundException |
||
741 | * @throws InvalidIdException |
||
742 | * @throws RemoteInstanceException |
||
743 | * @throws RemoteNotFoundException |
||
744 | * @throws RemoteResourceNotFoundException |
||
745 | * @throws SingleCircleNotFoundException |
||
746 | * @throws UnknownRemoteException |
||
747 | * @throws FederatedItemException |
||
748 | * @throws RequestBuilderException |
||
749 | */ |
||
750 | private function getFederatedUser_User(string $userId, string $instance): FederatedUser { |
||
765 | |||
766 | |||
767 | /** |
||
768 | * @param string $groupName |
||
769 | * @param string $instance |
||
770 | * |
||
771 | * @return FederatedUser |
||
772 | * @throws FederatedEventException |
||
773 | * @throws FederatedItemException |
||
774 | * @throws FederatedUserException |
||
775 | * @throws GroupNotFoundException |
||
776 | * @throws InitiatorNotConfirmedException |
||
777 | * @throws InvalidIdException |
||
778 | * @throws OwnerNotFoundException |
||
779 | * @throws RemoteInstanceException |
||
780 | * @throws RemoteNotFoundException |
||
781 | * @throws RemoteResourceNotFoundException |
||
782 | * @throws SingleCircleNotFoundException |
||
783 | * @throws UnknownRemoteException |
||
784 | * @throws RequestBuilderException |
||
785 | */ |
||
786 | public function getFederatedUser_Group(string $groupName, string $instance): FederatedUser { |
||
796 | |||
797 | |||
798 | /** |
||
799 | * @param string $mailAddress |
||
800 | * |
||
801 | * @return FederatedUser |
||
802 | * @throws ContactAddressBookNotFoundException |
||
803 | * @throws ContactFormatException |
||
804 | * @throws ContactNotFoundException |
||
805 | * @throws FederatedUserException |
||
806 | * @throws InvalidIdException |
||
807 | * @throws RequestBuilderException |
||
808 | * @throws SingleCircleNotFoundException |
||
809 | */ |
||
810 | public function getFederatedUser_Mail(string $mailAddress): FederatedUser { |
||
817 | |||
818 | |||
819 | /** |
||
820 | * @param string $contactPath |
||
821 | * |
||
822 | * @return FederatedUser |
||
823 | * @throws ContactAddressBookNotFoundException |
||
824 | * @throws ContactFormatException |
||
825 | * @throws ContactNotFoundException |
||
826 | * @throws FederatedUserException |
||
827 | * @throws InvalidIdException |
||
828 | * @throws RequestBuilderException |
||
829 | * @throws SingleCircleNotFoundException |
||
830 | */ |
||
831 | public function getFederatedUser_Contact(string $contactPath): FederatedUser { |
||
844 | |||
845 | |||
846 | /** |
||
847 | * extract userID and instance from a federatedId |
||
848 | * |
||
849 | * @param string $federatedId |
||
850 | * |
||
851 | * @return array |
||
852 | */ |
||
853 | public function extractIdAndInstance(string $federatedId): array { |
||
864 | |||
865 | |||
866 | /** |
||
867 | * @param FederatedUser $federatedUser |
||
868 | * @param bool $generate |
||
869 | * |
||
870 | * @throws ContactAddressBookNotFoundException |
||
871 | * @throws ContactFormatException |
||
872 | * @throws ContactNotFoundException |
||
873 | * @throws FederatedUserException |
||
874 | * @throws InvalidIdException |
||
875 | * @throws RequestBuilderException |
||
876 | * @throws SingleCircleNotFoundException |
||
877 | */ |
||
878 | private function fillSingleCircleId(FederatedUser $federatedUser, bool $generate = true): void { |
||
888 | |||
889 | |||
890 | /** |
||
891 | * get the Single Circle from a local user |
||
892 | * |
||
893 | * @param FederatedUser $federatedUser |
||
894 | * @param bool $generate |
||
895 | * |
||
896 | * @return Circle |
||
897 | * @throws ContactAddressBookNotFoundException |
||
898 | * @throws ContactFormatException |
||
899 | * @throws ContactNotFoundException |
||
900 | * @throws FederatedUserException |
||
901 | * @throws InvalidIdException |
||
902 | * @throws RequestBuilderException |
||
903 | * @throws SingleCircleNotFoundException |
||
904 | */ |
||
905 | private function getSingleCircle(FederatedUser $federatedUser, bool $generate = true): Circle { |
||
967 | |||
968 | |||
969 | /** |
||
970 | * Confirm that all field of a FederatedUser are filled. |
||
971 | * |
||
972 | * @param FederatedUser $federatedUser |
||
973 | * |
||
974 | * @throws FederatedUserException |
||
975 | */ |
||
976 | private function confirmFederatedUser(FederatedUser $federatedUser): void { |
||
985 | |||
986 | /** |
||
987 | * Confirm that the singleId of a FederatedUser is unique and not used to any other member of the |
||
988 | * database. |
||
989 | * |
||
990 | * @param FederatedUser $federatedUser |
||
991 | * |
||
992 | * @throws FederatedUserException |
||
993 | * @throws RequestBuilderException |
||
994 | * @deprecated: use confirmSingleIdUniqueness() |
||
995 | */ |
||
996 | public function confirmLocalSingleId(IFederatedUser $federatedUser): void { |
||
1009 | |||
1010 | |||
1011 | /** |
||
1012 | * // TODO: implement this check in a maintenance background job |
||
1013 | * |
||
1014 | * @param IFederatedUser $federatedUser |
||
1015 | * |
||
1016 | * @throws FederatedUserException |
||
1017 | * @throws RemoteNotFoundException |
||
1018 | * @throws RequestBuilderException |
||
1019 | * @throws UnknownRemoteException |
||
1020 | */ |
||
1021 | public function confirmSingleIdUniqueness(IFederatedUser $federatedUser): void { |
||
1059 | |||
1060 | |||
1061 | /** |
||
1062 | * @param IFederatedUser $federatedUser |
||
1063 | * @param Member $knownMember |
||
1064 | * @param int $conflict |
||
1065 | * |
||
1066 | * @throws FederatedUserException |
||
1067 | */ |
||
1068 | private function markConflict(IFederatedUser $federatedUser, Member $knownMember, int $conflict): void { |
||
1101 | |||
1102 | /** |
||
1103 | * @param string $groupId |
||
1104 | * |
||
1105 | * @return Circle |
||
1106 | * @throws GroupNotFoundException |
||
1107 | * @throws FederatedEventException |
||
1108 | * @throws FederatedItemException |
||
1109 | * @throws FederatedUserException |
||
1110 | * @throws InitiatorNotConfirmedException |
||
1111 | * @throws InvalidIdException |
||
1112 | * @throws OwnerNotFoundException |
||
1113 | * @throws RemoteInstanceException |
||
1114 | * @throws RemoteNotFoundException |
||
1115 | * @throws RemoteResourceNotFoundException |
||
1116 | * @throws SingleCircleNotFoundException |
||
1117 | * @throws UnknownRemoteException |
||
1118 | * @throws RequestBuilderException |
||
1119 | */ |
||
1120 | public function getGroupCircle(string $groupId): Circle { |
||
1157 | |||
1158 | } |
||
1159 | |||
1160 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.