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:
| 1 | <?php |
||
| 46 | class FederatedService { |
||
| 47 | |||
| 48 | |||
| 49 | /** @var string */ |
||
| 50 | private $userId; |
||
| 51 | |||
| 52 | /** @var IL10N */ |
||
| 53 | private $l10n; |
||
| 54 | |||
| 55 | /** @var CirclesRequest */ |
||
| 56 | private $circlesRequest; |
||
| 57 | |||
| 58 | /** @var ConfigService */ |
||
| 59 | private $configService; |
||
| 60 | |||
| 61 | /** @var CirclesService */ |
||
| 62 | private $circlesService; |
||
| 63 | |||
| 64 | /** @var BroadcastService */ |
||
| 65 | private $broadcastService; |
||
| 66 | |||
| 67 | /** @var FederatedLinksRequest */ |
||
| 68 | private $federatedLinksRequest; |
||
| 69 | |||
| 70 | /** @var string */ |
||
| 71 | private $serverHost; |
||
| 72 | |||
| 73 | /** @var ClientService */ |
||
| 74 | private $clientService; |
||
| 75 | |||
| 76 | /** @var MiscService */ |
||
| 77 | private $miscService; |
||
| 78 | |||
| 79 | |||
| 80 | /** |
||
| 81 | * CirclesService constructor. |
||
| 82 | * |
||
| 83 | * @param $userId |
||
| 84 | * @param IL10N $l10n |
||
| 85 | * @param CirclesRequest $circlesRequest |
||
| 86 | * @param ConfigService $configService |
||
| 87 | * @param CirclesService $circlesService |
||
| 88 | * @param BroadcastService $broadcastService |
||
| 89 | * @param FederatedLinksRequest $federatedLinksRequest |
||
| 90 | * @param string $serverHost |
||
| 91 | * @param ClientService $clientService |
||
| 92 | * @param MiscService $miscService |
||
| 93 | */ |
||
| 94 | public function __construct( |
||
| 95 | $userId, |
||
| 96 | IL10N $l10n, |
||
| 97 | CirclesRequest $circlesRequest, |
||
| 98 | ConfigService $configService, |
||
| 99 | CirclesService $circlesService, |
||
| 100 | BroadcastService $broadcastService, |
||
| 101 | FederatedLinksRequest $federatedLinksRequest, |
||
| 102 | $serverHost, |
||
| 103 | ClientService $clientService, |
||
| 104 | MiscService $miscService |
||
| 105 | ) { |
||
| 106 | $this->userId = $userId; |
||
| 107 | $this->l10n = $l10n; |
||
| 108 | $this->circlesRequest = $circlesRequest; |
||
| 109 | $this->configService = $configService; |
||
| 110 | $this->circlesService = $circlesService; |
||
| 111 | $this->broadcastService = $broadcastService; |
||
| 112 | $this->federatedLinksRequest = $federatedLinksRequest; |
||
| 113 | $this->serverHost = (string)$serverHost; |
||
| 114 | $this->clientService = $clientService; |
||
| 115 | $this->miscService = $miscService; |
||
| 116 | } |
||
| 117 | |||
| 118 | |||
| 119 | /** |
||
| 120 | * linkCircle() |
||
| 121 | * |
||
| 122 | * link to a circle. |
||
| 123 | * Function will check if settings allow Federated links between circles, and the format of |
||
| 124 | * the link ($remote). If no exception, a request to the remote circle will be initiated |
||
| 125 | * using requestLinkWithCircle() |
||
| 126 | * |
||
| 127 | * $remote format: <circle_name>@<remote_host> |
||
| 128 | * |
||
| 129 | * @param int $circleId |
||
| 130 | * @param string $remote |
||
| 131 | * |
||
| 132 | * @throws Exception |
||
| 133 | * @throws FederatedCircleLinkFormatException |
||
| 134 | * @throws CircleTypeNotValid |
||
| 135 | * @throws MemberIsNotAdminException |
||
| 136 | * |
||
| 137 | * @return FederatedLink |
||
| 138 | */ |
||
| 139 | public function linkCircle($circleId, $remote) { |
||
| 140 | |||
| 141 | if (!$this->configService->isFederatedAllowed()) { |
||
| 142 | throw new FederatedCircleNotAllowedException( |
||
| 143 | $this->l10n->t("Federated circles are not allowed on this Nextcloud") |
||
| 144 | ); |
||
| 145 | } |
||
| 146 | |||
| 147 | if (strpos($remote, '@') === false) { |
||
| 148 | throw new FederatedCircleLinkFormatException( |
||
| 149 | $this->l10n->t("Federated link does not have a valid format") |
||
| 150 | ); |
||
| 151 | } |
||
| 152 | |||
| 153 | try { |
||
| 154 | return $this->requestLinkWithCircle($circleId, $remote); |
||
| 155 | } catch (Exception $e) { |
||
| 156 | throw $e; |
||
| 157 | } |
||
| 158 | } |
||
| 159 | |||
| 160 | |||
| 161 | /** |
||
| 162 | * requestLinkWithCircle() |
||
| 163 | * |
||
| 164 | * Using CircleId, function will get more infos from the database. |
||
| 165 | * Will check if author is not admin and initiate a FederatedLink, save it |
||
| 166 | * in the database and send a request to the remote circle using requestLink() |
||
| 167 | * If any issue, entry is removed from the database. |
||
| 168 | * |
||
| 169 | * @param integer $circleId |
||
| 170 | * @param string $remote |
||
| 171 | * |
||
| 172 | * @return FederatedLink |
||
| 173 | * @throws Exception |
||
| 174 | */ |
||
| 175 | private function requestLinkWithCircle($circleId, $remote) { |
||
| 176 | |||
| 177 | $link = null; |
||
| 178 | try { |
||
| 179 | list($remoteCircle, $remoteAddress) = explode('@', $remote, 2); |
||
| 180 | |||
| 181 | $circle = $this->circlesService->detailsCircle($circleId); |
||
| 182 | $circle->getUser() |
||
| 183 | ->hasToBeAdmin(); |
||
| 184 | $circle->cantBePersonal(); |
||
| 185 | |||
| 186 | $link = new FederatedLink(); |
||
| 187 | $link->setCircleId($circleId) |
||
| 188 | ->setLocalAddress($this->serverHost) |
||
| 189 | ->setAddress($remoteAddress) |
||
| 190 | ->setRemoteCircleName($remoteCircle) |
||
| 191 | ->setStatus(FederatedLink::STATUS_LINK_SETUP) |
||
| 192 | ->generateToken(); |
||
| 193 | |||
| 194 | $this->federatedLinksRequest->create($link); |
||
| 195 | $this->requestLink($circle, $link); |
||
| 196 | |||
| 197 | } catch (Exception $e) { |
||
| 198 | $this->federatedLinksRequest->delete($link); |
||
|
|
|||
| 199 | throw $e; |
||
| 200 | } |
||
| 201 | |||
| 202 | return $link; |
||
| 203 | } |
||
| 204 | |||
| 205 | |||
| 206 | /** |
||
| 207 | * @param string $remote |
||
| 208 | * |
||
| 209 | * @return string |
||
| 210 | */ |
||
| 211 | View Code Duplication | private function generateLinkRemoteURL($remote) { |
|
| 218 | |||
| 219 | /** |
||
| 220 | * @param string $remote |
||
| 221 | * |
||
| 222 | * @return string |
||
| 223 | */ |
||
| 224 | View Code Duplication | private function generatePayloadDeliveryURL($remote) { |
|
| 231 | |||
| 232 | |||
| 233 | /** |
||
| 234 | * requestLink() |
||
| 235 | * |
||
| 236 | * |
||
| 237 | * @param Circle $circle |
||
| 238 | * @param FederatedLink $link |
||
| 239 | * |
||
| 240 | * @return boolean |
||
| 241 | * @throws Exception |
||
| 242 | */ |
||
| 243 | private function requestLink(Circle $circle, FederatedLink & $link) { |
||
| 278 | |||
| 279 | |||
| 280 | private function parsingRequestLinkResult($result) { |
||
| 308 | |||
| 309 | |||
| 310 | /** |
||
| 311 | * Create a new link into database and assign the correct status. |
||
| 312 | * |
||
| 313 | * @param Circle $circle |
||
| 314 | * @param FederatedLink $link |
||
| 315 | * |
||
| 316 | * @throws Exception |
||
| 317 | */ |
||
| 318 | public function initiateLink(Circle $circle, FederatedLink & $link) { |
||
| 335 | |||
| 336 | |||
| 337 | /** |
||
| 338 | * @param Circle $circle |
||
| 339 | * @param FederatedLink $link |
||
| 340 | * |
||
| 341 | * @throws LinkCreationException |
||
| 342 | */ |
||
| 343 | private function checkLinkRequestValidity($circle, $link) { |
||
| 357 | |||
| 358 | |||
| 359 | /** |
||
| 360 | * @param string $token |
||
| 361 | * @param string $uniqueId |
||
| 362 | * @param SharingFrame $frame |
||
| 363 | * |
||
| 364 | * @return bool |
||
| 365 | * @throws Exception |
||
| 366 | */ |
||
| 367 | public function receiveFrame($token, $uniqueId, SharingFrame & $frame) { |
||
| 395 | |||
| 396 | /** |
||
| 397 | * @param integer $circleId |
||
| 398 | * @param string $uniqueId |
||
| 399 | * |
||
| 400 | * @return FederatedLink |
||
| 401 | */ |
||
| 402 | public function getLink($circleId, $uniqueId) { |
||
| 405 | |||
| 406 | |||
| 407 | /** |
||
| 408 | * @param integer $circleId |
||
| 409 | * |
||
| 410 | * @return FederatedLink[] |
||
| 411 | */ |
||
| 412 | public function getLinks($circleId) { |
||
| 415 | |||
| 416 | |||
| 417 | /** |
||
| 418 | * @param int $circleId |
||
| 419 | * @param string $uniqueId |
||
| 420 | * |
||
| 421 | * @return bool |
||
| 422 | * @throws Exception |
||
| 423 | */ |
||
| 424 | public function initiateRemoteShare($circleId, $uniqueId) { |
||
| 450 | |||
| 451 | |||
| 452 | /** |
||
| 453 | * @param SharingFrame $frame |
||
| 454 | * |
||
| 455 | * @throws Exception |
||
| 456 | */ |
||
| 457 | public function sendRemoteShare(SharingFrame $frame) { |
||
| 487 | |||
| 488 | |||
| 489 | /** |
||
| 490 | * generateHeaders() |
||
| 491 | * |
||
| 492 | * Generate new headers for the current Payload, and save them in the SharingFrame. |
||
| 493 | * |
||
| 494 | * @param SharingFrame $frame |
||
| 495 | */ |
||
| 496 | public function updateFrameWithCloudId(SharingFrame $frame) { |
||
| 500 | |||
| 501 | } |
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: