Complex classes like RemoteStreamService 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 RemoteStreamService, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 72 | class RemoteStreamService extends NC22Signature { |
||
| 73 | |||
| 74 | |||
| 75 | use TNC22Deserialize; |
||
| 76 | use TNC22LocalSignatory; |
||
| 77 | use TStringTools; |
||
| 78 | use TNC22WellKnown; |
||
| 79 | |||
| 80 | |||
| 81 | const UPDATE_DATA = 'data'; |
||
| 82 | const UPDATE_ITEM = 'item'; |
||
| 83 | const UPDATE_TYPE = 'type'; |
||
| 84 | const UPDATE_INSTANCE = 'instance'; |
||
| 85 | const UPDATE_HREF = 'href'; |
||
| 86 | |||
| 87 | |||
| 88 | /** @var IURLGenerator */ |
||
| 89 | private $urlGenerator; |
||
| 90 | |||
| 91 | /** @var RemoteRequest */ |
||
| 92 | private $remoteRequest; |
||
| 93 | |||
| 94 | /** @var InterfaceService */ |
||
| 95 | private $interfaceService; |
||
| 96 | |||
| 97 | /** @var ConfigService */ |
||
| 98 | private $configService; |
||
| 99 | |||
| 100 | |||
| 101 | /** |
||
| 102 | * RemoteStreamService constructor. |
||
| 103 | * |
||
| 104 | * @param IURLGenerator $urlGenerator |
||
| 105 | * @param RemoteRequest $remoteRequest |
||
| 106 | * @param InterfaceService $interfaceService |
||
| 107 | * @param ConfigService $configService |
||
| 108 | */ |
||
| 109 | public function __construct( |
||
| 110 | IURLGenerator $urlGenerator, |
||
| 111 | RemoteRequest $remoteRequest, |
||
| 112 | InterfaceService $interfaceService, |
||
| 113 | ConfigService $configService |
||
| 114 | ) { |
||
| 115 | $this->setup('app', 'circles'); |
||
| 116 | |||
| 117 | $this->urlGenerator = $urlGenerator; |
||
| 118 | $this->remoteRequest = $remoteRequest; |
||
| 119 | $this->interfaceService = $interfaceService; |
||
| 120 | $this->configService = $configService; |
||
| 121 | } |
||
| 122 | |||
| 123 | |||
| 124 | /** |
||
| 125 | * Returns the Signatory model for the Circles app. |
||
| 126 | * Can be signed with a confirmKey. |
||
| 127 | * |
||
| 128 | * @param bool $generate |
||
| 129 | * @param string $confirmKey |
||
| 130 | * |
||
| 131 | * @return RemoteInstance |
||
| 132 | * @throws SignatoryException |
||
| 133 | * @throws UnknownInterfaceException |
||
| 134 | */ |
||
| 135 | public function getAppSignatory(bool $generate = true, string $confirmKey = ''): RemoteInstance { |
||
| 136 | $app = new RemoteInstance($this->interfaceService->getCloudPath('circles.Remote.appService')); |
||
| 137 | $this->fillSimpleSignatory($app, $generate); |
||
| 138 | $app->setUidFromKey(); |
||
| 139 | |||
| 140 | if ($confirmKey !== '') { |
||
| 141 | $app->setAuthSigned($this->signString($confirmKey, $app)); |
||
| 142 | } |
||
| 143 | |||
| 144 | $app->setRoot($this->interfaceService->getCloudPath()); |
||
| 145 | $app->setEvent($this->interfaceService->getCloudPath('circles.Remote.event')); |
||
| 146 | $app->setIncoming($this->interfaceService->getCloudPath('circles.Remote.incoming')); |
||
| 147 | $app->setTest($this->interfaceService->getCloudPath('circles.Remote.test')); |
||
| 148 | $app->setCircles($this->interfaceService->getCloudPath('circles.Remote.circles')); |
||
| 149 | $app->setCircle( |
||
| 150 | urldecode( |
||
| 151 | $this->interfaceService->getCloudPath('circles.Remote.circle', ['circleId' => '{circleId}']) |
||
| 152 | ) |
||
| 153 | ); |
||
| 154 | $app->setMembers( |
||
| 155 | urldecode( |
||
| 156 | $this->interfaceService->getCloudPath('circles.Remote.members', ['circleId' => '{circleId}']) |
||
| 157 | ) |
||
| 158 | ); |
||
| 159 | $app->setMember( |
||
| 160 | urldecode( |
||
| 161 | $this->interfaceService->getCloudPath( |
||
| 162 | 'circles.Remote.member', ['type' => '{type}', 'userId' => '{userId}'] |
||
| 163 | ) |
||
| 164 | ) |
||
| 165 | ); |
||
| 166 | |||
| 167 | if ($this->interfaceService->isCurrentInterfaceInternal()) { |
||
| 168 | $app->setAliases(array_values(array_filter($this->interfaceService->getInterfaces(false)))); |
||
| 169 | } |
||
| 170 | |||
| 171 | $app->setOrigData($this->serialize($app)); |
||
| 172 | |||
| 173 | return $app; |
||
| 174 | } |
||
| 175 | |||
| 176 | |||
| 177 | /** |
||
| 178 | * Reset the Signatory (and the Identity) for the Circles app. |
||
| 179 | */ |
||
| 180 | public function resetAppSignatory(): void { |
||
| 181 | try { |
||
| 182 | $app = $this->getAppSignatory(); |
||
| 183 | |||
| 184 | $this->removeSimpleSignatory($app); |
||
| 185 | } catch (SignatoryException $e) { |
||
|
|
|||
| 186 | } |
||
| 187 | } |
||
| 188 | |||
| 189 | |||
| 190 | /** |
||
| 191 | * shortcut to requestRemoteInstance that return result if available, or exception. |
||
| 192 | * |
||
| 193 | * @param string $instance |
||
| 194 | * @param string $item |
||
| 195 | * @param int $type |
||
| 196 | * @param JsonSerializable|null $object |
||
| 197 | * @param array $params |
||
| 198 | * |
||
| 199 | * @return array |
||
| 200 | * @throws RemoteInstanceException |
||
| 201 | * @throws RemoteNotFoundException |
||
| 202 | * @throws RemoteResourceNotFoundException |
||
| 203 | * @throws UnknownRemoteException |
||
| 204 | * @throws FederatedItemException |
||
| 205 | */ |
||
| 206 | public function resultRequestRemoteInstance( |
||
| 207 | string $instance, |
||
| 208 | string $item, |
||
| 209 | int $type = Request::TYPE_GET, |
||
| 210 | ?JsonSerializable $object = null, |
||
| 211 | array $params = [] |
||
| 212 | ): array { |
||
| 213 | $this->interfaceService->setCurrentInterfaceFromInstance($instance); |
||
| 214 | |||
| 215 | $signedRequest = $this->requestRemoteInstance($instance, $item, $type, $object, $params); |
||
| 216 | if (!$signedRequest->getOutgoingRequest()->hasResult()) { |
||
| 217 | throw new RemoteInstanceException(); |
||
| 218 | } |
||
| 219 | |||
| 220 | $result = $signedRequest->getOutgoingRequest()->getResult(); |
||
| 221 | |||
| 222 | if ($result->getStatusCode() === Http::STATUS_OK) { |
||
| 223 | return $result->getAsArray(); |
||
| 224 | } |
||
| 225 | |||
| 226 | throw $this->getFederatedItemExceptionFromResult($result); |
||
| 227 | } |
||
| 228 | |||
| 229 | |||
| 230 | /** |
||
| 231 | * Send a request to a remote instance, based on: |
||
| 232 | * - instance: address as saved in database, |
||
| 233 | * - item: the item to request (incoming, event, ...) |
||
| 234 | * - type: GET, POST |
||
| 235 | * - data: Serializable to be send if needed |
||
| 236 | * |
||
| 237 | * @param string $instance |
||
| 238 | * @param string $item |
||
| 239 | * @param int $type |
||
| 240 | * @param JsonSerializable|null $object |
||
| 241 | * @param array $params |
||
| 242 | * |
||
| 243 | * @return NC22SignedRequest |
||
| 244 | * @throws RemoteNotFoundException |
||
| 245 | * @throws RemoteResourceNotFoundException |
||
| 246 | * @throws UnknownRemoteException |
||
| 247 | * @throws RemoteInstanceException |
||
| 248 | * @throws UnknownInterfaceException |
||
| 249 | */ |
||
| 250 | private function requestRemoteInstance( |
||
| 251 | string $instance, |
||
| 252 | string $item, |
||
| 253 | int $type = Request::TYPE_GET, |
||
| 254 | ?JsonSerializable $object = null, |
||
| 255 | array $params = [] |
||
| 256 | ): NC22SignedRequest { |
||
| 257 | $request = new NC22Request('', $type); |
||
| 258 | $this->configService->configureRequest($request); |
||
| 259 | $link = $this->getRemoteInstanceEntry($instance, $item, $params); |
||
| 260 | $request->basedOnUrl($link); |
||
| 261 | |||
| 262 | // TODO: Work Around: on local, if object is empty, request takes 10s. check on other configuration |
||
| 263 | if (is_null($object) || empty($object->jsonSerialize())) { |
||
| 264 | $object = new SimpleDataStore(['empty' => 1]); |
||
| 265 | } |
||
| 266 | |||
| 267 | if (!is_null($object)) { |
||
| 268 | $request->setDataSerialize($object); |
||
| 269 | } |
||
| 270 | |||
| 271 | try { |
||
| 272 | $app = $this->getAppSignatory(); |
||
| 273 | // $app->setAlgorithm(NC22Signatory::SHA512); |
||
| 274 | $signedRequest = $this->signOutgoingRequest($request, $app); |
||
| 275 | $this->doRequest($signedRequest->getOutgoingRequest(), false); |
||
| 276 | } catch (RequestNetworkException | SignatoryException $e) { |
||
| 277 | throw new RemoteInstanceException($e->getMessage()); |
||
| 278 | } |
||
| 279 | |||
| 280 | return $signedRequest; |
||
| 281 | } |
||
| 282 | |||
| 283 | |||
| 284 | /** |
||
| 285 | * get the value of an entry from the Signatory of the RemoteInstance. |
||
| 286 | * |
||
| 287 | * @param string $instance |
||
| 288 | * @param string $item |
||
| 289 | * @param array $params |
||
| 290 | * |
||
| 291 | * @return string |
||
| 292 | * @throws RemoteNotFoundException |
||
| 293 | * @throws RemoteResourceNotFoundException |
||
| 294 | * @throws UnknownRemoteException |
||
| 295 | */ |
||
| 296 | private function getRemoteInstanceEntry(string $instance, string $item, array $params = []): string { |
||
| 306 | |||
| 307 | |||
| 308 | /** |
||
| 309 | * get RemoteInstance with confirmed and known identity from database. |
||
| 310 | * |
||
| 311 | * @param string $instance |
||
| 312 | * |
||
| 313 | * @return RemoteInstance |
||
| 314 | * @throws RemoteNotFoundException |
||
| 315 | * @throws UnknownRemoteException |
||
| 316 | */ |
||
| 317 | public function getCachedRemoteInstance(string $instance): RemoteInstance { |
||
| 325 | |||
| 326 | |||
| 327 | /** |
||
| 328 | * Add a remote instance, based on the address |
||
| 329 | * |
||
| 330 | * @param string $instance |
||
| 331 | * |
||
| 332 | * @return RemoteInstance |
||
| 333 | * @throws RequestNetworkException |
||
| 334 | * @throws SignatoryException |
||
| 335 | * @throws SignatureException |
||
| 336 | * @throws WellKnownLinkNotFoundException |
||
| 337 | */ |
||
| 338 | public function retrieveRemoteInstance(string $instance): RemoteInstance { |
||
| 339 | $resource = $this->getResourceData($instance, Application::APP_SUBJECT, Application::APP_REL); |
||
| 340 | |||
| 341 | /** @var RemoteInstance $remoteInstance */ |
||
| 342 | $remoteInstance = $this->retrieveSignatory($resource->g('id'), true); |
||
| 343 | $remoteInstance->setInstance($instance); |
||
| 344 | |||
| 345 | return $remoteInstance; |
||
| 346 | } |
||
| 347 | |||
| 348 | |||
| 349 | /** |
||
| 350 | * retrieve Signatory. |
||
| 351 | * |
||
| 352 | * @param string $keyId |
||
| 353 | * @param bool $refresh |
||
| 354 | * |
||
| 355 | * @return RemoteInstance |
||
| 356 | * @throws SignatoryException |
||
| 357 | * @throws SignatureException |
||
| 358 | */ |
||
| 359 | public function retrieveSignatory(string $keyId, bool $refresh = true): NC22Signatory { |
||
| 381 | |||
| 382 | |||
| 383 | /** |
||
| 384 | * Add a remote instance, based on the address |
||
| 385 | * |
||
| 386 | * @param string $instance |
||
| 387 | * @param string $type |
||
| 388 | * @param int $iface |
||
| 389 | * @param bool $overwrite |
||
| 390 | * |
||
| 391 | * @throws RemoteAlreadyExistsException |
||
| 392 | * @throws RemoteUidException |
||
| 393 | * @throws RequestNetworkException |
||
| 394 | * @throws SignatoryException |
||
| 395 | * @throws SignatureException |
||
| 396 | * @throws WellKnownLinkNotFoundException |
||
| 397 | */ |
||
| 398 | public function addRemoteInstance( |
||
| 428 | |||
| 429 | |||
| 430 | /** |
||
| 431 | * Confirm the Auth of a RemoteInstance, based on the result from a request |
||
| 432 | * |
||
| 433 | * @param RemoteInstance $remote |
||
| 434 | * @param string $auth |
||
| 435 | * |
||
| 436 | * @throws SignatureException |
||
| 437 | */ |
||
| 438 | private function confirmAuth(RemoteInstance $remote, string $auth): void { |
||
| 454 | |||
| 455 | |||
| 456 | /** |
||
| 457 | * @param NC22RequestResult $result |
||
| 458 | * |
||
| 459 | * @return FederatedItemException |
||
| 460 | */ |
||
| 461 | private function getFederatedItemExceptionFromResult(NC22RequestResult $result): FederatedItemException { |
||
| 478 | |||
| 479 | |||
| 480 | /** |
||
| 481 | * @param ReflectionClass $class |
||
| 482 | * |
||
| 483 | * @return void |
||
| 484 | * @throws FederatedItemException |
||
| 485 | */ |
||
| 486 | private function confirmFederatedItemExceptionFromClass(ReflectionClass $class): void { |
||
| 499 | |||
| 500 | |||
| 501 | /** |
||
| 502 | * @param int $statusCode |
||
| 503 | * |
||
| 504 | * @return string |
||
| 505 | */ |
||
| 506 | private function getFederatedItemExceptionFromStatus(int $statusCode): string { |
||
| 515 | |||
| 516 | |||
| 517 | /** |
||
| 518 | * TODO: confirm if method is really needed |
||
| 519 | * |
||
| 520 | * @param RemoteInstance $remote |
||
| 521 | * @param RemoteInstance|null $stored |
||
| 522 | * |
||
| 523 | * @throws RemoteNotFoundException |
||
| 524 | * @throws RemoteUidException |
||
| 525 | */ |
||
| 526 | public function confirmValidRemote(RemoteInstance $remote, ?RemoteInstance &$stored = null): void { |
||
| 541 | |||
| 542 | |||
| 543 | /** |
||
| 544 | * TODO: check if this method is not useless |
||
| 545 | * |
||
| 546 | * @param RemoteInstance $remote |
||
| 547 | * @param string $update |
||
| 548 | * |
||
| 549 | * @throws RemoteUidException |
||
| 550 | */ |
||
| 551 | public function update(RemoteInstance $remote, string $update = self::UPDATE_DATA): void { |
||
| 578 | |||
| 579 | } |
||
| 580 | |||
| 581 |