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); |
||
| 61 | class GSUpstreamService { |
||
| 62 | |||
| 63 | |||
| 64 | use TRequest; |
||
| 65 | use TArrayTools; |
||
| 66 | |||
| 67 | |||
| 68 | /** @var string */ |
||
| 69 | private $userId = ''; |
||
| 70 | |||
| 71 | /** @var IURLGenerator */ |
||
| 72 | private $urlGenerator; |
||
| 73 | |||
| 74 | /** @var GSEventsRequest */ |
||
| 75 | private $gsEventsRequest; |
||
| 76 | |||
| 77 | /** @var CirclesRequest */ |
||
| 78 | private $circlesRequest; |
||
| 79 | |||
| 80 | /** @var GlobalScaleService */ |
||
| 81 | private $globalScaleService; |
||
| 82 | |||
| 83 | /** @var ConfigService */ |
||
| 84 | private $configService; |
||
| 85 | |||
| 86 | /** @var MiscService */ |
||
| 87 | private $miscService; |
||
| 88 | |||
| 89 | |||
| 90 | /** |
||
| 91 | * GSUpstreamService constructor. |
||
| 92 | * |
||
| 93 | * @param $userId |
||
| 94 | * @param IURLGenerator $urlGenerator |
||
| 95 | * @param GSEventsRequest $gsEventsRequest |
||
| 96 | * @param CirclesRequest $circlesRequest |
||
| 97 | * @param GlobalScaleService $globalScaleService |
||
| 98 | * @param ConfigService $configService |
||
| 99 | * @param MiscService $miscService |
||
| 100 | */ |
||
| 101 | public function __construct( |
||
| 118 | |||
| 119 | |||
| 120 | /** |
||
| 121 | * @param GSEvent $event |
||
| 122 | * |
||
| 123 | * @throws Exception |
||
| 124 | */ |
||
| 125 | public function newEvent(GSEvent $event) { |
||
| 126 | try { |
||
| 127 | $gs = $this->globalScaleService->getGlobalScaleEvent($event); |
||
| 128 | |||
| 129 | $this->fillEvent($event); |
||
| 130 | if ($this->isLocalEvent($event)) { |
||
| 131 | $gs->verify($event, true); |
||
| 132 | $gs->manage($event); |
||
| 133 | |||
| 134 | $this->globalScaleService->asyncBroadcast($event); |
||
| 135 | } else { |
||
| 136 | $gs->verify($event); // needed ? as we check event on the 'master' of the circle |
||
| 137 | $this->confirmEvent($event); |
||
| 138 | $gs->manage($event); |
||
| 139 | } |
||
| 140 | } catch (Exception $e) { |
||
| 141 | $this->miscService->log( |
||
| 142 | get_class($e) . ' on new event: ' . $e->getMessage() . ' - ' . json_encode($event), 1 |
||
| 143 | ); |
||
| 144 | throw $e; |
||
| 145 | } |
||
| 146 | } |
||
| 147 | |||
| 148 | |||
| 149 | /** |
||
| 150 | * @param GSWrapper $wrapper |
||
| 151 | * @param string $protocol |
||
| 152 | */ |
||
| 153 | public function broadcastWrapper(GSWrapper $wrapper, string $protocol): void { |
||
| 154 | $status = GSWrapper::STATUS_FAILED; |
||
| 155 | |||
| 156 | try { |
||
| 157 | $this->broadcastEvent($wrapper->getEvent(), $wrapper->getInstance(), $protocol); |
||
| 158 | $status = GSWrapper::STATUS_DONE; |
||
| 159 | } catch (RequestContentException | RequestNetworkException | RequestResultSizeException | RequestServerException | RequestResultNotJsonException $e) { |
||
|
|
|||
| 160 | } |
||
| 161 | |||
| 162 | if ($wrapper->getSeverity() === GSEvent::SEVERITY_HIGH) { |
||
| 163 | $wrapper->setStatus($status); |
||
| 164 | } else { |
||
| 165 | $wrapper->setStatus(GSWrapper::STATUS_OVER); |
||
| 166 | } |
||
| 167 | |||
| 168 | $this->gsEventsRequest->update($wrapper); |
||
| 169 | } |
||
| 170 | |||
| 171 | |||
| 172 | /** |
||
| 173 | * @param GSEvent $event |
||
| 174 | * @param string $instance |
||
| 175 | * @param string $protocol |
||
| 176 | * |
||
| 177 | * @throws RequestContentException |
||
| 178 | * @throws RequestNetworkException |
||
| 179 | * @throws RequestResultNotJsonException |
||
| 180 | * @throws RequestResultSizeException |
||
| 181 | * @throws RequestServerException |
||
| 182 | */ |
||
| 183 | public function broadcastEvent(GSEvent $event, string $instance, string $protocol = ''): void { |
||
| 184 | $this->signEvent($event); |
||
| 185 | |||
| 186 | $path = $this->urlGenerator->linkToRoute('circles.GlobalScale.broadcast'); |
||
| 187 | $request = new Request($path, Request::TYPE_POST); |
||
| 188 | |||
| 189 | $protocols = ['https', 'http']; |
||
| 190 | if ($protocol !== '') { |
||
| 191 | $protocols = [$protocol]; |
||
| 192 | } |
||
| 193 | |||
| 194 | $request->setProtocols($protocols); |
||
| 195 | $request->setDataSerialize($event); |
||
| 196 | |||
| 197 | $request->setAddress($instance); |
||
| 198 | |||
| 199 | $data = $this->retrieveJson($request); |
||
| 200 | $event->setResult(new SimpleDataStore($this->getArray('result', $data, []))); |
||
| 201 | } |
||
| 202 | |||
| 203 | |||
| 204 | /** |
||
| 205 | * @param GSEvent $event |
||
| 206 | * |
||
| 207 | * @throws RequestContentException |
||
| 208 | * @throws RequestNetworkException |
||
| 209 | * @throws RequestResultSizeException |
||
| 210 | * @throws RequestServerException |
||
| 211 | * @throws RequestResultNotJsonException |
||
| 212 | * @throws GlobalScaleEventException |
||
| 213 | */ |
||
| 214 | public function confirmEvent(GSEvent $event): void { |
||
| 232 | |||
| 233 | |||
| 234 | /** |
||
| 235 | * @param GSEvent $event |
||
| 236 | * |
||
| 237 | * @throws GSStatusException |
||
| 238 | */ |
||
| 239 | private function fillEvent(GSEvent $event): void { |
||
| 246 | |||
| 247 | |||
| 248 | /** |
||
| 249 | * @param GSEvent $event |
||
| 250 | */ |
||
| 251 | private function signEvent(GSevent $event) { |
||
| 254 | |||
| 255 | |||
| 256 | /** |
||
| 257 | * We check that the event can be managed/checked locally or if the owner of the circle belongs to |
||
| 258 | * an other instance of Nextcloud |
||
| 259 | * |
||
| 260 | * @param GSEvent $event |
||
| 261 | *asyncBroadcast |
||
| 262 | * |
||
| 263 | * @return bool |
||
| 264 | */ |
||
| 265 | private function isLocalEvent(GSEvent $event): bool { |
||
| 281 | |||
| 282 | |||
| 283 | /** |
||
| 284 | * @param string $token |
||
| 285 | * |
||
| 286 | * @return GSWrapper[] |
||
| 287 | * @throws JsonException |
||
| 288 | * @throws ModelException |
||
| 289 | */ |
||
| 290 | public function getEventsByToken(string $token): array { |
||
| 293 | |||
| 294 | |||
| 295 | /** |
||
| 296 | * @param array $circles |
||
| 297 | * |
||
| 298 | * @throws GSStatusException |
||
| 299 | */ |
||
| 300 | public function syncCircles(array $circles): void { |
||
| 312 | |||
| 313 | |||
| 314 | /** |
||
| 315 | * @param Circle $circle |
||
| 316 | * |
||
| 317 | * @return bool |
||
| 318 | * @throws GSStatusException |
||
| 319 | */ |
||
| 320 | public function confirmCircleStatus(Circle $circle): bool { |
||
| 398 | |||
| 399 | |||
| 400 | /** |
||
| 401 | * @param string $token |
||
| 402 | */ |
||
| 403 | public function manageResults(string $token): void { |
||
| 430 | |||
| 431 | |||
| 432 | /** |
||
| 433 | * |
||
| 434 | */ |
||
| 435 | public function deprecatedEvents(): void { |
||
| 438 | |||
| 439 | } |
||
| 440 | |||
| 441 |