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 |
||
| 78 | class FederatedEventService extends NC21Signature { |
||
| 79 | |||
| 80 | |||
| 81 | use TNC21Request; |
||
| 82 | use TStringTools; |
||
| 83 | |||
| 84 | /** @var IL10N */ |
||
| 85 | private $l10n; |
||
| 86 | |||
| 87 | /** @var RemoteWrapperRequest */ |
||
| 88 | private $remoteWrapperRequest; |
||
| 89 | |||
| 90 | /** @var RemoteRequest */ |
||
| 91 | private $remoteRequest; |
||
| 92 | |||
| 93 | /** @var RemoteUpstreamService */ |
||
| 94 | private $remoteUpstreamService; |
||
| 95 | |||
| 96 | /** @var ConfigService */ |
||
| 97 | private $configService; |
||
| 98 | |||
| 99 | /** @var MiscService */ |
||
| 100 | private $miscService; |
||
| 101 | |||
| 102 | |||
| 103 | /** |
||
| 104 | * FederatedEventService constructor. |
||
| 105 | * |
||
| 106 | * @param IL10N $l10n |
||
| 107 | * @param RemoteWrapperRequest $remoteWrapperRequest |
||
| 108 | * @param RemoteRequest $remoteRequest |
||
| 109 | * @param RemoteUpstreamService $remoteUpstreamService |
||
| 110 | * @param ConfigService $configService |
||
| 111 | */ |
||
| 112 | public function __construct( |
||
| 122 | |||
| 123 | |||
| 124 | /** |
||
| 125 | * Called when creating a new Event. |
||
| 126 | * This method will manage the event locally and upstream the payload if needed. |
||
| 127 | * |
||
| 128 | * @param FederatedEvent $event |
||
| 129 | * |
||
| 130 | * @throws InitiatorNotConfirmedException |
||
| 131 | * @throws OwnerNotFoundException |
||
| 132 | * @throws FederatedEventException |
||
| 133 | * @throws RequestNetworkException |
||
| 134 | * @throws RemoteNotFoundException |
||
| 135 | * @throws RemoteResourceNotFoundException |
||
| 136 | * @throws UnknownRemoteException |
||
| 137 | * @throws SignatoryException |
||
| 138 | * @throws FederatedItemException |
||
| 139 | * @throws FederatedEventDSyncException |
||
| 140 | */ |
||
| 141 | public function newEvent(FederatedEvent $event): void { |
||
| 174 | |||
| 175 | |||
| 176 | /** |
||
| 177 | * This confirmation is optional, method is just here to avoid going too far away on the process |
||
| 178 | * |
||
| 179 | * @param FederatedEvent $event |
||
| 180 | * @param bool $local |
||
| 181 | * |
||
| 182 | * @throws InitiatorNotConfirmedException |
||
| 183 | */ |
||
| 184 | public function confirmInitiator(FederatedEvent $event, bool $local = false): void { |
||
| 208 | |||
| 209 | |||
| 210 | |||
| 211 | // /** |
||
| 212 | // * We check that the event can be managed/checked locally or if the owner of the circle belongs to |
||
| 213 | // * an other instance of Nextcloud |
||
| 214 | // * |
||
| 215 | // * @param RemoteEvent $event |
||
| 216 | // * |
||
| 217 | // * @return bool |
||
| 218 | // * @throws CircleNotFoundException |
||
| 219 | // * @throws OwnerNotFoundException |
||
| 220 | // */ |
||
| 221 | // public function isLocalEvent(RemoteEvent $event): bool { |
||
| 222 | //// if ($event->isLocal()) { |
||
| 223 | //// return true; |
||
| 224 | //// } |
||
| 225 | // |
||
| 226 | // $circle = $event->getCircle(); |
||
| 227 | // |
||
| 228 | //// if (!$circle->hasOwner()) { |
||
| 229 | // return ($this->configService->isLocalInstance($circle->getInstance())); |
||
| 230 | //// } |
||
| 231 | // |
||
| 232 | //// if ($event->isVerifiedCircle()) { |
||
| 233 | //// $localCircle = $event->getCircle(); |
||
| 234 | //// } else { |
||
| 235 | //// $localCircle = $this->circleRequest->getCircle($circle->getId()); |
||
| 236 | //// } |
||
| 237 | //// |
||
| 238 | //// $owner = $localCircle->getOwner(); |
||
| 239 | //// if ($owner->getInstance() === '' |
||
| 240 | //// || $this->configService->isLocalInstance($owner->getInstance())) { |
||
| 241 | //// return true; |
||
| 242 | //// } |
||
| 243 | //// |
||
| 244 | //// return false; |
||
| 245 | // } |
||
| 246 | |||
| 247 | |||
| 248 | /** |
||
| 249 | * @param FederatedEvent $event |
||
| 250 | * @param bool $checkLocalOnly |
||
| 251 | * |
||
| 252 | * @return IFederatedItem |
||
| 253 | * @throws FederatedEventException |
||
| 254 | */ |
||
| 255 | public function getFederatedItem(FederatedEvent $event, bool $checkLocalOnly = true): IFederatedItem { |
||
| 278 | |||
| 279 | |||
| 280 | /** |
||
| 281 | * Some event might need to bypass some checks |
||
| 282 | * |
||
| 283 | * @param FederatedEvent $event |
||
| 284 | * @param IFederatedItem $gs |
||
| 285 | */ |
||
| 286 | private function setFederatedEventBypass(FederatedEvent $event, IFederatedItem $gs) { |
||
| 297 | |||
| 298 | /** |
||
| 299 | * Some event might require additional check |
||
| 300 | * |
||
| 301 | * @param FederatedEvent $event |
||
| 302 | * @param IFederatedItem $item |
||
| 303 | * @param bool $checkLocalOnly |
||
| 304 | * |
||
| 305 | * @throws FederatedEventException |
||
| 306 | */ |
||
| 307 | private function confirmRequiredCondition( |
||
| 333 | |||
| 334 | |||
| 335 | /** |
||
| 336 | * @param FederatedEvent $event |
||
| 337 | * @param IFederatedItem $item |
||
| 338 | */ |
||
| 339 | private function configureEvent(FederatedEvent $event, IFederatedItem $item) { |
||
| 344 | |||
| 345 | |||
| 346 | /** |
||
| 347 | * async the process, generate a local request that will be closed. |
||
| 348 | * |
||
| 349 | * @param FederatedEvent $event |
||
| 350 | * @param array $filter |
||
| 351 | */ |
||
| 352 | public function initBroadcast(FederatedEvent $event, array $filter = []): void { |
||
| 382 | |||
| 383 | |||
| 384 | /** |
||
| 385 | * @param bool $all |
||
| 386 | * @param Circle|null $circle |
||
| 387 | * |
||
| 388 | * @return array |
||
| 389 | */ |
||
| 390 | public function getInstances(bool $all = false, ?Circle $circle = null): array { |
||
| 409 | |||
| 410 | |||
| 411 | /** |
||
| 412 | * @param array $current |
||
| 413 | */ |
||
| 414 | private function updateGlobalScaleInstances(array $current): void { |
||
| 417 | |||
| 418 | /** |
||
| 419 | * @return array |
||
| 420 | */ |
||
| 421 | private function getRemoteInstances(): array { |
||
| 424 | |||
| 425 | |||
| 426 | /** |
||
| 427 | * should be used to manage results from events, like sending mails on user creation |
||
| 428 | * |
||
| 429 | * @param string $token |
||
| 430 | */ |
||
| 431 | public function manageResults(string $token): void { |
||
| 458 | |||
| 459 | } |
||
| 460 | |||
| 461 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.