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 | |||
| 85 | /** @var IL10N */ |
||
| 86 | private $l10n; |
||
| 87 | |||
| 88 | /** @var RemoteWrapperRequest */ |
||
| 89 | private $remoteWrapperRequest; |
||
| 90 | |||
| 91 | /** @var RemoteRequest */ |
||
| 92 | private $remoteRequest; |
||
| 93 | |||
| 94 | /** @var RemoteUpstreamService */ |
||
| 95 | private $remoteUpstreamService; |
||
| 96 | |||
| 97 | /** @var ConfigService */ |
||
| 98 | private $configService; |
||
| 99 | |||
| 100 | /** @var MiscService */ |
||
| 101 | private $miscService; |
||
| 102 | |||
| 103 | |||
| 104 | /** |
||
| 105 | * FederatedEventService constructor. |
||
| 106 | * |
||
| 107 | * @param IL10N $l10n |
||
| 108 | * @param RemoteWrapperRequest $remoteWrapperRequest |
||
| 109 | * @param RemoteRequest $remoteRequest |
||
| 110 | * @param RemoteUpstreamService $remoteUpstreamService |
||
| 111 | * @param ConfigService $configService |
||
| 112 | */ |
||
| 113 | public function __construct( |
||
| 114 | IL10N $l10n, RemoteWrapperRequest $remoteWrapperRequest, RemoteRequest $remoteRequest, |
||
| 115 | RemoteUpstreamService $remoteUpstreamService, ConfigService $configService |
||
| 116 | ) { |
||
| 117 | $this->l10n = $l10n; |
||
| 118 | $this->remoteWrapperRequest = $remoteWrapperRequest; |
||
| 119 | $this->remoteRequest = $remoteRequest; |
||
| 120 | $this->remoteUpstreamService = $remoteUpstreamService; |
||
| 121 | $this->configService = $configService; |
||
| 122 | } |
||
| 123 | |||
| 124 | |||
| 125 | /** |
||
| 126 | * Called when creating a new Event. |
||
| 127 | * This method will manage the event locally and upstream the payload if needed. |
||
| 128 | * |
||
| 129 | * @param FederatedEvent $event |
||
| 130 | * |
||
| 131 | * @throws InitiatorNotConfirmedException |
||
| 132 | * @throws OwnerNotFoundException |
||
| 133 | * @throws FederatedEventException |
||
| 134 | * @throws RequestNetworkException |
||
| 135 | * @throws RemoteNotFoundException |
||
| 136 | * @throws RemoteResourceNotFoundException |
||
| 137 | * @throws UnknownRemoteException |
||
| 138 | * @throws SignatoryException |
||
| 139 | * @throws FederatedItemException |
||
| 140 | * @throws FederatedEventDSyncException |
||
| 141 | */ |
||
| 142 | public function newEvent(FederatedEvent $event): void { |
||
| 143 | $event->setSource($this->configService->getLocalInstance()); |
||
| 144 | |||
| 145 | try { |
||
| 146 | $federatedItem = $this->getFederatedItem($event, false); |
||
| 147 | } catch (FederatedEventException $e) { |
||
| 148 | $this->e($e); |
||
| 149 | throw $e; |
||
| 150 | } |
||
| 151 | |||
| 152 | $this->confirmInitiator($event, true); |
||
| 153 | if ($this->configService->isLocalInstance($event->getCircle()->getInstance())) { |
||
| 154 | try { |
||
| 155 | $federatedItem->verify($event); |
||
| 156 | $reading = $event->getReadingOutcome(); |
||
| 157 | $reading->s('translated', $this->l10n->t($reading->g('message'), $reading->gArray('params'))); |
||
| 158 | } catch (FederatedItemException $e) { |
||
| 159 | throw new FederatedItemException($this->l10n->t($e->getMessage(), $e->getParams())); |
||
| 160 | } |
||
| 161 | |||
| 162 | if (!$event->isAsync()) { |
||
| 163 | $federatedItem->manage($event); |
||
| 164 | } |
||
| 165 | |||
| 166 | $this->initBroadcast($event); |
||
| 167 | } else { |
||
| 168 | $this->remoteUpstreamService->confirmEvent($event); |
||
| 169 | if (!$event->isAsync()) { |
||
| 170 | $federatedItem->manage($event); |
||
| 171 | } |
||
| 172 | } |
||
| 173 | |||
| 174 | } |
||
| 175 | |||
| 176 | |||
| 177 | /** |
||
| 178 | * This confirmation is optional, method is just here to avoid going too far away on the process |
||
| 179 | * |
||
| 180 | * @param FederatedEvent $event |
||
| 181 | * @param bool $local |
||
| 182 | * |
||
| 183 | * @throws InitiatorNotConfirmedException |
||
| 184 | */ |
||
| 185 | public function confirmInitiator(FederatedEvent $event, bool $local = false): void { |
||
| 209 | |||
| 210 | |||
| 211 | |||
| 212 | // /** |
||
| 213 | // * We check that the event can be managed/checked locally or if the owner of the circle belongs to |
||
| 214 | // * an other instance of Nextcloud |
||
| 215 | // * |
||
| 216 | // * @param RemoteEvent $event |
||
| 217 | // * |
||
| 218 | // * @return bool |
||
| 219 | // * @throws CircleNotFoundException |
||
| 220 | // * @throws OwnerNotFoundException |
||
| 221 | // */ |
||
| 222 | // public function isLocalEvent(RemoteEvent $event): bool { |
||
| 223 | //// if ($event->isLocal()) { |
||
| 224 | //// return true; |
||
| 225 | //// } |
||
| 226 | // |
||
| 227 | // $circle = $event->getCircle(); |
||
| 228 | // |
||
| 229 | //// if (!$circle->hasOwner()) { |
||
| 230 | // return ($this->configService->isLocalInstance($circle->getInstance())); |
||
| 231 | //// } |
||
| 232 | // |
||
| 233 | //// if ($event->isVerifiedCircle()) { |
||
| 234 | //// $localCircle = $event->getCircle(); |
||
| 235 | //// } else { |
||
| 236 | //// $localCircle = $this->circleRequest->getCircle($circle->getId()); |
||
| 237 | //// } |
||
| 238 | //// |
||
| 239 | //// $owner = $localCircle->getOwner(); |
||
| 240 | //// if ($owner->getInstance() === '' |
||
| 241 | //// || $this->configService->isLocalInstance($owner->getInstance())) { |
||
| 242 | //// return true; |
||
| 243 | //// } |
||
| 244 | //// |
||
| 245 | //// return false; |
||
| 246 | // } |
||
| 247 | |||
| 248 | |||
| 249 | /** |
||
| 250 | * @param FederatedEvent $event |
||
| 251 | * @param bool $checkLocalOnly |
||
| 252 | * |
||
| 253 | * @return IFederatedItem |
||
| 254 | * @throws FederatedEventException |
||
| 255 | */ |
||
| 256 | public function getFederatedItem(FederatedEvent $event, bool $checkLocalOnly = true): IFederatedItem { |
||
| 279 | |||
| 280 | |||
| 281 | /** |
||
| 282 | * Some event might need to bypass some checks |
||
| 283 | * |
||
| 284 | * @param FederatedEvent $event |
||
| 285 | * @param IFederatedItem $gs |
||
| 286 | */ |
||
| 287 | private function setFederatedEventBypass(FederatedEvent $event, IFederatedItem $gs) { |
||
| 298 | |||
| 299 | /** |
||
| 300 | * Some event might require additional check |
||
| 301 | * |
||
| 302 | * @param FederatedEvent $event |
||
| 303 | * @param IFederatedItem $item |
||
| 304 | * @param bool $checkLocalOnly |
||
| 305 | * |
||
| 306 | * @throws FederatedEventException |
||
| 307 | */ |
||
| 308 | private function confirmRequiredCondition( |
||
| 334 | |||
| 335 | |||
| 336 | /** |
||
| 337 | * @param FederatedEvent $event |
||
| 338 | * @param IFederatedItem $item |
||
| 339 | */ |
||
| 340 | private function configureEvent(FederatedEvent $event, IFederatedItem $item) { |
||
| 345 | |||
| 346 | |||
| 347 | /** |
||
| 348 | * async the process, generate a local request that will be closed. |
||
| 349 | * |
||
| 350 | * @param FederatedEvent $event |
||
| 351 | * @param array $filter |
||
| 352 | */ |
||
| 353 | public function initBroadcast(FederatedEvent $event, array $filter = []): void { |
||
| 383 | |||
| 384 | |||
| 385 | /** |
||
| 386 | * @param bool $all |
||
| 387 | * @param Circle|null $circle |
||
| 388 | * |
||
| 389 | * @return array |
||
| 390 | */ |
||
| 391 | public function getInstances(bool $all = false, ?Circle $circle = null): array { |
||
| 410 | |||
| 411 | |||
| 412 | /** |
||
| 413 | * @param array $current |
||
| 414 | */ |
||
| 415 | private function updateGlobalScaleInstances(array $current): void { |
||
| 418 | |||
| 419 | /** |
||
| 420 | * @return array |
||
| 421 | */ |
||
| 422 | private function getRemoteInstances(): array { |
||
| 425 | |||
| 426 | |||
| 427 | /** |
||
| 428 | * should be used to manage results from events, like sending mails on user creation |
||
| 429 | * |
||
| 430 | * @param string $token |
||
| 431 | */ |
||
| 432 | public function manageResults(string $token): void { |
||
| 459 | |||
| 460 | } |
||
| 461 | |||
| 462 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.