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 | |||
151 | /** |
||
152 | * FederatedUserService constructor. |
||
153 | * |
||
154 | * @param IUserSession $userSession |
||
155 | * @param IUserManager $userManager |
||
156 | * @param IGroupManager $groupManager |
||
157 | * @param FederatedEventService $federatedEventService |
||
158 | * @param MembershipService $membershipService |
||
159 | * @param CircleRequest $circleRequest |
||
160 | * @param MemberRequest $memberRequest |
||
161 | * @param RemoteService $remoteService |
||
162 | * @param RemoteStreamService $remoteStreamService |
||
163 | * @param ContactService $contactService |
||
164 | * @param InterfaceService $interfaceService |
||
165 | * @param ConfigService $configService |
||
166 | */ |
||
167 | public function __construct( |
||
198 | |||
199 | |||
200 | /** |
||
201 | * @throws FederatedUserException |
||
202 | * @throws FederatedUserNotFoundException |
||
203 | * @throws InvalidIdException |
||
204 | * @throws SingleCircleNotFoundException |
||
205 | * @throws RequestBuilderException |
||
206 | */ |
||
207 | public function initCurrentUser() { |
||
215 | |||
216 | |||
217 | /** |
||
218 | * @param IUser|null $user |
||
219 | * |
||
220 | * @throws FederatedUserException |
||
221 | * @throws FederatedUserNotFoundException |
||
222 | * @throws InvalidIdException |
||
223 | * @throws SingleCircleNotFoundException |
||
224 | * @throws RequestBuilderException |
||
225 | */ |
||
226 | public function setLocalCurrentUser(?IUser $user): void { |
||
233 | |||
234 | /** |
||
235 | * @param string $userId |
||
236 | * |
||
237 | * @throws FederatedUserException |
||
238 | * @throws FederatedUserNotFoundException |
||
239 | * @throws InvalidIdException |
||
240 | * @throws RequestBuilderException |
||
241 | * @throws SingleCircleNotFoundException |
||
242 | */ |
||
243 | public function setLocalCurrentUserId(string $userId): void { |
||
246 | |||
247 | /** |
||
248 | * @param string $appId |
||
249 | * @param int $appNumber |
||
250 | * |
||
251 | * @throws FederatedUserException |
||
252 | * @throws InvalidIdException |
||
253 | * @throws SingleCircleNotFoundException |
||
254 | */ |
||
255 | public function setLocalCurrentApp(string $appId, int $appNumber): void { |
||
258 | |||
259 | |||
260 | /** |
||
261 | * set a CurrentUser, based on a IFederatedUser. |
||
262 | * CurrentUser is mainly used to manage rights when requesting the database. |
||
263 | * |
||
264 | * @param IFederatedUser $federatedUser |
||
265 | * |
||
266 | * @throws FederatedUserException |
||
267 | */ |
||
268 | public function setCurrentUser(IFederatedUser $federatedUser): void { |
||
279 | |||
280 | /** |
||
281 | * |
||
282 | */ |
||
283 | public function unsetCurrentUser(): void { |
||
286 | |||
287 | /** |
||
288 | * @return FederatedUser|null |
||
289 | */ |
||
290 | public function getCurrentUser(): ?FederatedUser { |
||
293 | |||
294 | /** |
||
295 | * @return bool |
||
296 | */ |
||
297 | public function hasCurrentUser(): bool { |
||
300 | |||
301 | /** |
||
302 | * @throws InitiatorNotFoundException |
||
303 | */ |
||
304 | public function mustHaveCurrentUser(): void { |
||
312 | |||
313 | /** |
||
314 | * @param bool $bypass |
||
315 | */ |
||
316 | public function bypassCurrentUserCondition(bool $bypass): void { |
||
319 | |||
320 | |||
321 | /** |
||
322 | * @param bool $initiatedByOcc |
||
323 | */ |
||
324 | public function setInitiatedByOcc(bool $initiatedByOcc): void { |
||
327 | |||
328 | /** |
||
329 | * @return bool |
||
330 | */ |
||
331 | public function isInitiatedByOcc(): bool { |
||
334 | |||
335 | /** |
||
336 | * @param Member $member |
||
337 | * |
||
338 | * @throws ContactAddressBookNotFoundException |
||
339 | * @throws ContactFormatException |
||
340 | * @throws ContactNotFoundException |
||
341 | * @throws FederatedUserException |
||
342 | * @throws InvalidIdException |
||
343 | * @throws RequestBuilderException |
||
344 | * @throws SingleCircleNotFoundException |
||
345 | */ |
||
346 | public function setMemberPatron(Member $member): void { |
||
353 | |||
354 | |||
355 | /** |
||
356 | * @return FederatedUser|null |
||
357 | */ |
||
358 | public function getCurrentApp(): ?FederatedUser { |
||
361 | |||
362 | /** |
||
363 | * @return bool |
||
364 | */ |
||
365 | public function hasCurrentApp(): bool { |
||
368 | |||
369 | |||
370 | /** |
||
371 | * set a RemoteInstance, mostly from a remote request (RemoteController) |
||
372 | * Used to limit rights in some request in the local database. |
||
373 | * |
||
374 | * @param RemoteInstance $remoteInstance |
||
375 | */ |
||
376 | public function setRemoteInstance(RemoteInstance $remoteInstance): void { |
||
379 | |||
380 | /** |
||
381 | * @return RemoteInstance|null |
||
382 | */ |
||
383 | public function getRemoteInstance(): ?RemoteInstance { |
||
386 | |||
387 | /** |
||
388 | * @return bool |
||
389 | */ |
||
390 | public function hasRemoteInstance(): bool { |
||
393 | |||
394 | |||
395 | /** |
||
396 | * Get the full FederatedUser for a local user. |
||
397 | * Will generate the SingleId if none exist |
||
398 | * |
||
399 | * @param string $userId |
||
400 | * @param bool $check |
||
401 | * |
||
402 | * @return FederatedUser |
||
403 | * @throws ContactAddressBookNotFoundException |
||
404 | * @throws ContactFormatException |
||
405 | * @throws ContactNotFoundException |
||
406 | * @throws FederatedUserException |
||
407 | * @throws FederatedUserNotFoundException |
||
408 | * @throws InvalidIdException |
||
409 | * @throws RequestBuilderException |
||
410 | * @throws SingleCircleNotFoundException |
||
411 | */ |
||
412 | public function getLocalFederatedUser(string $userId, bool $check = true): FederatedUser { |
||
429 | |||
430 | |||
431 | /** |
||
432 | * Get the full FederatedUser for a local user. |
||
433 | * Will generate the SingleId if none exist |
||
434 | * |
||
435 | * @param string $appId |
||
436 | * @param int $appNumber |
||
437 | * |
||
438 | * @return FederatedUser |
||
439 | * @throws ContactAddressBookNotFoundException |
||
440 | * @throws ContactFormatException |
||
441 | * @throws ContactNotFoundException |
||
442 | * @throws FederatedUserException |
||
443 | * @throws InvalidIdException |
||
444 | * @throws RequestBuilderException |
||
445 | * @throws SingleCircleNotFoundException |
||
446 | */ |
||
447 | public function getAppInitiator( |
||
464 | |||
465 | |||
466 | /** |
||
467 | * some ./occ commands allows to add an Initiator, or force the PoV from the local circles' owner |
||
468 | * |
||
469 | * TODO: manage non-user type ? |
||
470 | * |
||
471 | * @param string $userId |
||
472 | * @param int $userType |
||
473 | * @param string $circleId |
||
474 | * @param bool $bypass |
||
475 | * |
||
476 | * @throws CircleNotFoundException |
||
477 | * @throws FederatedItemException |
||
478 | * @throws FederatedUserException |
||
479 | * @throws FederatedUserNotFoundException |
||
480 | * @throws InvalidIdException |
||
481 | * @throws MemberNotFoundException |
||
482 | * @throws OwnerNotFoundException |
||
483 | * @throws RemoteInstanceException |
||
484 | * @throws RemoteNotFoundException |
||
485 | * @throws RemoteResourceNotFoundException |
||
486 | * @throws RequestBuilderException |
||
487 | * @throws SingleCircleNotFoundException |
||
488 | * @throws UnknownRemoteException |
||
489 | * @throws UserTypeNotFoundException |
||
490 | */ |
||
491 | public function commandLineInitiator( |
||
520 | |||
521 | |||
522 | /** |
||
523 | * Works like getFederatedUser, but returns a member. |
||
524 | * Allow to specify a level: handle@instance,level |
||
525 | * |
||
526 | * Used for filters when searching for Circles |
||
527 | * TODO: Used outside of ./occ circles:manage:list ? |
||
528 | * |
||
529 | * @param string $userId |
||
530 | * @param int $level |
||
531 | * |
||
532 | * @return Member |
||
533 | * @throws CircleNotFoundException |
||
534 | * @throws FederatedItemException |
||
535 | * @throws FederatedUserException |
||
536 | * @throws FederatedUserNotFoundException |
||
537 | * @throws InvalidIdException |
||
538 | * @throws MemberNotFoundException |
||
539 | * @throws OwnerNotFoundException |
||
540 | * @throws RemoteInstanceException |
||
541 | * @throws RemoteNotFoundException |
||
542 | * @throws RemoteResourceNotFoundException |
||
543 | * @throws SingleCircleNotFoundException |
||
544 | * @throws UnknownRemoteException |
||
545 | * @throws UserTypeNotFoundException |
||
546 | */ |
||
547 | public function getFederatedMember(string $userId, int $level = Member::LEVEL_MEMBER): Member { |
||
560 | |||
561 | |||
562 | /** |
||
563 | * get a valid FederatedUser, based on the federatedId (userId@instance) and its type. |
||
564 | * If instance is local, get the local valid FederatedUser |
||
565 | * If instance is not local, get the remote valid FederatedUser |
||
566 | * |
||
567 | * @param string $federatedId |
||
568 | * @param int $type |
||
569 | * |
||
570 | * @return FederatedUser |
||
571 | * @throws CircleNotFoundException |
||
572 | * @throws FederatedItemException |
||
573 | * @throws FederatedUserException |
||
574 | * @throws FederatedUserNotFoundException |
||
575 | * @throws InvalidIdException |
||
576 | * @throws MemberNotFoundException |
||
577 | * @throws OwnerNotFoundException |
||
578 | * @throws RemoteInstanceException |
||
579 | * @throws RemoteNotFoundException |
||
580 | * @throws RemoteResourceNotFoundException |
||
581 | * @throws SingleCircleNotFoundException |
||
582 | * @throws UnknownRemoteException |
||
583 | * @throws UserTypeNotFoundException |
||
584 | * @throws RequestBuilderException |
||
585 | */ |
||
586 | public function getFederatedUser(string $federatedId, int $type = Member::TYPE_SINGLE): FederatedUser { |
||
613 | |||
614 | |||
615 | /** |
||
616 | * Generate a FederatedUser based on local data. |
||
617 | * WARNING: There is no confirmation that the returned FederatedUser exists or is valid at this point. |
||
618 | * Use getFederatedUser() instead if a valid and confirmed FederatedUser is needed. |
||
619 | * |
||
620 | * if $federatedId is a known SingleId, will returns data from the local database. |
||
621 | * if $federatedId is a local username, will returns data from the local database. |
||
622 | * Otherwise, the FederatedUser will not contains a SingleId. |
||
623 | * |
||
624 | * @param string $federatedId |
||
625 | * @param int $type |
||
626 | * |
||
627 | * @return FederatedUser |
||
628 | */ |
||
629 | public function generateFederatedUser(string $federatedId, int $type = 0): FederatedUser { |
||
641 | |||
642 | |||
643 | /** |
||
644 | * @param FederatedUser $federatedUser |
||
645 | */ |
||
646 | public function deleteFederatedUser(FederatedUser $federatedUser): void { |
||
651 | |||
652 | |||
653 | /** |
||
654 | * @param string $singleId |
||
655 | * @param string $instance |
||
656 | * |
||
657 | * @return FederatedUser |
||
658 | * @throws CircleNotFoundException |
||
659 | * @throws FederatedItemException |
||
660 | * @throws FederatedUserException |
||
661 | * @throws FederatedUserNotFoundException |
||
662 | * @throws MemberNotFoundException |
||
663 | * @throws OwnerNotFoundException |
||
664 | * @throws RemoteInstanceException |
||
665 | * @throws RemoteNotFoundException |
||
666 | * @throws RemoteResourceNotFoundException |
||
667 | * @throws UnknownRemoteException |
||
668 | * @throws RequestBuilderException |
||
669 | */ |
||
670 | public function getFederatedUser_SingleId(string $singleId, string $instance): FederatedUser { |
||
685 | |||
686 | |||
687 | /** |
||
688 | * @param string $userId |
||
689 | * @param string $instance |
||
690 | * |
||
691 | * @return FederatedUser |
||
692 | * @throws FederatedUserException |
||
693 | * @throws FederatedUserNotFoundException |
||
694 | * @throws InvalidIdException |
||
695 | * @throws RemoteInstanceException |
||
696 | * @throws RemoteNotFoundException |
||
697 | * @throws RemoteResourceNotFoundException |
||
698 | * @throws SingleCircleNotFoundException |
||
699 | * @throws UnknownRemoteException |
||
700 | * @throws FederatedItemException |
||
701 | * @throws RequestBuilderException |
||
702 | */ |
||
703 | private function getFederatedUser_User(string $userId, string $instance): FederatedUser { |
||
718 | |||
719 | |||
720 | /** |
||
721 | * @param string $groupName |
||
722 | * @param string $instance |
||
723 | * |
||
724 | * @return FederatedUser |
||
725 | * @throws FederatedEventException |
||
726 | * @throws FederatedItemException |
||
727 | * @throws FederatedUserException |
||
728 | * @throws GroupNotFoundException |
||
729 | * @throws InitiatorNotConfirmedException |
||
730 | * @throws InvalidIdException |
||
731 | * @throws OwnerNotFoundException |
||
732 | * @throws RemoteInstanceException |
||
733 | * @throws RemoteNotFoundException |
||
734 | * @throws RemoteResourceNotFoundException |
||
735 | * @throws SingleCircleNotFoundException |
||
736 | * @throws UnknownRemoteException |
||
737 | * @throws RequestBuilderException |
||
738 | */ |
||
739 | public function getFederatedUser_Group(string $groupName, string $instance): FederatedUser { |
||
749 | |||
750 | |||
751 | /** |
||
752 | * @param string $mailAddress |
||
753 | * |
||
754 | * @return FederatedUser |
||
755 | * @throws ContactAddressBookNotFoundException |
||
756 | * @throws ContactFormatException |
||
757 | * @throws ContactNotFoundException |
||
758 | * @throws FederatedUserException |
||
759 | * @throws InvalidIdException |
||
760 | * @throws RequestBuilderException |
||
761 | * @throws SingleCircleNotFoundException |
||
762 | */ |
||
763 | public function getFederatedUser_Mail(string $mailAddress): FederatedUser { |
||
770 | |||
771 | |||
772 | /** |
||
773 | * @param string $contactPath |
||
774 | * |
||
775 | * @return FederatedUser |
||
776 | * @throws ContactAddressBookNotFoundException |
||
777 | * @throws ContactFormatException |
||
778 | * @throws ContactNotFoundException |
||
779 | * @throws FederatedUserException |
||
780 | * @throws InvalidIdException |
||
781 | * @throws RequestBuilderException |
||
782 | * @throws SingleCircleNotFoundException |
||
783 | */ |
||
784 | public function getFederatedUser_Contact(string $contactPath): FederatedUser { |
||
797 | |||
798 | |||
799 | /** |
||
800 | * extract userID and instance from a federatedId |
||
801 | * |
||
802 | * @param string $federatedId |
||
803 | * |
||
804 | * @return array |
||
805 | */ |
||
806 | public function extractIdAndInstance(string $federatedId): array { |
||
817 | |||
818 | |||
819 | /** |
||
820 | * @param FederatedUser $federatedUser |
||
821 | * @param bool $generate |
||
822 | * |
||
823 | * @throws ContactAddressBookNotFoundException |
||
824 | * @throws ContactFormatException |
||
825 | * @throws ContactNotFoundException |
||
826 | * @throws FederatedUserException |
||
827 | * @throws InvalidIdException |
||
828 | * @throws RequestBuilderException |
||
829 | * @throws SingleCircleNotFoundException |
||
830 | */ |
||
831 | private function fillSingleCircleId(FederatedUser $federatedUser, bool $generate = true): void { |
||
841 | |||
842 | |||
843 | /** |
||
844 | * get the Single Circle from a local user |
||
845 | * |
||
846 | * @param FederatedUser $federatedUser |
||
847 | * @param bool $generate |
||
848 | * |
||
849 | * @return Circle |
||
850 | * @throws ContactAddressBookNotFoundException |
||
851 | * @throws ContactFormatException |
||
852 | * @throws ContactNotFoundException |
||
853 | * @throws FederatedUserException |
||
854 | * @throws InvalidIdException |
||
855 | * @throws RequestBuilderException |
||
856 | * @throws SingleCircleNotFoundException |
||
857 | */ |
||
858 | private function getSingleCircle(FederatedUser $federatedUser, bool $generate = true): Circle { |
||
920 | |||
921 | |||
922 | /** |
||
923 | * Confirm that all field of a FederatedUser are filled. |
||
924 | * |
||
925 | * @param FederatedUser $federatedUser |
||
926 | * |
||
927 | * @throws FederatedUserException |
||
928 | */ |
||
929 | private function confirmFederatedUser(FederatedUser $federatedUser): void { |
||
938 | |||
939 | /** |
||
940 | * Confirm that the singleId of a FederatedUser is unique and not used to any other member of the |
||
941 | * database. |
||
942 | * |
||
943 | * @param FederatedUser $federatedUser |
||
944 | * |
||
945 | * @throws FederatedUserException |
||
946 | * @throws RequestBuilderException |
||
947 | * @deprecated: use confirmSingleIdUniqueness() |
||
948 | */ |
||
949 | public function confirmLocalSingleId(IFederatedUser $federatedUser): void { |
||
962 | |||
963 | |||
964 | /** |
||
965 | * // TODO: implement this check in a maintenance background job |
||
966 | * |
||
967 | * @param IFederatedUser $federatedUser |
||
968 | * |
||
969 | * @throws FederatedUserException |
||
970 | * @throws RemoteNotFoundException |
||
971 | * @throws RequestBuilderException |
||
972 | * @throws UnknownRemoteException |
||
973 | */ |
||
974 | public function confirmSingleIdUniqueness(IFederatedUser $federatedUser): void { |
||
1012 | |||
1013 | |||
1014 | /** |
||
1015 | * @param IFederatedUser $federatedUser |
||
1016 | * @param Member $knownMember |
||
1017 | * @param int $conflict |
||
1018 | * |
||
1019 | * @throws FederatedUserException |
||
1020 | */ |
||
1021 | private function markConflict(IFederatedUser $federatedUser, Member $knownMember, int $conflict): void { |
||
1054 | |||
1055 | /** |
||
1056 | * @param string $groupId |
||
1057 | * |
||
1058 | * @return Circle |
||
1059 | * @throws GroupNotFoundException |
||
1060 | * @throws FederatedEventException |
||
1061 | * @throws FederatedItemException |
||
1062 | * @throws FederatedUserException |
||
1063 | * @throws InitiatorNotConfirmedException |
||
1064 | * @throws InvalidIdException |
||
1065 | * @throws OwnerNotFoundException |
||
1066 | * @throws RemoteInstanceException |
||
1067 | * @throws RemoteNotFoundException |
||
1068 | * @throws RemoteResourceNotFoundException |
||
1069 | * @throws SingleCircleNotFoundException |
||
1070 | * @throws UnknownRemoteException |
||
1071 | * @throws RequestBuilderException |
||
1072 | */ |
||
1073 | public function getGroupCircle(string $groupId): Circle { |
||
1110 | |||
1111 | } |
||
1112 | |||
1113 |
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: