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 IURLGenerator */ |
||
| 88 | private $urlGenerator; |
||
| 89 | |||
| 90 | /** @var RemoteRequest */ |
||
| 91 | private $remoteRequest; |
||
| 92 | |||
| 93 | /** @var InterfaceService */ |
||
| 94 | private $interfaceService; |
||
| 95 | |||
| 96 | /** @var ConfigService */ |
||
| 97 | private $configService; |
||
| 98 | |||
| 99 | |||
| 100 | /** |
||
| 101 | * RemoteStreamService constructor. |
||
| 102 | * |
||
| 103 | * @param IURLGenerator $urlGenerator |
||
| 104 | * @param RemoteRequest $remoteRequest |
||
| 105 | * @param InterfaceService $interfaceService |
||
| 106 | * @param ConfigService $configService |
||
| 107 | */ |
||
| 108 | public function __construct( |
||
| 121 | |||
| 122 | |||
| 123 | /** |
||
| 124 | * Returns the Signatory model for the Circles app. |
||
| 125 | * Can be signed with a confirmKey. |
||
| 126 | * |
||
| 127 | * @param bool $generate |
||
| 128 | * @param string $confirmKey |
||
| 129 | * |
||
| 130 | * @return RemoteInstance |
||
| 131 | * @throws SignatoryException |
||
| 132 | * @throws UnknownInterfaceException |
||
| 133 | */ |
||
| 134 | public function getAppSignatory(bool $generate = true, string $confirmKey = ''): RemoteInstance { |
||
| 170 | |||
| 171 | |||
| 172 | /** |
||
| 173 | * Reset the Signatory (and the Identity) for the Circles app. |
||
| 174 | */ |
||
| 175 | public function resetAppSignatory(): void { |
||
| 183 | |||
| 184 | |||
| 185 | /** |
||
| 186 | * shortcut to requestRemoteInstance that return result if available, or exception. |
||
| 187 | * |
||
| 188 | * @param string $instance |
||
| 189 | * @param string $item |
||
| 190 | * @param int $type |
||
| 191 | * @param JsonSerializable|null $object |
||
| 192 | * @param array $params |
||
| 193 | * |
||
| 194 | * @return array |
||
| 195 | * @throws RemoteInstanceException |
||
| 196 | * @throws RemoteNotFoundException |
||
| 197 | * @throws RemoteResourceNotFoundException |
||
| 198 | * @throws UnknownRemoteException |
||
| 199 | * @throws FederatedItemException |
||
| 200 | */ |
||
| 201 | public function resultRequestRemoteInstance( |
||
| 226 | |||
| 227 | |||
| 228 | /** |
||
| 229 | * Send a request to a remote instance, based on: |
||
| 230 | * - instance: address as saved in database, |
||
| 231 | * - item: the item to request (incoming, event, ...) |
||
| 232 | * - type: GET, POST |
||
| 233 | * - data: Serializable to be send if needed |
||
| 234 | * |
||
| 235 | * @param string $instance |
||
| 236 | * @param string $item |
||
| 237 | * @param int $type |
||
| 238 | * @param JsonSerializable|null $object |
||
| 239 | * @param array $params |
||
| 240 | * |
||
| 241 | * @return NC22SignedRequest |
||
| 242 | * @throws RemoteNotFoundException |
||
| 243 | * @throws RemoteResourceNotFoundException |
||
| 244 | * @throws UnknownRemoteException |
||
| 245 | * @throws RemoteInstanceException |
||
| 246 | * @throws UnknownInterfaceException |
||
| 247 | */ |
||
| 248 | private function requestRemoteInstance( |
||
| 280 | |||
| 281 | |||
| 282 | /** |
||
| 283 | * get the value of an entry from the Signatory of the RemoteInstance. |
||
| 284 | * |
||
| 285 | * @param string $instance |
||
| 286 | * @param string $item |
||
| 287 | * @param array $params |
||
| 288 | * |
||
| 289 | * @return string |
||
| 290 | * @throws RemoteNotFoundException |
||
| 291 | * @throws RemoteResourceNotFoundException |
||
| 292 | * @throws UnknownRemoteException |
||
| 293 | */ |
||
| 294 | private function getRemoteInstanceEntry(string $instance, string $item, array $params = []): string { |
||
| 304 | |||
| 305 | |||
| 306 | /** |
||
| 307 | * get RemoteInstance with confirmed and known identity from database. |
||
| 308 | * |
||
| 309 | * @param string $instance |
||
| 310 | * |
||
| 311 | * @return RemoteInstance |
||
| 312 | * @throws RemoteNotFoundException |
||
| 313 | * @throws UnknownRemoteException |
||
| 314 | */ |
||
| 315 | public function getCachedRemoteInstance(string $instance): RemoteInstance { |
||
| 323 | |||
| 324 | |||
| 325 | /** |
||
| 326 | * Add a remote instance, based on the address |
||
| 327 | * |
||
| 328 | * @param string $instance |
||
| 329 | * |
||
| 330 | * @return RemoteInstance |
||
| 331 | * @throws RequestNetworkException |
||
| 332 | * @throws SignatoryException |
||
| 333 | * @throws SignatureException |
||
| 334 | * @throws WellKnownLinkNotFoundException |
||
| 335 | */ |
||
| 336 | public function retrieveRemoteInstance(string $instance): RemoteInstance { |
||
| 344 | |||
| 345 | |||
| 346 | /** |
||
| 347 | * retrieve Signatory. |
||
| 348 | * |
||
| 349 | * @param string $keyId |
||
| 350 | * @param bool $refresh |
||
| 351 | * |
||
| 352 | * @return RemoteInstance |
||
| 353 | * @throws SignatoryException |
||
| 354 | * @throws SignatureException |
||
| 355 | */ |
||
| 356 | public function retrieveSignatory(string $keyId, bool $refresh = true): RemoteInstance { |
||
| 378 | |||
| 379 | |||
| 380 | /** |
||
| 381 | * Add a remote instance, based on the address |
||
| 382 | * |
||
| 383 | * @param string $instance |
||
| 384 | * @param string $type |
||
| 385 | * @param int $iface |
||
| 386 | * @param bool $overwrite |
||
| 387 | * |
||
| 388 | * @throws RemoteAlreadyExistsException |
||
| 389 | * @throws RemoteUidException |
||
| 390 | * @throws RequestNetworkException |
||
| 391 | * @throws SignatoryException |
||
| 392 | * @throws SignatureException |
||
| 393 | * @throws WellKnownLinkNotFoundException |
||
| 394 | */ |
||
| 395 | public function addRemoteInstance( |
||
| 421 | |||
| 422 | |||
| 423 | /** |
||
| 424 | * Confirm the Auth of a RemoteInstance, based on the result from a request |
||
| 425 | * |
||
| 426 | * @param RemoteInstance $remote |
||
| 427 | * @param string $auth |
||
| 428 | * |
||
| 429 | * @throws SignatureException |
||
| 430 | */ |
||
| 431 | private function confirmAuth(RemoteInstance $remote, string $auth): void { |
||
| 447 | |||
| 448 | |||
| 449 | /** |
||
| 450 | * @param NC22RequestResult $result |
||
| 451 | * |
||
| 452 | * @return FederatedItemException |
||
| 453 | */ |
||
| 454 | private function getFederatedItemExceptionFromResult(NC22RequestResult $result): FederatedItemException { |
||
| 471 | |||
| 472 | |||
| 473 | /** |
||
| 474 | * @param ReflectionClass $class |
||
| 475 | * |
||
| 476 | * @return void |
||
| 477 | * @throws FederatedItemException |
||
| 478 | */ |
||
| 479 | private function confirmFederatedItemExceptionFromClass(ReflectionClass $class): void { |
||
| 492 | |||
| 493 | |||
| 494 | /** |
||
| 495 | * @param int $statusCode |
||
| 496 | * |
||
| 497 | * @return string |
||
| 498 | */ |
||
| 499 | private function getFederatedItemExceptionFromStatus(int $statusCode): string { |
||
| 508 | |||
| 509 | |||
| 510 | /** |
||
| 511 | * TODO: confirm if method is really needed |
||
| 512 | * |
||
| 513 | * @param RemoteInstance $remote |
||
| 514 | * @param RemoteInstance|null $stored |
||
| 515 | * |
||
| 516 | * @throws RemoteNotFoundException |
||
| 517 | * @throws RemoteUidException |
||
| 518 | */ |
||
| 519 | public function confirmValidRemote(RemoteInstance $remote, ?RemoteInstance &$stored = null): void { |
||
| 534 | |||
| 535 | |||
| 536 | /** |
||
| 537 | * TODO: check if this method is not useless |
||
| 538 | * |
||
| 539 | * @param RemoteInstance $remote |
||
| 540 | * @param string $update |
||
| 541 | * |
||
| 542 | * @throws RemoteUidException |
||
| 543 | */ |
||
| 544 | public function update(RemoteInstance $remote, string $update = self::UPDATE_DATA): void { |
||
| 563 | |||
| 564 | } |
||
| 565 | |||
| 566 |