Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like FederatedService 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 FederatedService, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 54 | class FederatedService { |
||
| 55 | |||
| 56 | const REMOTE_URL_LINK = '/index.php/apps/circles/v1/link'; |
||
| 57 | const REMOTE_URL_PAYLOAD = '/index.php/apps/circles/v1/payload'; |
||
| 58 | |||
| 59 | /** @var string */ |
||
| 60 | private $userId; |
||
| 61 | |||
| 62 | /** @var IL10N */ |
||
| 63 | private $l10n; |
||
| 64 | |||
| 65 | /** @var CirclesRequest */ |
||
| 66 | private $circlesRequest; |
||
| 67 | |||
| 68 | /** @var ConfigService */ |
||
| 69 | private $configService; |
||
| 70 | |||
| 71 | /** @var CirclesService */ |
||
| 72 | private $circlesService; |
||
| 73 | |||
| 74 | /** @var BroadcastService */ |
||
| 75 | private $broadcastService; |
||
| 76 | |||
| 77 | /** @var FederatedLinksRequest */ |
||
| 78 | private $federatedLinksRequest; |
||
| 79 | |||
| 80 | /** @var EventsService */ |
||
| 81 | private $eventsService; |
||
| 82 | |||
| 83 | /** @var string */ |
||
| 84 | private $serverHost; |
||
| 85 | |||
| 86 | /** @var ClientService */ |
||
| 87 | private $clientService; |
||
| 88 | |||
| 89 | /** @var MiscService */ |
||
| 90 | private $miscService; |
||
| 91 | |||
| 92 | |||
| 93 | /** |
||
| 94 | * CirclesService constructor. |
||
| 95 | * |
||
| 96 | * @param $userId |
||
| 97 | * @param IL10N $l10n |
||
| 98 | * @param CirclesRequest $circlesRequest |
||
| 99 | * @param ConfigService $configService |
||
| 100 | * @param CirclesService $circlesService |
||
| 101 | * @param BroadcastService $broadcastService |
||
| 102 | * @param FederatedLinksRequest $federatedLinksRequest |
||
| 103 | * @param EventsService $eventsService |
||
| 104 | * @param ClientService $clientService |
||
| 105 | * @param MiscService $miscService |
||
| 106 | */ |
||
| 107 | public function __construct( |
||
| 108 | $userId, |
||
| 109 | IL10N $l10n, |
||
| 110 | CirclesRequest $circlesRequest, |
||
| 111 | ConfigService $configService, |
||
| 112 | CirclesService $circlesService, |
||
| 113 | BroadcastService $broadcastService, |
||
| 114 | FederatedLinksRequest $federatedLinksRequest, |
||
| 115 | EventsService $eventsService, |
||
| 116 | ClientService $clientService, |
||
| 117 | MiscService $miscService |
||
| 118 | ) { |
||
| 119 | $this->userId = $userId; |
||
| 120 | $this->l10n = $l10n; |
||
| 121 | $this->circlesRequest = $circlesRequest; |
||
| 122 | $this->configService = $configService; |
||
| 123 | $this->circlesService = $circlesService; |
||
| 124 | $this->broadcastService = $broadcastService; |
||
| 125 | $this->federatedLinksRequest = $federatedLinksRequest; |
||
| 126 | $this->eventsService = $eventsService; |
||
| 127 | $this->serverHost = $this->configService->getLocalAddress(); |
||
| 128 | |||
| 129 | |||
| 130 | $this->clientService = $clientService; |
||
| 131 | $this->miscService = $miscService; |
||
| 132 | } |
||
| 133 | |||
| 134 | |||
| 135 | /** |
||
| 136 | * linkCircle(); |
||
| 137 | * |
||
| 138 | * link to a circle. |
||
| 139 | * Function will check if settings allow Federated links between circles, and the format of |
||
| 140 | * the link ($remote). If no exception, a request to the remote circle will be initiated |
||
| 141 | * using requestLinkWithCircle() |
||
| 142 | * |
||
| 143 | * $remote format: <circle_name>@<remote_host> |
||
| 144 | * |
||
| 145 | * @param string $circleUniqueId |
||
| 146 | * @param string $remote |
||
| 147 | * |
||
| 148 | * @throws Exception |
||
| 149 | * @throws FederatedCircleLinkFormatException |
||
| 150 | * @throws CircleTypeNotValidException |
||
| 151 | * |
||
| 152 | * @return FederatedLink |
||
| 153 | */ |
||
| 154 | public function linkCircle($circleUniqueId, $remote) { |
||
| 174 | |||
| 175 | |||
| 176 | /** |
||
| 177 | * linkStatus() |
||
| 178 | * |
||
| 179 | * Update the status of a link. |
||
| 180 | * Function will check if user can edit the status, will update it and send the update to |
||
| 181 | * remote |
||
| 182 | * |
||
| 183 | * @param int $linkId |
||
| 184 | * @param int $status |
||
| 185 | * |
||
| 186 | * @throws Exception |
||
| 187 | * @throws FederatedCircleLinkFormatException |
||
| 188 | * @throws CircleTypeNotValidException |
||
| 189 | * @throws MemberIsNotAdminException |
||
| 190 | * |
||
| 191 | * @return FederatedLink[] |
||
| 192 | */ |
||
| 193 | public function linkStatus($linkId, $status) { |
||
| 194 | |||
| 195 | $status = (int)$status; |
||
| 196 | $link = null; |
||
| 197 | try { |
||
| 198 | |||
| 199 | $link = $this->circlesRequest->getLinkFromId($linkId); |
||
| 200 | $circle = $this->circlesRequest->getCircle($link->getCircleId(), $this->userId); |
||
| 201 | $circle->hasToBeFederated(); |
||
| 202 | $circle->getHigherViewer() |
||
| 203 | ->hasToBeAdmin(); |
||
| 204 | $link->hasToBeValidStatusUpdate($status); |
||
| 205 | |||
| 206 | if (!$this->eventOnLinkStatus($circle, $link, $status)) { |
||
| 207 | return $this->circlesRequest->getLinksFromCircle($circle->getUniqueId()); |
||
| 208 | } |
||
| 209 | |||
| 210 | } catch (Exception $e) { |
||
| 211 | throw $e; |
||
| 212 | } |
||
| 213 | |||
| 214 | $link->setStatus($status); |
||
| 215 | $link->setCircleId($circle->getUniqueId(true)); |
||
| 216 | |||
| 217 | try { |
||
| 218 | $this->updateLinkRemote($link); |
||
| 219 | } catch (Exception $e) { |
||
| 220 | if ($status !== FederatedLink::STATUS_LINK_REMOVE) { |
||
| 221 | throw $e; |
||
| 222 | } |
||
| 223 | } |
||
| 224 | |||
| 225 | $this->federatedLinksRequest->update($link); |
||
| 226 | |||
| 227 | return $this->circlesRequest->getLinksFromCircle($circle->getUniqueId()); |
||
| 228 | } |
||
| 229 | |||
| 230 | |||
| 231 | /** |
||
| 232 | * eventOnLinkStatus(); |
||
| 233 | * |
||
| 234 | * Called by linkStatus() to manage events when status is changing. |
||
| 235 | * If status does not need update, returns false; |
||
| 236 | * |
||
| 237 | * @param Circle $circle |
||
| 238 | * @param FederatedLink $link |
||
| 239 | * @param $status |
||
| 240 | * |
||
| 241 | * @return bool |
||
| 242 | */ |
||
| 243 | private function eventOnLinkStatus(Circle $circle, FederatedLink $link, $status) { |
||
| 259 | |||
| 260 | |||
| 261 | /** |
||
| 262 | * requestLinkWithCircle() |
||
| 263 | * |
||
| 264 | * Using CircleId, function will get more infos from the database. |
||
| 265 | * Will check if author is at least admin and initiate a FederatedLink, save it |
||
| 266 | * in the database and send a request to the remote circle using requestLink() |
||
| 267 | * If any issue, entry is removed from the database. |
||
| 268 | * |
||
| 269 | * @param string $circleUniqueId |
||
| 270 | * @param string $remote |
||
| 271 | * |
||
| 272 | * @return FederatedLink |
||
| 273 | * @throws Exception |
||
| 274 | */ |
||
| 275 | private function requestLinkWithCircle($circleUniqueId, $remote) { |
||
| 307 | |||
| 308 | |||
| 309 | /** |
||
| 310 | * @param string $remote |
||
| 311 | * |
||
| 312 | * @return string |
||
| 313 | */ |
||
| 314 | private function generateLinkRemoteURL($remote) { |
||
| 315 | return $this->generateRemoteHost($remote) . self::REMOTE_URL_LINK; |
||
| 316 | } |
||
| 317 | |||
| 318 | |||
| 319 | /** |
||
| 320 | * @param string $remote |
||
| 321 | * |
||
| 322 | * @return string |
||
| 323 | */ |
||
| 324 | private function generatePayloadDeliveryURL($remote) { |
||
| 325 | return $this->generateRemoteHost($remote) . self::REMOTE_URL_PAYLOAD; |
||
| 326 | } |
||
| 327 | |||
| 328 | |||
| 329 | /** |
||
| 330 | * @param string $remote |
||
| 331 | * |
||
| 332 | * @return string |
||
| 333 | */ |
||
| 334 | private function generateRemoteHost($remote) { |
||
| 343 | |||
| 344 | |||
| 345 | /** |
||
| 346 | * requestLink() |
||
| 347 | * |
||
| 348 | * |
||
| 349 | * @param Circle $circle |
||
| 350 | * @param FederatedLink $link |
||
| 351 | * |
||
| 352 | * @return boolean |
||
| 353 | * @throws Exception |
||
| 354 | */ |
||
| 355 | private function requestLink(Circle $circle, FederatedLink &$link) { |
||
| 356 | $args = [ |
||
| 357 | 'apiVersion' => Circles::version(), |
||
| 358 | 'token' => $link->getToken(true), |
||
| 359 | 'uniqueId' => $circle->getUniqueId(true), |
||
| 360 | 'sourceName' => $circle->getName(), |
||
| 361 | 'linkTo' => $link->getRemoteCircleName(), |
||
| 362 | 'address' => $link->getLocalAddress() |
||
| 363 | ]; |
||
| 364 | |||
| 365 | $client = $this->clientService->newClient(); |
||
| 366 | |||
| 367 | try { |
||
| 368 | $request = $client->put( |
||
| 369 | $this->generateLinkRemoteURL($link->getAddress()), [ |
||
| 370 | 'body' => $args, |
||
| 371 | 'timeout' => 10, |
||
| 372 | 'connect_timeout' => 10, |
||
| 373 | ] |
||
| 374 | ); |
||
| 375 | |||
| 376 | $result = json_decode($request->getBody(), true); |
||
| 377 | if ($result === null) { |
||
| 378 | throw new FederatedRemoteIsDownException( |
||
| 379 | $this->l10n->t( |
||
| 380 | 'The remote host is down or the Circles app is not installed on it' |
||
| 381 | ) |
||
| 382 | ); |
||
| 383 | } |
||
| 384 | |||
| 385 | $this->eventOnRequestLink( |
||
| 386 | $circle, $link, $result['status'], |
||
| 387 | ((key_exists('reason', $result)) ? $result['reason'] : '') |
||
| 388 | ); |
||
| 389 | |||
| 390 | $link->setUniqueId($result['uniqueId']); |
||
| 391 | $this->federatedLinksRequest->update($link); |
||
| 392 | |||
| 393 | return true; |
||
| 394 | } catch (Exception $e) { |
||
| 395 | throw $e; |
||
| 396 | } |
||
| 397 | } |
||
| 398 | |||
| 399 | |||
| 400 | /** |
||
| 401 | * eventOnRequestLink(); |
||
| 402 | * |
||
| 403 | * Called by requestLink() will update status and event |
||
| 404 | * Will also manage errors returned by the remote link |
||
| 405 | * |
||
| 406 | * @param Circle $circle |
||
| 407 | * @param FederatedLink $link |
||
| 408 | * @param $status |
||
| 409 | * @param $reason |
||
| 410 | * |
||
| 411 | * @throws Exception |
||
| 412 | */ |
||
| 413 | private function eventOnRequestLink(Circle $circle, FederatedLink $link, $status, $reason) { |
||
| 429 | |||
| 430 | |||
| 431 | /** |
||
| 432 | * parseRequestLinkError(); |
||
| 433 | * |
||
| 434 | * Will parse the error reason returned by requestLink() and throw an Exception |
||
| 435 | * |
||
| 436 | * @param $reason |
||
| 437 | * |
||
| 438 | * @throws Exception |
||
| 439 | * @throws FederatedRemoteCircleDoesNotExistException |
||
| 440 | * @throws FederatedRemoteDoesNotAllowException |
||
| 441 | */ |
||
| 442 | private function parseRequestLinkError($reason) { |
||
| 476 | |||
| 477 | |||
| 478 | /** |
||
| 479 | * @param string $token |
||
| 480 | * @param string $uniqueId |
||
| 481 | * @param int $status |
||
| 482 | * |
||
| 483 | * @return FederatedLink |
||
| 484 | * @throws Exception |
||
| 485 | */ |
||
| 486 | public function updateLinkFromRemote($token, $uniqueId, $status) { |
||
| 505 | |||
| 506 | /** |
||
| 507 | * checkUpdateLinkFromRemote(); |
||
| 508 | * |
||
| 509 | * will throw exception is the status sent by remote is not correct |
||
| 510 | * |
||
| 511 | * @param int $status |
||
| 512 | * |
||
| 513 | * @throws FederatedCircleStatusUpdateException |
||
| 514 | */ |
||
| 515 | private function checkUpdateLinkFromRemote($status) { |
||
| 525 | |||
| 526 | |||
| 527 | /** |
||
| 528 | * checkUpdateLinkFromRemoteLinkUp() |
||
| 529 | * |
||
| 530 | * in case of a request of status update from remote for a link up, we check the current |
||
| 531 | * status of the link locally. |
||
| 532 | * |
||
| 533 | * @param Circle $circle |
||
| 534 | * @param FederatedLink $link |
||
| 535 | * @param int $status |
||
| 536 | * |
||
| 537 | * @throws FederatedCircleStatusUpdateException |
||
| 538 | */ |
||
| 539 | private function checkUpdateLinkFromRemoteLinkUp(Circle $circle, FederatedLink $link, $status) { |
||
| 554 | |||
| 555 | |||
| 556 | /** |
||
| 557 | * checkUpdateLinkFromRemoteLinkRemove(); |
||
| 558 | * |
||
| 559 | * in case of a request of status update from remote for a link down, we check the current |
||
| 560 | * status of the link locally |
||
| 561 | * |
||
| 562 | * @param Circle $circle |
||
| 563 | * @param FederatedLink $link |
||
| 564 | * @param int $status |
||
| 565 | * |
||
| 566 | * @throws FederatedCircleStatusUpdateException |
||
| 567 | */ |
||
| 568 | private function checkUpdateLinkFromRemoteLinkRemove( |
||
| 600 | |||
| 601 | |||
| 602 | /** |
||
| 603 | * updateLinkRemote() |
||
| 604 | * |
||
| 605 | * Send a request to the remote of the link to update its status. |
||
| 606 | * |
||
| 607 | * @param FederatedLink $link |
||
| 608 | * |
||
| 609 | * @return bool |
||
| 610 | * @throws Exception |
||
| 611 | */ |
||
| 612 | public function updateLinkRemote(FederatedLink &$link) { |
||
| 640 | |||
| 641 | |||
| 642 | /** |
||
| 643 | * Create a new link into database and assign the correct status. |
||
| 644 | * |
||
| 645 | * @param Circle $circle |
||
| 646 | * @param FederatedLink $link |
||
| 647 | * |
||
| 648 | * @throws Exception |
||
| 649 | */ |
||
| 650 | public function initiateLink(Circle $circle, FederatedLink &$link) { |
||
| 669 | |||
| 670 | |||
| 671 | /** |
||
| 672 | * @param Circle $circle |
||
| 673 | * @param FederatedLink $link |
||
| 674 | * |
||
| 675 | * @throws FederatedLinkCreationException |
||
| 676 | */ |
||
| 677 | private function checkLinkRequestValidity($circle, $link) { |
||
| 690 | |||
| 691 | |||
| 692 | /** |
||
| 693 | * @param string $token |
||
| 694 | * @param string $uniqueId |
||
| 695 | * @param SharingFrame $frame |
||
| 696 | * |
||
| 697 | * @return bool |
||
| 698 | * @throws Exception |
||
| 699 | */ |
||
| 700 | public function receiveFrame($token, $uniqueId, SharingFrame &$frame) { |
||
| 724 | |||
| 725 | /** |
||
| 726 | * @param string $circleUniqueId |
||
| 727 | * @param string $uniqueId |
||
| 728 | * |
||
| 729 | * @return FederatedLink |
||
| 730 | */ |
||
| 731 | public function getLink($circleUniqueId, $uniqueId) { |
||
| 734 | |||
| 735 | |||
| 736 | /** |
||
| 737 | * @param string $circleUniqueId |
||
| 738 | * |
||
| 739 | * @return FederatedLink[] |
||
| 740 | */ |
||
| 741 | public function getLinksFromCircle($circleUniqueId) { |
||
| 744 | |||
| 745 | |||
| 746 | /** |
||
| 747 | * @param string $circleUniqueId |
||
| 748 | * @param string $frameUniqueId |
||
| 749 | * |
||
| 750 | * @return bool |
||
| 751 | * @throws Exception |
||
| 752 | */ |
||
| 753 | public function initiateShare($circleUniqueId, $frameUniqueId) { |
||
| 779 | |||
| 780 | |||
| 781 | /** |
||
| 782 | * @param SharingFrame $frame |
||
| 783 | * |
||
| 784 | * @throws Exception |
||
| 785 | */ |
||
| 786 | public function sendRemoteShare(SharingFrame $frame) { |
||
| 835 | |||
| 836 | |||
| 837 | /** |
||
| 838 | * generateHeaders() |
||
| 839 | * |
||
| 840 | * Generate new headers for the current Payload, and save them in the SharingFrame. |
||
| 841 | * |
||
| 842 | * @param SharingFrame $frame |
||
| 843 | */ |
||
| 844 | public function updateFrameWithCloudId(SharingFrame $frame) { |
||
| 848 | |||
| 849 | } |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.