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_TYPE = 'type'; |
||
| 83 | const UPDATE_INSTANCE = 'instance'; |
||
| 84 | const UPDATE_HREF = 'href'; |
||
| 85 | |||
| 86 | |||
| 87 | /** @var IL10N */ |
||
| 88 | private $l10n; |
||
| 89 | |||
| 90 | /** @var IURLGenerator */ |
||
| 91 | private $urlGenerator; |
||
| 92 | |||
| 93 | /** @var RemoteRequest */ |
||
| 94 | private $remoteRequest; |
||
| 95 | |||
| 96 | /** @var ConfigService */ |
||
| 97 | private $configService; |
||
| 98 | |||
| 99 | |||
| 100 | /** |
||
| 101 | * RemoteStreamService constructor. |
||
| 102 | * |
||
| 103 | * @param IL10N $l10n |
||
| 104 | * @param IURLGenerator $urlGenerator |
||
| 105 | * @param RemoteRequest $remoteRequest |
||
| 106 | * @param ConfigService $configService |
||
| 107 | */ |
||
| 108 | public function __construct( |
||
| 118 | |||
| 119 | |||
| 120 | /** |
||
| 121 | * Returns the Signatory model for the Circles app. |
||
| 122 | * Can be signed with a confirmKey. |
||
| 123 | * |
||
| 124 | * @param bool $generate |
||
| 125 | * @param string $confirmKey |
||
| 126 | * |
||
| 127 | * @return RemoteInstance |
||
| 128 | * @throws SignatoryException |
||
| 129 | */ |
||
| 130 | public function getAppSignatory(bool $generate = true, string $confirmKey = ''): RemoteInstance { |
||
| 173 | |||
| 174 | |||
| 175 | /** |
||
| 176 | * Reset the Signatory (and the Identity) for the Circles app. |
||
| 177 | */ |
||
| 178 | public function resetAppSignatory(): void { |
||
| 186 | |||
| 187 | |||
| 188 | /** |
||
| 189 | * shortcut to requestRemoteInstance that return result if available, or exception. |
||
| 190 | * |
||
| 191 | * @param string $instance |
||
| 192 | * @param string $item |
||
| 193 | * @param int $type |
||
| 194 | * @param JsonSerializable|null $object |
||
| 195 | * @param array $params |
||
| 196 | * |
||
| 197 | * @return array |
||
| 198 | * @throws RemoteInstanceException |
||
| 199 | * @throws RemoteNotFoundException |
||
| 200 | * @throws RemoteResourceNotFoundException |
||
| 201 | * @throws UnknownRemoteException |
||
| 202 | * @throws FederatedItemException |
||
| 203 | */ |
||
| 204 | public function resultRequestRemoteInstance( |
||
| 225 | |||
| 226 | |||
| 227 | /** |
||
| 228 | * Send a request to a remote instance, based on: |
||
| 229 | * - instance: address as saved in database, |
||
| 230 | * - item: the item to request (incoming, event, ...) |
||
| 231 | * - type: GET, POST |
||
| 232 | * - data: Serializable to be send if needed |
||
| 233 | * |
||
| 234 | * @param string $instance |
||
| 235 | * @param string $item |
||
| 236 | * @param int $type |
||
| 237 | * @param JsonSerializable|null $object |
||
| 238 | * @param array $params |
||
| 239 | * |
||
| 240 | * @return NC22SignedRequest |
||
| 241 | * @throws RemoteNotFoundException |
||
| 242 | * @throws RemoteResourceNotFoundException |
||
| 243 | * @throws UnknownRemoteException |
||
| 244 | * @throws RemoteInstanceException |
||
| 245 | */ |
||
| 246 | private function requestRemoteInstance( |
||
| 283 | |||
| 284 | |||
| 285 | /** |
||
| 286 | * get the value of an entry from the Signatory of the RemoteInstance. |
||
| 287 | * |
||
| 288 | * @param string $instance |
||
| 289 | * @param string $item |
||
| 290 | * @param array $params |
||
| 291 | * |
||
| 292 | * @return string |
||
| 293 | * @throws RemoteNotFoundException |
||
| 294 | * @throws RemoteResourceNotFoundException |
||
| 295 | * @throws UnknownRemoteException |
||
| 296 | */ |
||
| 297 | private function getRemoteInstanceEntry(string $instance, string $item, array $params = []): string { |
||
| 307 | |||
| 308 | |||
| 309 | /** |
||
| 310 | * get RemoteInstance with confirmed and known identity from database. |
||
| 311 | * |
||
| 312 | * @param string $instance |
||
| 313 | * |
||
| 314 | * @return RemoteInstance |
||
| 315 | * @throws RemoteNotFoundException |
||
| 316 | * @throws UnknownRemoteException |
||
| 317 | */ |
||
| 318 | public function getCachedRemoteInstance(string $instance): RemoteInstance { |
||
| 326 | |||
| 327 | |||
| 328 | /** |
||
| 329 | * Add a remote instance, based on the address |
||
| 330 | * |
||
| 331 | * @param string $instance |
||
| 332 | * |
||
| 333 | * @return RemoteInstance |
||
| 334 | * @throws RequestNetworkException |
||
| 335 | * @throws SignatoryException |
||
| 336 | * @throws SignatureException |
||
| 337 | * @throws WellKnownLinkNotFoundException |
||
| 338 | */ |
||
| 339 | public function retrieveRemoteInstance(string $instance): RemoteInstance { |
||
| 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): RemoteInstance { |
||
| 381 | |||
| 382 | |||
| 383 | /** |
||
| 384 | * Add a remote instance, based on the address |
||
| 385 | * |
||
| 386 | * @param string $instance |
||
| 387 | * @param string $type |
||
| 388 | * @param bool $overwrite |
||
| 389 | * |
||
| 390 | * @throws RequestNetworkException |
||
| 391 | * @throws SignatoryException |
||
| 392 | * @throws SignatureException |
||
| 393 | * @throws WellKnownLinkNotFoundException |
||
| 394 | * @throws RemoteAlreadyExistsException |
||
| 395 | * @throws RemoteUidException |
||
| 396 | */ |
||
| 397 | public function addRemoteInstance( |
||
| 419 | |||
| 420 | |||
| 421 | /** |
||
| 422 | * Confirm the Auth of a RemoteInstance, based on the result from a request |
||
| 423 | * |
||
| 424 | * @param RemoteInstance $remote |
||
| 425 | * @param string $auth |
||
| 426 | * |
||
| 427 | * @throws SignatureException |
||
| 428 | */ |
||
| 429 | private function confirmAuth(RemoteInstance $remote, string $auth): void { |
||
| 445 | |||
| 446 | |||
| 447 | /** |
||
| 448 | * @param NC22RequestResult $result |
||
| 449 | * |
||
| 450 | * @return FederatedItemException |
||
| 451 | */ |
||
| 452 | private function getFederatedItemExceptionFromResult(NC22RequestResult $result): FederatedItemException { |
||
| 469 | |||
| 470 | |||
| 471 | /** |
||
| 472 | * @param ReflectionClass $class |
||
| 473 | * |
||
| 474 | * @return void |
||
| 475 | * @throws FederatedItemException |
||
| 476 | */ |
||
| 477 | private function confirmFederatedItemExceptionFromClass(ReflectionClass $class): void { |
||
| 490 | |||
| 491 | |||
| 492 | /** |
||
| 493 | * @param int $statusCode |
||
| 494 | * |
||
| 495 | * @return string |
||
| 496 | */ |
||
| 497 | private function getFederatedItemExceptionFromStatus(int $statusCode): string { |
||
| 506 | |||
| 507 | |||
| 508 | /** |
||
| 509 | * TODO: confirm if method is really needed |
||
| 510 | * |
||
| 511 | * @param RemoteInstance $remote |
||
| 512 | * @param RemoteInstance|null $stored |
||
| 513 | * |
||
| 514 | * @throws RemoteNotFoundException |
||
| 515 | * @throws RemoteUidException |
||
| 516 | */ |
||
| 517 | public function confirmValidRemote(RemoteInstance $remote, ?RemoteInstance &$stored = null): void { |
||
| 532 | |||
| 533 | |||
| 534 | /** |
||
| 535 | * TODO: check if this method is not useless |
||
| 536 | * |
||
| 537 | * @param RemoteInstance $remote |
||
| 538 | * @param string $update |
||
| 539 | * |
||
| 540 | * @throws RemoteUidException |
||
| 541 | */ |
||
| 542 | public function update(RemoteInstance $remote, string $update = self::UPDATE_DATA): void { |
||
| 561 | |||
| 562 | } |
||
| 563 | |||
| 564 |