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 |
||
| 76 | class FederatedEventService extends NC21Signature { |
||
| 77 | |||
| 78 | |||
| 79 | use TNC21Request; |
||
| 80 | use TStringTools; |
||
| 81 | |||
| 82 | |||
| 83 | /** @var RemoteWrapperRequest */ |
||
| 84 | private $remoteWrapperRequest; |
||
| 85 | |||
| 86 | /** @var RemoteRequest */ |
||
| 87 | private $remoteRequest; |
||
| 88 | |||
| 89 | /** @var RemoteUpstreamService */ |
||
| 90 | private $remoteUpstreamService; |
||
| 91 | |||
| 92 | /** @var ConfigService */ |
||
| 93 | private $configService; |
||
| 94 | |||
| 95 | /** @var MiscService */ |
||
| 96 | private $miscService; |
||
| 97 | |||
| 98 | |||
| 99 | /** |
||
| 100 | * FederatedEventService constructor. |
||
| 101 | * |
||
| 102 | * @param RemoteWrapperRequest $remoteWrapperRequest |
||
| 103 | * @param RemoteRequest $remoteRequest |
||
| 104 | * @param RemoteUpstreamService $remoteUpstreamService |
||
| 105 | * @param ConfigService $configService |
||
| 106 | */ |
||
| 107 | public function __construct( |
||
| 116 | |||
| 117 | |||
| 118 | /** |
||
| 119 | * Called when creating a new Event. |
||
| 120 | * This method will manage the event locally and upstream the payload if needed. |
||
| 121 | * |
||
| 122 | * @param FederatedEvent $event |
||
| 123 | * |
||
| 124 | * @throws InitiatorNotConfirmedException |
||
| 125 | * @throws OwnerNotFoundException |
||
| 126 | * @throws FederatedEventException |
||
| 127 | * @throws RequestNetworkException |
||
| 128 | * @throws RemoteNotFoundException |
||
| 129 | * @throws RemoteResourceNotFoundException |
||
| 130 | * @throws UnknownRemoteException |
||
| 131 | * @throws SignatoryException |
||
| 132 | * @throws FederatedItemException |
||
| 133 | */ |
||
| 134 | public function newEvent(FederatedEvent $event): void { |
||
| 159 | |||
| 160 | |||
| 161 | /** |
||
| 162 | * This confirmation is optional, method is just here to avoid going too far away on the process |
||
| 163 | * |
||
| 164 | * @param FederatedEvent $event |
||
| 165 | * @param bool $local |
||
| 166 | * |
||
| 167 | * @throws InitiatorNotConfirmedException |
||
| 168 | */ |
||
| 169 | public function confirmInitiator(FederatedEvent $event, bool $local = false): void { |
||
| 193 | |||
| 194 | |||
| 195 | |||
| 196 | // /** |
||
| 197 | // * We check that the event can be managed/checked locally or if the owner of the circle belongs to |
||
| 198 | // * an other instance of Nextcloud |
||
| 199 | // * |
||
| 200 | // * @param RemoteEvent $event |
||
| 201 | // * |
||
| 202 | // * @return bool |
||
| 203 | // * @throws CircleNotFoundException |
||
| 204 | // * @throws OwnerNotFoundException |
||
| 205 | // */ |
||
| 206 | // public function isLocalEvent(RemoteEvent $event): bool { |
||
| 207 | //// if ($event->isLocal()) { |
||
| 208 | //// return true; |
||
| 209 | //// } |
||
| 210 | // |
||
| 211 | // $circle = $event->getCircle(); |
||
| 212 | // |
||
| 213 | //// if (!$circle->hasOwner()) { |
||
| 214 | // return ($this->configService->isLocalInstance($circle->getInstance())); |
||
| 215 | //// } |
||
| 216 | // |
||
| 217 | //// if ($event->isVerifiedCircle()) { |
||
| 218 | //// $localCircle = $event->getCircle(); |
||
| 219 | //// } else { |
||
| 220 | //// $localCircle = $this->circleRequest->getCircle($circle->getId()); |
||
| 221 | //// } |
||
| 222 | //// |
||
| 223 | //// $owner = $localCircle->getOwner(); |
||
| 224 | //// if ($owner->getInstance() === '' |
||
| 225 | //// || $this->configService->isLocalInstance($owner->getInstance())) { |
||
| 226 | //// return true; |
||
| 227 | //// } |
||
| 228 | //// |
||
| 229 | //// return false; |
||
| 230 | // } |
||
| 231 | |||
| 232 | |||
| 233 | /** |
||
| 234 | * @param FederatedEvent $event |
||
| 235 | * @param bool $local |
||
| 236 | * |
||
| 237 | * @return IFederatedItem |
||
| 238 | * @throws FederatedEventException |
||
| 239 | */ |
||
| 240 | public function getFederatedItem(FederatedEvent $event, bool $local = false): IFederatedItem { |
||
| 263 | |||
| 264 | |||
| 265 | /** |
||
| 266 | * Some event might need to bypass some checks |
||
| 267 | * |
||
| 268 | * @param FederatedEvent $event |
||
| 269 | * @param IFederatedItem $gs |
||
| 270 | */ |
||
| 271 | private function setFederatedEventBypass(FederatedEvent $event, IFederatedItem $gs) { |
||
| 282 | |||
| 283 | /** |
||
| 284 | * Some event might require additional check |
||
| 285 | * |
||
| 286 | * @param FederatedEvent $event |
||
| 287 | * @param IFederatedItem $item |
||
| 288 | * @param bool $local |
||
| 289 | * |
||
| 290 | * @throws FederatedEventException |
||
| 291 | */ |
||
| 292 | private function confirmRequiredCondition( |
||
| 318 | |||
| 319 | |||
| 320 | /** |
||
| 321 | * @param FederatedEvent $event |
||
| 322 | * @param IFederatedItem $item |
||
| 323 | */ |
||
| 324 | private function configureEvent(FederatedEvent $event, IFederatedItem $item) { |
||
| 329 | |||
| 330 | |||
| 331 | /** |
||
| 332 | * async the process, generate a local request that will be closed. |
||
| 333 | * |
||
| 334 | * @param FederatedEvent $event |
||
| 335 | * @param array $filter |
||
| 336 | */ |
||
| 337 | public function initBroadcast(FederatedEvent $event, array $filter = []): void { |
||
| 367 | |||
| 368 | |||
| 369 | /** |
||
| 370 | * @param bool $all |
||
| 371 | * @param Circle|null $circle |
||
| 372 | * |
||
| 373 | * @return array |
||
| 374 | */ |
||
| 375 | public function getInstances(bool $all = false, ?Circle $circle = null): array { |
||
| 394 | |||
| 395 | |||
| 396 | /** |
||
| 397 | * @param array $current |
||
| 398 | */ |
||
| 399 | private function updateGlobalScaleInstances(array $current): void { |
||
| 402 | |||
| 403 | /** |
||
| 404 | * @return array |
||
| 405 | */ |
||
| 406 | private function getRemoteInstances(): array { |
||
| 409 | |||
| 410 | |||
| 411 | /** |
||
| 412 | * should be used to manage results from events, like sending mails on user creation |
||
| 413 | * |
||
| 414 | * @param string $token |
||
| 415 | */ |
||
| 416 | public function manageResults(string $token): void { |
||
| 443 | |||
| 444 | } |
||
| 445 | |||
| 446 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.