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 |
||
| 47 | class FederatedService { |
||
| 48 | |||
| 49 | |||
| 50 | /** @var string */ |
||
| 51 | private $userId; |
||
| 52 | |||
| 53 | /** @var IL10N */ |
||
| 54 | private $l10n; |
||
| 55 | |||
| 56 | /** @var ConfigService */ |
||
| 57 | private $configService; |
||
| 58 | |||
| 59 | /** @var CirclesService */ |
||
| 60 | private $circlesService; |
||
| 61 | |||
| 62 | /** @var FederatedLinksRequest */ |
||
| 63 | private $federatedLinksRequest; |
||
| 64 | |||
| 65 | /** @var CirclesMapper */ |
||
| 66 | private $dbCircles; |
||
| 67 | |||
| 68 | /** @var MembersMapper */ |
||
| 69 | private $dbMembers; |
||
| 70 | |||
| 71 | /** @var ClientService */ |
||
| 72 | private $clientService; |
||
| 73 | |||
| 74 | /** @var MiscService */ |
||
| 75 | private $miscService; |
||
| 76 | |||
| 77 | |||
| 78 | /** |
||
| 79 | * CirclesService constructor. |
||
| 80 | * |
||
| 81 | * @param $userId |
||
| 82 | * @param IL10N $l10n |
||
| 83 | * @param ConfigService $configService |
||
| 84 | * @param DatabaseService $databaseService |
||
| 85 | * @param CirclesService $circlesService |
||
| 86 | * @param FederatedLinksRequest $federatedLinksRequest |
||
| 87 | * @param string $serverHost |
||
| 88 | * @param ClientService $clientService |
||
| 89 | * @param MiscService $miscService |
||
| 90 | */ |
||
| 91 | public function __construct( |
||
| 92 | $userId, |
||
| 93 | IL10N $l10n, |
||
| 94 | ConfigService $configService, |
||
| 95 | DatabaseService $databaseService, |
||
| 96 | CirclesService $circlesService, |
||
| 97 | FederatedLinksRequest $federatedLinksRequest, |
||
| 98 | string $serverHost, |
||
| 99 | ClientService $clientService, |
||
| 100 | MiscService $miscService |
||
| 101 | ) { |
||
| 102 | $this->userId = $userId; |
||
| 103 | $this->l10n = $l10n; |
||
| 104 | $this->configService = $configService; |
||
| 105 | $this->circlesService = $circlesService; |
||
| 106 | $this->federatedLinksRequest = $federatedLinksRequest; |
||
| 107 | $this->serverHost = $serverHost; |
||
|
|
|||
| 108 | $this->clientService = $clientService; |
||
| 109 | |||
| 110 | $this->miscService = $miscService; |
||
| 111 | |||
| 112 | $this->dbCircles = $databaseService->getCirclesMapper(); |
||
| 113 | $this->dbMembers = $databaseService->getMembersMapper(); |
||
| 114 | } |
||
| 115 | |||
| 116 | |||
| 117 | /** |
||
| 118 | * linkCircle() |
||
| 119 | * |
||
| 120 | * link to a circle. |
||
| 121 | * Function will check if settings allow Federated links between circles, and the format of |
||
| 122 | * the link ($remote). If no exception, a request to the remote circle will be initiated |
||
| 123 | * using requestLinkWithCircle() |
||
| 124 | * |
||
| 125 | * $remote format: <circle_name>@<remote_host> |
||
| 126 | * |
||
| 127 | * @param int $circleId |
||
| 128 | * @param string $remote |
||
| 129 | * |
||
| 130 | * @throws Exception |
||
| 131 | * @throws FederatedCircleLinkFormatException |
||
| 132 | * @throws CircleTypeNotValid |
||
| 133 | * @throws MemberIsNotAdminException |
||
| 134 | * |
||
| 135 | * @return FederatedLink |
||
| 136 | */ |
||
| 137 | public function linkCircle($circleId, $remote) { |
||
| 138 | |||
| 139 | if (!$this->configService->isFederatedAllowed()) { |
||
| 140 | throw new FederatedCircleNotAllowedException( |
||
| 141 | $this->l10n->t("Federated circles are not allowed on this Nextcloud") |
||
| 142 | ); |
||
| 143 | } |
||
| 144 | |||
| 145 | if (strpos($remote, '@') === false) { |
||
| 146 | throw new FederatedCircleLinkFormatException( |
||
| 147 | $this->l10n->t("Federated link does not have a valid format") |
||
| 148 | ); |
||
| 149 | } |
||
| 150 | |||
| 151 | try { |
||
| 152 | return $this->requestLinkWithCircle($circleId, $remote); |
||
| 153 | } catch (Exception $e) { |
||
| 154 | throw $e; |
||
| 155 | } |
||
| 156 | } |
||
| 157 | |||
| 158 | |||
| 159 | /** |
||
| 160 | * requestLinkWithCircle() |
||
| 161 | * |
||
| 162 | * Using CircleId, function will get more infos from the database. |
||
| 163 | * Will check if author is not admin and initiate a FederatedLink, save it |
||
| 164 | * in the database and send a request to the remote circle using requestLink() |
||
| 165 | * If any issue, entry is removed from the database. |
||
| 166 | * |
||
| 167 | * @param $circleId |
||
| 168 | * @param $remote |
||
| 169 | * |
||
| 170 | * @return FederatedLink |
||
| 171 | * @throws Exception |
||
| 172 | */ |
||
| 173 | private function requestLinkWithCircle($circleId, $remote) { |
||
| 174 | |||
| 175 | $link = null; |
||
| 176 | try { |
||
| 177 | list($remoteCircle, $remoteAddress) = explode('@', $remote, 2); |
||
| 178 | |||
| 179 | $circle = $this->circlesService->detailsCircle($circleId); |
||
| 180 | $circle->getUser() |
||
| 181 | ->hasToBeAdmin(); |
||
| 182 | $circle->cantBePersonal(); |
||
| 183 | |||
| 184 | $link = new FederatedLink(); |
||
| 185 | $link->setCircleId($circleId) |
||
| 186 | ->setLocalAddress($this->serverHost) |
||
| 187 | ->setAddress($remoteAddress) |
||
| 188 | ->setRemoteCircleName($remoteCircle) |
||
| 189 | ->setStatus(FederatedLink::STATUS_LINK_SETUP) |
||
| 190 | ->generateToken(); |
||
| 191 | |||
| 192 | $this->federatedLinksRequest->create($link); |
||
| 193 | $this->requestLink($circle, $link); |
||
| 194 | |||
| 195 | } catch (Exception $e) { |
||
| 196 | $this->federatedLinksRequest->delete($link); |
||
| 197 | throw $e; |
||
| 198 | } |
||
| 199 | |||
| 200 | return $link; |
||
| 201 | } |
||
| 202 | |||
| 203 | |||
| 204 | /** |
||
| 205 | * @param string $remote |
||
| 206 | * |
||
| 207 | * @return string |
||
| 208 | */ |
||
| 209 | View Code Duplication | private function generateLinkRemoteURL($remote) { |
|
| 210 | if (strpos($remote, 'http') !== 0) { |
||
| 211 | $remote = 'https://' . $remote; |
||
| 212 | } |
||
| 213 | |||
| 214 | return rtrim($remote, '/') . '/index.php/apps/circles/circles/link/'; |
||
| 215 | } |
||
| 216 | |||
| 217 | /** |
||
| 218 | * @param string $remote |
||
| 219 | * |
||
| 220 | * @return string |
||
| 221 | */ |
||
| 222 | View Code Duplication | private function generatePayloadDeliveryURL($remote) { |
|
| 223 | if (strpos($remote, 'http') !== 0) { |
||
| 224 | $remote = 'http://' . $remote; |
||
| 225 | } |
||
| 226 | |||
| 227 | return rtrim($remote, '/') . '/index.php/apps/circles/circles/payload/'; |
||
| 228 | } |
||
| 229 | |||
| 230 | |||
| 231 | /** |
||
| 232 | * requestLink() |
||
| 233 | * |
||
| 234 | * |
||
| 235 | * @param Circle $circle |
||
| 236 | * @param FederatedLink $link |
||
| 237 | * |
||
| 238 | * @return int |
||
| 239 | * @throws Exception |
||
| 240 | */ |
||
| 241 | private function requestLink(Circle $circle, FederatedLink &$link) { |
||
| 242 | $args = [ |
||
| 243 | 'token' => $link->getToken(), |
||
| 244 | 'uniqueId' => $circle->getUniqueId(), |
||
| 245 | 'sourceName' => $circle->getName(), |
||
| 246 | 'linkTo' => $link->getRemoteCircleName(), |
||
| 247 | 'address' => $link->getLocalAddress() |
||
| 248 | ]; |
||
| 249 | |||
| 250 | $client = $this->clientService->newClient(); |
||
| 251 | |||
| 252 | try { |
||
| 253 | $request = $client->put( |
||
| 254 | $this->generateLinkRemoteURL($link->getAddress()), [ |
||
| 255 | 'body' => $args, |
||
| 256 | 'timeout' => 10, |
||
| 257 | 'connect_timeout' => 10, |
||
| 258 | ] |
||
| 259 | ); |
||
| 260 | |||
| 261 | $result = json_decode($request->getBody(), true); |
||
| 262 | |||
| 263 | $link->setStatus($result['status']); |
||
| 264 | if (!$link->isValid()) { |
||
| 265 | $this->parsingRequestLinkResult($result); |
||
| 266 | } |
||
| 267 | |||
| 268 | $link->setUniqueId($result['uniqueId']); |
||
| 269 | $this->federatedLinksRequest->update($link); |
||
| 270 | |||
| 271 | return true; |
||
| 272 | } catch (Exception $e) { |
||
| 273 | throw $e; |
||
| 274 | } |
||
| 275 | } |
||
| 276 | |||
| 277 | |||
| 278 | private function parsingRequestLinkResult($result) { |
||
| 306 | |||
| 307 | |||
| 308 | /** |
||
| 309 | * Create a new link into database and assign the correct status. |
||
| 310 | * |
||
| 311 | * @param Circle $circle |
||
| 312 | * @param FederatedLink $link |
||
| 313 | * |
||
| 314 | * @return bool |
||
| 315 | */ |
||
| 316 | public function initiateLink(Circle $circle, FederatedLink &$link) { |
||
| 328 | |||
| 329 | |||
| 330 | /** |
||
| 331 | * @param $circleId |
||
| 332 | * @param $uniqueId |
||
| 333 | * |
||
| 334 | * @return FederatedLink |
||
| 335 | */ |
||
| 336 | public function getLink($circleId, $uniqueId) { |
||
| 339 | |||
| 340 | |||
| 341 | /** |
||
| 342 | * @param $circleId |
||
| 343 | * |
||
| 344 | * @return FederatedLink[] |
||
| 345 | */ |
||
| 346 | public function getLinks($circleId) { |
||
| 349 | |||
| 350 | |||
| 351 | /** |
||
| 352 | * @param $uniqueId |
||
| 353 | * |
||
| 354 | * @return bool |
||
| 355 | * @throws Exception |
||
| 356 | */ |
||
| 357 | public function initiateRemoteShare($uniqueId) { |
||
| 382 | |||
| 383 | |||
| 384 | /** |
||
| 385 | * @param SharingFrame $frame |
||
| 386 | * |
||
| 387 | * @throws Exception |
||
| 388 | */ |
||
| 389 | public function sendRemoteShare(SharingFrame $frame) { |
||
| 414 | |||
| 415 | } |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: