Complex classes like FederatedEventService 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 FederatedEventService, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 75 | class FederatedEventService extends NC21Signature { |
||
| 76 | |||
| 77 | |||
| 78 | use TNC21Request; |
||
| 79 | use TStringTools; |
||
| 80 | |||
| 81 | |||
| 82 | /** @var RemoteWrapperRequest */ |
||
| 83 | private $remoteWrapperRequest; |
||
| 84 | |||
| 85 | /** @var RemoteRequest */ |
||
| 86 | private $remoteRequest; |
||
| 87 | |||
| 88 | /** @var RemoteUpstreamService */ |
||
| 89 | private $remoteUpstreamService; |
||
| 90 | |||
| 91 | /** @var ConfigService */ |
||
| 92 | private $configService; |
||
| 93 | |||
| 94 | /** @var MiscService */ |
||
| 95 | private $miscService; |
||
| 96 | |||
| 97 | |||
| 98 | /** |
||
| 99 | * FederatedEventService constructor. |
||
| 100 | * |
||
| 101 | * @param RemoteWrapperRequest $remoteWrapperRequest |
||
| 102 | * @param RemoteRequest $remoteRequest |
||
| 103 | * @param RemoteUpstreamService $remoteUpstreamService |
||
| 104 | * @param ConfigService $configService |
||
| 105 | */ |
||
| 106 | public function __construct( |
||
| 115 | |||
| 116 | |||
| 117 | /** |
||
| 118 | * Called when creating a new Event. |
||
| 119 | * This method will manage the event locally and upstream the payload if needed. |
||
| 120 | * |
||
| 121 | * @param FederatedEvent $event |
||
| 122 | * |
||
| 123 | * @throws InitiatorNotConfirmedException |
||
| 124 | * @throws OwnerNotFoundException |
||
| 125 | * @throws FederatedEventException |
||
| 126 | * @throws RequestNetworkException |
||
| 127 | * @throws RemoteNotFoundException |
||
| 128 | * @throws RemoteResourceNotFoundException |
||
| 129 | * @throws UnknownRemoteException |
||
| 130 | * @throws SignatoryException |
||
| 131 | * @throws FederatedItemException |
||
| 132 | */ |
||
| 133 | public function newEvent(FederatedEvent $event): void { |
||
| 155 | |||
| 156 | |||
| 157 | /** |
||
| 158 | * This confirmation is optional, method is just here to avoid going too far away on the process |
||
| 159 | * |
||
| 160 | * @param FederatedEvent $event |
||
| 161 | * @param bool $local |
||
| 162 | * |
||
| 163 | * @throws InitiatorNotConfirmedException |
||
| 164 | */ |
||
| 165 | public function confirmInitiator(FederatedEvent $event, bool $local = false): void { |
||
| 189 | |||
| 190 | |||
| 191 | |||
| 192 | // /** |
||
| 193 | // * We check that the event can be managed/checked locally or if the owner of the circle belongs to |
||
| 194 | // * an other instance of Nextcloud |
||
| 195 | // * |
||
| 196 | // * @param RemoteEvent $event |
||
| 197 | // * |
||
| 198 | // * @return bool |
||
| 199 | // * @throws CircleNotFoundException |
||
| 200 | // * @throws OwnerNotFoundException |
||
| 201 | // */ |
||
| 202 | // public function isLocalEvent(RemoteEvent $event): bool { |
||
| 203 | //// if ($event->isLocal()) { |
||
| 204 | //// return true; |
||
| 205 | //// } |
||
| 206 | // |
||
| 207 | // $circle = $event->getCircle(); |
||
| 208 | // |
||
| 209 | //// if (!$circle->hasOwner()) { |
||
| 210 | // return ($this->configService->isLocalInstance($circle->getInstance())); |
||
| 211 | //// } |
||
| 212 | // |
||
| 213 | //// if ($event->isVerifiedCircle()) { |
||
| 214 | //// $localCircle = $event->getCircle(); |
||
| 215 | //// } else { |
||
| 216 | //// $localCircle = $this->circleRequest->getCircle($circle->getId()); |
||
| 217 | //// } |
||
| 218 | //// |
||
| 219 | //// $owner = $localCircle->getOwner(); |
||
| 220 | //// if ($owner->getInstance() === '' |
||
| 221 | //// || $this->configService->isLocalInstance($owner->getInstance())) { |
||
| 222 | //// return true; |
||
| 223 | //// } |
||
| 224 | //// |
||
| 225 | //// return false; |
||
| 226 | // } |
||
| 227 | |||
| 228 | |||
| 229 | /** |
||
| 230 | * @param FederatedEvent $event |
||
| 231 | * @param bool $local |
||
| 232 | * |
||
| 233 | * @return IFederatedItem |
||
| 234 | * @throws FederatedEventException |
||
| 235 | */ |
||
| 236 | public function getFederatedItem(FederatedEvent $event, bool $local = false): IFederatedItem { |
||
| 258 | |||
| 259 | |||
| 260 | /** |
||
| 261 | * Some event might need to bypass some checks |
||
| 262 | * |
||
| 263 | * @param FederatedEvent $event |
||
| 264 | * @param IFederatedItem $gs |
||
| 265 | */ |
||
| 266 | private function setFederatedEventBypass(FederatedEvent $event, IFederatedItem $gs) { |
||
| 277 | |||
| 278 | /** |
||
| 279 | * Some event might require additional check |
||
| 280 | * |
||
| 281 | * @param FederatedEvent $event |
||
| 282 | * @param IFederatedItem $item |
||
| 283 | * @param bool $local |
||
| 284 | * |
||
| 285 | * @throws FederatedEventException |
||
| 286 | */ |
||
| 287 | private function confirmRequiredCondition( |
||
| 313 | |||
| 314 | |||
| 315 | /** |
||
| 316 | * async the process, generate a local request that will be closed. |
||
| 317 | * |
||
| 318 | * @param FederatedEvent $event |
||
| 319 | */ |
||
| 320 | public function initBroadcast(FederatedEvent $event): void { |
||
| 350 | |||
| 351 | |||
| 352 | /** |
||
| 353 | * @param bool $all |
||
| 354 | * @param Circle|null $circle |
||
| 355 | * |
||
| 356 | * @return array |
||
| 357 | */ |
||
| 358 | public function getInstances(bool $all = false, ?Circle $circle = null): array { |
||
| 377 | |||
| 378 | |||
| 379 | /** |
||
| 380 | * @param array $current |
||
| 381 | */ |
||
| 382 | private function updateGlobalScaleInstances(array $current): void { |
||
| 385 | |||
| 386 | /** |
||
| 387 | * @return array |
||
| 388 | */ |
||
| 389 | private function getRemoteInstances(): array { |
||
| 392 | |||
| 393 | |||
| 394 | /** |
||
| 395 | * should be used to manage results from events, like sending mails on user creation |
||
| 396 | * |
||
| 397 | * @param string $token |
||
| 398 | */ |
||
| 399 | public function manageResults(string $token): void { |
||
| 426 | |||
| 427 | } |
||
| 428 | |||
| 429 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.