Complex classes like GSUpstreamService 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 GSUpstreamService, and based on these observations, apply Extract Interface, too.
| 1 | <?php declare(strict_types=1); |
||
| 63 | class GSUpstreamService { |
||
| 64 | |||
| 65 | |||
| 66 | use TNC19Request; |
||
| 67 | use TArrayTools; |
||
| 68 | |||
| 69 | |||
| 70 | /** @var string */ |
||
| 71 | private $userId = ''; |
||
| 72 | |||
| 73 | /** @var IURLGenerator */ |
||
| 74 | private $urlGenerator; |
||
| 75 | |||
| 76 | /** @var GSEventsRequest */ |
||
| 77 | private $gsEventsRequest; |
||
| 78 | |||
| 79 | /** @var CirclesRequest */ |
||
| 80 | private $circlesRequest; |
||
| 81 | |||
| 82 | /** @var MembersRequest */ |
||
| 83 | private $membersRequest; |
||
| 84 | |||
| 85 | /** @var GlobalScaleService */ |
||
| 86 | private $globalScaleService; |
||
| 87 | |||
| 88 | /** @var ConfigService */ |
||
| 89 | private $configService; |
||
| 90 | |||
| 91 | /** @var MiscService */ |
||
| 92 | private $miscService; |
||
| 93 | |||
| 94 | |||
| 95 | /** |
||
| 96 | * GSUpstreamService constructor. |
||
| 97 | * |
||
| 98 | * @param $userId |
||
| 99 | * @param IURLGenerator $urlGenerator |
||
| 100 | * @param GSEventsRequest $gsEventsRequest |
||
| 101 | * @param CirclesRequest $circlesRequest |
||
| 102 | * @param MembersRequest $membersRequest |
||
| 103 | * @param GlobalScaleService $globalScaleService |
||
| 104 | * @param ConfigService $configService |
||
| 105 | * @param MiscService $miscService |
||
| 106 | */ |
||
| 107 | public function __construct( |
||
| 108 | $userId, |
||
| 109 | IURLGenerator $urlGenerator, |
||
| 110 | GSEventsRequest $gsEventsRequest, |
||
| 111 | CirclesRequest $circlesRequest, |
||
| 112 | MembersRequest $membersRequest, |
||
| 113 | GlobalScaleService $globalScaleService, |
||
| 114 | ConfigService $configService, |
||
| 115 | MiscService $miscService |
||
| 116 | ) { |
||
| 117 | $this->userId = $userId; |
||
| 118 | $this->urlGenerator = $urlGenerator; |
||
| 119 | $this->gsEventsRequest = $gsEventsRequest; |
||
| 120 | $this->circlesRequest = $circlesRequest; |
||
| 121 | $this->membersRequest = $membersRequest; |
||
| 122 | $this->globalScaleService = $globalScaleService; |
||
| 123 | $this->configService = $configService; |
||
| 124 | $this->miscService = $miscService; |
||
| 125 | } |
||
| 126 | |||
| 127 | |||
| 128 | /** |
||
| 129 | * @param GSEvent $event |
||
| 130 | * |
||
| 131 | * @return string |
||
| 132 | * @throws Exception |
||
| 133 | */ |
||
| 134 | public function newEvent(GSEvent $event): string { |
||
| 135 | $event->setSource($this->configService->getLocalCloudId()); |
||
| 136 | |||
| 137 | try { |
||
| 138 | $gs = $this->globalScaleService->getGlobalScaleEvent($event); |
||
| 139 | |||
| 140 | if ($this->isLocalEvent($event)) { |
||
| 141 | $gs->verify($event, true); |
||
| 142 | if (!$event->isAsync()) { |
||
| 143 | $gs->manage($event); |
||
| 144 | } |
||
| 145 | |||
| 146 | return $this->globalScaleService->asyncBroadcast($event); |
||
| 147 | } else { |
||
| 148 | $this->confirmEvent($event); |
||
| 149 | |||
| 150 | return ''; |
||
| 151 | } |
||
| 152 | } catch (Exception $e) { |
||
| 153 | $this->miscService->log( |
||
| 154 | get_class($e) . ' on new event: ' . $e->getMessage() . ' - ' . json_encode($event), 1 |
||
| 155 | ); |
||
| 156 | throw $e; |
||
| 157 | } |
||
| 158 | |||
| 159 | } |
||
| 160 | |||
| 161 | |||
| 162 | /** |
||
| 163 | * @param GSWrapper $wrapper |
||
| 164 | * @param string $protocol |
||
| 165 | */ |
||
| 166 | public function broadcastWrapper(GSWrapper $wrapper, string $protocol): void { |
||
| 167 | $status = GSWrapper::STATUS_FAILED; |
||
| 168 | |||
| 169 | try { |
||
| 170 | $this->broadcastEvent($wrapper->getEvent(), $wrapper->getInstance(), $protocol); |
||
| 171 | $status = GSWrapper::STATUS_DONE; |
||
| 172 | } catch (RequestContentException | RequestNetworkException | RequestResultSizeException | RequestServerException | RequestResultNotJsonException $e) { |
||
|
|
|||
| 173 | } |
||
| 174 | |||
| 175 | if ($wrapper->getSeverity() === GSEvent::SEVERITY_HIGH) { |
||
| 176 | $wrapper->setStatus($status); |
||
| 177 | } else { |
||
| 178 | $wrapper->setStatus(GSWrapper::STATUS_OVER); |
||
| 179 | } |
||
| 180 | |||
| 181 | $this->gsEventsRequest->update($wrapper); |
||
| 182 | } |
||
| 183 | |||
| 184 | |||
| 185 | /** |
||
| 186 | * @param GSEvent $event |
||
| 187 | * @param string $instance |
||
| 188 | * @param string $protocol |
||
| 189 | * |
||
| 190 | * @throws RequestContentException |
||
| 191 | * @throws RequestNetworkException |
||
| 192 | * @throws RequestResultNotJsonException |
||
| 193 | * @throws RequestResultSizeException |
||
| 194 | * @throws RequestServerException |
||
| 195 | */ |
||
| 196 | public function broadcastEvent(GSEvent $event, string $instance, string $protocol = ''): void { |
||
| 197 | $this->signEvent($event); |
||
| 198 | |||
| 199 | $path = $this->urlGenerator->linkToRoute('circles.GlobalScale.broadcast'); |
||
| 200 | $request = new NC19Request($path, Request::TYPE_POST); |
||
| 201 | $this->configService->configureRequest($request); |
||
| 202 | $protocols = ['https', 'http']; |
||
| 203 | if ($protocol !== '') { |
||
| 204 | $protocols = [$protocol]; |
||
| 205 | } |
||
| 206 | |||
| 207 | $request->setProtocols($protocols); |
||
| 208 | $request->setDataSerialize($event); |
||
| 209 | |||
| 210 | $request->setAddress($instance); |
||
| 211 | |||
| 212 | $data = $this->retrieveJson($request); |
||
| 213 | $event->setResult(new SimpleDataStore($this->getArray('result', $data, []))); |
||
| 214 | } |
||
| 215 | |||
| 216 | |||
| 217 | /** |
||
| 218 | * @param GSEvent $event |
||
| 219 | * |
||
| 220 | * @throws RequestContentException |
||
| 221 | * @throws RequestNetworkException |
||
| 222 | * @throws RequestResultSizeException |
||
| 223 | * @throws RequestServerException |
||
| 224 | * @throws RequestResultNotJsonException |
||
| 225 | * @throws GlobalScaleEventException |
||
| 226 | */ |
||
| 227 | public function confirmEvent(GSEvent &$event): void { |
||
| 228 | $this->signEvent($event); |
||
| 229 | |||
| 230 | $circle = $event->getCircle(); |
||
| 231 | $owner = $circle->getOwner(); |
||
| 232 | $path = $this->urlGenerator->linkToRoute('circles.GlobalScale.event'); |
||
| 233 | |||
| 234 | $request = new NC19Request($path, Request::TYPE_POST); |
||
| 235 | $this->configService->configureRequest($request); |
||
| 236 | |||
| 237 | if ($this->get('REQUEST_SCHEME', $_SERVER) !== '') { |
||
| 238 | $request->setProtocols([$_SERVER['REQUEST_SCHEME']]); |
||
| 239 | } else { |
||
| 240 | $request->setProtocols(['https', 'http']); |
||
| 241 | } |
||
| 242 | $request->setAddressFromUrl($owner->getInstance()); |
||
| 243 | $request->setDataSerialize($event); |
||
| 244 | |||
| 245 | $result = $this->retrieveJson($request); |
||
| 246 | $this->miscService->log('result ' . json_encode($result), 0); |
||
| 247 | if ($this->getInt('status', $result) === 0) { |
||
| 248 | throw new GlobalScaleEventException($this->get('error', $result)); |
||
| 249 | } |
||
| 250 | |||
| 251 | $updatedData = $this->getArray('event', $result); |
||
| 252 | $this->miscService->log('updatedEvent: ' . json_encode($updatedData), 0); |
||
| 253 | if (!empty($updatedData)) { |
||
| 254 | $updated = new GSEvent(); |
||
| 255 | try { |
||
| 256 | $updated->import($updatedData); |
||
| 257 | $event = $updated; |
||
| 258 | } catch (Exception $e) { |
||
| 259 | } |
||
| 260 | } |
||
| 261 | } |
||
| 262 | |||
| 263 | |||
| 264 | /** |
||
| 265 | * @param GSEvent $event |
||
| 266 | */ |
||
| 267 | private function signEvent(GSEvent $event) { |
||
| 270 | |||
| 271 | |||
| 272 | /** |
||
| 273 | * We check that the event can be managed/checked locally or if the owner of the circle belongs to |
||
| 274 | * an other instance of Nextcloud |
||
| 275 | * |
||
| 276 | * @param GSEvent $event |
||
| 277 | * |
||
| 278 | * @return bool |
||
| 279 | */ |
||
| 280 | private function isLocalEvent(GSEvent $event): bool { |
||
| 281 | if ($event->isLocal()) { |
||
| 282 | return true; |
||
| 283 | } |
||
| 284 | |||
| 285 | $circle = $event->getCircle(); |
||
| 286 | $owner = $circle->getOwner(); |
||
| 287 | if ($owner->getInstance() === '' |
||
| 288 | || in_array($owner->getInstance(), $this->configService->getTrustedDomains())) { |
||
| 289 | return true; |
||
| 290 | } |
||
| 291 | |||
| 292 | return false; |
||
| 293 | } |
||
| 294 | |||
| 295 | |||
| 296 | /** |
||
| 297 | * @param string $token |
||
| 298 | * |
||
| 299 | * @return GSWrapper[] |
||
| 300 | * @throws JsonException |
||
| 301 | * @throws ModelException |
||
| 302 | */ |
||
| 303 | public function getEventsByToken(string $token): array { |
||
| 304 | return $this->gsEventsRequest->getByToken($token); |
||
| 305 | } |
||
| 306 | |||
| 307 | |||
| 308 | /** |
||
| 309 | * should be used to manage results from events, like sending mails on user creation |
||
| 310 | * |
||
| 311 | * @param string $token |
||
| 312 | */ |
||
| 313 | public function manageResults(string $token): void { |
||
| 340 | |||
| 341 | |||
| 342 | /** |
||
| 343 | * @param array $sync |
||
| 344 | * |
||
| 345 | * @throws GSStatusException |
||
| 346 | */ |
||
| 347 | public function synchronize(array $sync) { |
||
| 348 | $this->configService->getGSStatus(); |
||
| 349 | |||
| 350 | $this->synchronizeCircles($sync); |
||
| 351 | $this->removeDeprecatedCircles(); |
||
| 352 | $this->removeDeprecatedEvents(); |
||
| 353 | } |
||
| 354 | |||
| 355 | |||
| 356 | /** |
||
| 357 | * @param array $circles |
||
| 358 | */ |
||
| 359 | public function synchronizeCircles(array $circles): void { |
||
| 371 | |||
| 372 | |||
| 373 | /** |
||
| 374 | * |
||
| 375 | */ |
||
| 376 | private function removeDeprecatedCircles() { |
||
| 377 | $knownCircles = $this->circlesRequest->forceGetCircles(); |
||
| 378 | foreach ($knownCircles as $knownItem) { |
||
| 390 | |||
| 391 | |||
| 392 | /** |
||
| 393 | * @param Circle $circle |
||
| 394 | * |
||
| 395 | * @throws GSStatusException |
||
| 396 | */ |
||
| 397 | private function checkCircle(Circle $circle): void { |
||
| 405 | |||
| 406 | |||
| 407 | /** |
||
| 408 | * @param Circle $circle |
||
| 409 | * |
||
| 410 | * @return bool |
||
| 411 | * @throws GSStatusException |
||
| 412 | */ |
||
| 413 | public function confirmCircleStatus(Circle $circle): bool { |
||
| 491 | |||
| 492 | /** |
||
| 493 | * @throws GSStatusException |
||
| 494 | */ |
||
| 495 | public function syncEvents() { |
||
| 498 | |||
| 499 | /** |
||
| 500 | * |
||
| 501 | */ |
||
| 502 | private function removeDeprecatedEvents() { |
||
| 506 | |||
| 507 | |||
| 508 | } |
||
| 509 | |||
| 510 |