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); |
||
| 62 | class GSUpstreamService { |
||
| 63 | |||
| 64 | |||
| 65 | use TRequest; |
||
| 66 | use TArrayTools; |
||
| 67 | |||
| 68 | |||
| 69 | /** @var string */ |
||
| 70 | private $userId = ''; |
||
| 71 | |||
| 72 | /** @var IURLGenerator */ |
||
| 73 | private $urlGenerator; |
||
| 74 | |||
| 75 | /** @var GSEventsRequest */ |
||
| 76 | private $gsEventsRequest; |
||
| 77 | |||
| 78 | /** @var CirclesRequest */ |
||
| 79 | private $circlesRequest; |
||
| 80 | |||
| 81 | /** @var MembersRequest */ |
||
| 82 | private $membersRequest; |
||
| 83 | |||
| 84 | /** @var GlobalScaleService */ |
||
| 85 | private $globalScaleService; |
||
| 86 | |||
| 87 | /** @var ConfigService */ |
||
| 88 | private $configService; |
||
| 89 | |||
| 90 | /** @var MiscService */ |
||
| 91 | private $miscService; |
||
| 92 | |||
| 93 | |||
| 94 | /** |
||
| 95 | * GSUpstreamService constructor. |
||
| 96 | * |
||
| 97 | * @param $userId |
||
| 98 | * @param IURLGenerator $urlGenerator |
||
| 99 | * @param GSEventsRequest $gsEventsRequest |
||
| 100 | * @param CirclesRequest $circlesRequest |
||
| 101 | * @param MembersRequest $membersRequest |
||
| 102 | * @param GlobalScaleService $globalScaleService |
||
| 103 | * @param ConfigService $configService |
||
| 104 | * @param MiscService $miscService |
||
| 105 | */ |
||
| 106 | public function __construct( |
||
| 107 | $userId, |
||
| 108 | IURLGenerator $urlGenerator, |
||
| 109 | GSEventsRequest $gsEventsRequest, |
||
| 110 | CirclesRequest $circlesRequest, |
||
| 111 | MembersRequest $membersRequest, |
||
| 112 | GlobalScaleService $globalScaleService, |
||
| 113 | ConfigService $configService, |
||
| 114 | MiscService $miscService |
||
| 115 | ) { |
||
| 116 | $this->userId = $userId; |
||
| 117 | $this->urlGenerator = $urlGenerator; |
||
| 118 | $this->gsEventsRequest = $gsEventsRequest; |
||
| 119 | $this->circlesRequest = $circlesRequest; |
||
| 120 | $this->membersRequest = $membersRequest; |
||
| 121 | $this->globalScaleService = $globalScaleService; |
||
| 122 | $this->configService = $configService; |
||
| 123 | $this->miscService = $miscService; |
||
| 124 | } |
||
| 125 | |||
| 126 | |||
| 127 | /** |
||
| 128 | * @param GSEvent $event |
||
| 129 | * |
||
| 130 | * @throws Exception |
||
| 131 | */ |
||
| 132 | public function newEvent(GSEvent $event) { |
||
| 133 | try { |
||
| 134 | $gs = $this->globalScaleService->getGlobalScaleEvent($event); |
||
| 135 | |||
| 136 | $this->fillEvent($event); |
||
| 137 | if ($this->isLocalEvent($event)) { |
||
| 138 | $gs->verify($event, true); |
||
| 139 | $gs->manage($event); |
||
| 140 | |||
| 141 | $this->globalScaleService->asyncBroadcast($event); |
||
| 142 | } else { |
||
| 143 | $gs->verify($event); // needed ? as we check event on the 'master' of the circle |
||
| 144 | $this->confirmEvent($event); |
||
| 145 | $gs->manage($event); |
||
| 146 | } |
||
| 147 | } catch (Exception $e) { |
||
| 148 | $this->miscService->log( |
||
| 149 | get_class($e) . ' on new event: ' . $e->getMessage() . ' - ' . json_encode($event), 1 |
||
| 150 | ); |
||
| 151 | throw $e; |
||
| 152 | } |
||
| 153 | } |
||
| 154 | |||
| 155 | |||
| 156 | /** |
||
| 157 | * @param GSWrapper $wrapper |
||
| 158 | * @param string $protocol |
||
| 159 | */ |
||
| 160 | public function broadcastWrapper(GSWrapper $wrapper, string $protocol): void { |
||
| 161 | $status = GSWrapper::STATUS_FAILED; |
||
| 162 | |||
| 163 | try { |
||
| 164 | $this->broadcastEvent($wrapper->getEvent(), $wrapper->getInstance(), $protocol); |
||
| 165 | $status = GSWrapper::STATUS_DONE; |
||
| 166 | } catch (RequestContentException | RequestNetworkException | RequestResultSizeException | RequestServerException | RequestResultNotJsonException $e) { |
||
|
|
|||
| 167 | } |
||
| 168 | |||
| 169 | if ($wrapper->getSeverity() === GSEvent::SEVERITY_HIGH) { |
||
| 170 | $wrapper->setStatus($status); |
||
| 171 | } else { |
||
| 172 | $wrapper->setStatus(GSWrapper::STATUS_OVER); |
||
| 173 | } |
||
| 174 | |||
| 175 | $this->gsEventsRequest->update($wrapper); |
||
| 176 | } |
||
| 177 | |||
| 178 | |||
| 179 | /** |
||
| 180 | * @param GSEvent $event |
||
| 181 | * @param string $instance |
||
| 182 | * @param string $protocol |
||
| 183 | * |
||
| 184 | * @throws RequestContentException |
||
| 185 | * @throws RequestNetworkException |
||
| 186 | * @throws RequestResultNotJsonException |
||
| 187 | * @throws RequestResultSizeException |
||
| 188 | * @throws RequestServerException |
||
| 189 | */ |
||
| 190 | public function broadcastEvent(GSEvent $event, string $instance, string $protocol = ''): void { |
||
| 191 | $this->signEvent($event); |
||
| 192 | |||
| 193 | $path = $this->urlGenerator->linkToRoute('circles.GlobalScale.broadcast'); |
||
| 194 | $request = new Request($path, Request::TYPE_POST); |
||
| 195 | |||
| 196 | $protocols = ['https', 'http']; |
||
| 197 | if ($protocol !== '') { |
||
| 198 | $protocols = [$protocol]; |
||
| 199 | } |
||
| 200 | |||
| 201 | $request->setProtocols($protocols); |
||
| 202 | $request->setDataSerialize($event); |
||
| 203 | |||
| 204 | $request->setAddress($instance); |
||
| 205 | |||
| 206 | $data = $this->retrieveJson($request); |
||
| 207 | $event->setResult(new SimpleDataStore($this->getArray('result', $data, []))); |
||
| 208 | } |
||
| 209 | |||
| 210 | |||
| 211 | /** |
||
| 212 | * @param GSEvent $event |
||
| 213 | * |
||
| 214 | * @throws RequestContentException |
||
| 215 | * @throws RequestNetworkException |
||
| 216 | * @throws RequestResultSizeException |
||
| 217 | * @throws RequestServerException |
||
| 218 | * @throws RequestResultNotJsonException |
||
| 219 | * @throws GlobalScaleEventException |
||
| 220 | */ |
||
| 221 | public function confirmEvent(GSEvent $event): void { |
||
| 222 | $this->signEvent($event); |
||
| 223 | |||
| 224 | $circle = $event->getCircle(); |
||
| 225 | $owner = $circle->getOwner(); |
||
| 226 | $path = $this->urlGenerator->linkToRoute('circles.GlobalScale.event'); |
||
| 227 | |||
| 228 | $request = new Request($path, Request::TYPE_POST); |
||
| 229 | $request->setProtocols([$_SERVER['REQUEST_SCHEME']]); |
||
| 230 | $request->setAddressFromUrl($owner->getInstance()); |
||
| 231 | $request->setDataSerialize($event); |
||
| 232 | |||
| 233 | $result = $this->retrieveJson($request); |
||
| 234 | $this->miscService->log('result ' . json_encode($result)); |
||
| 235 | if ($this->getInt('status', $result) === 0) { |
||
| 236 | throw new GlobalScaleEventException($this->get('error', $result)); |
||
| 237 | } |
||
| 238 | } |
||
| 239 | |||
| 240 | |||
| 241 | /** |
||
| 242 | * @param GSEvent $event |
||
| 243 | * |
||
| 244 | * @throws GSStatusException |
||
| 245 | */ |
||
| 246 | private function fillEvent(GSEvent $event): void { |
||
| 247 | if (!$this->configService->getGSStatus(ConfigService::GS_ENABLED)) { |
||
| 248 | return; |
||
| 249 | } |
||
| 250 | |||
| 251 | $event->setSource($this->configService->getLocalCloudId()); |
||
| 252 | } |
||
| 253 | |||
| 254 | |||
| 255 | /** |
||
| 256 | * @param GSEvent $event |
||
| 257 | */ |
||
| 258 | private function signEvent(GSevent $event) { |
||
| 259 | $event->setKey($this->globalScaleService->getKey()); |
||
| 260 | } |
||
| 261 | |||
| 262 | |||
| 263 | /** |
||
| 264 | * We check that the event can be managed/checked locally or if the owner of the circle belongs to |
||
| 265 | * an other instance of Nextcloud |
||
| 266 | * |
||
| 267 | * @param GSEvent $event |
||
| 268 | *asyncBroadcast |
||
| 269 | * |
||
| 270 | * @return bool |
||
| 271 | */ |
||
| 272 | private function isLocalEvent(GSEvent $event): bool { |
||
| 288 | |||
| 289 | |||
| 290 | /** |
||
| 291 | * @param string $token |
||
| 292 | * |
||
| 293 | * @return GSWrapper[] |
||
| 294 | * @throws JsonException |
||
| 295 | * @throws ModelException |
||
| 296 | */ |
||
| 297 | public function getEventsByToken(string $token): array { |
||
| 300 | |||
| 301 | |||
| 302 | /** |
||
| 303 | * Deprecated ? |
||
| 304 | * should be used to manage results from events, like sending mails on user creation |
||
| 305 | * |
||
| 306 | * @param string $token |
||
| 307 | */ |
||
| 308 | public function manageResults(string $token): void { |
||
| 335 | |||
| 336 | |||
| 337 | /** |
||
| 338 | * @throws GSStatusException |
||
| 339 | */ |
||
| 340 | public function synchronize() { |
||
| 349 | |||
| 350 | |||
| 351 | /** |
||
| 352 | * @param array $circles |
||
| 353 | * |
||
| 354 | * @throws GSStatusException |
||
| 355 | */ |
||
| 356 | public function synchronizeCircles(array $circles): void { |
||
| 368 | |||
| 369 | |||
| 370 | /** |
||
| 371 | * @return Circle[] |
||
| 372 | */ |
||
| 373 | private function getCirclesToSync(): array { |
||
| 392 | |||
| 393 | |||
| 394 | /** |
||
| 395 | * |
||
| 396 | */ |
||
| 397 | private function removeDeprecatedCircles() { |
||
| 412 | |||
| 413 | |||
| 414 | /** |
||
| 415 | * @param Circle $circle |
||
| 416 | * |
||
| 417 | * @throws GSStatusException |
||
| 418 | */ |
||
| 419 | private function checkCircle(Circle $circle): void { |
||
| 427 | |||
| 428 | |||
| 429 | /** |
||
| 430 | * @param Circle $circle |
||
| 431 | * |
||
| 432 | * @return bool |
||
| 433 | * @throws GSStatusException |
||
| 434 | */ |
||
| 435 | public function confirmCircleStatus(Circle $circle): bool { |
||
| 513 | |||
| 514 | /** |
||
| 515 | * @throws GSStatusException |
||
| 516 | */ |
||
| 517 | public function syncEvents() { |
||
| 520 | |||
| 521 | /** |
||
| 522 | * |
||
| 523 | */ |
||
| 524 | private function removeDeprecatedEvents() { |
||
| 528 | |||
| 529 | |||
| 530 | } |
||
| 531 | |||
| 532 |