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 FederatedController extends BaseController { |
||
| 47 | |||
| 48 | /** @var string */ |
||
| 49 | protected $userId; |
||
| 50 | |||
| 51 | /** @var IL10N */ |
||
| 52 | protected $l10n; |
||
| 53 | |||
| 54 | /** @var ConfigService */ |
||
| 55 | protected $configService; |
||
| 56 | |||
| 57 | /** @var CirclesService */ |
||
| 58 | protected $circlesService; |
||
| 59 | |||
| 60 | /** @var MembersService */ |
||
| 61 | protected $membersService; |
||
| 62 | |||
| 63 | /** @var SharesService */ |
||
| 64 | protected $sharesService; |
||
| 65 | |||
| 66 | /** @var FederatedService */ |
||
| 67 | protected $federatedService; |
||
| 68 | |||
| 69 | /** @var MiscService */ |
||
| 70 | protected $miscService; |
||
| 71 | |||
| 72 | |||
| 73 | /** |
||
| 74 | * requestedLink() |
||
| 75 | * |
||
| 76 | * Called when a remote circle want to create a link. |
||
| 77 | * The function check if it is possible first; then create a link-object |
||
| 78 | * and sent it to be saved in the database. |
||
| 79 | * |
||
| 80 | * @PublicPage |
||
| 81 | * @NoCSRFRequired |
||
| 82 | * |
||
| 83 | * @param array $apiVersion |
||
| 84 | * @param string $token |
||
| 85 | * @param string $uniqueId |
||
| 86 | * @param string $sourceName |
||
| 87 | * @param string $linkTo |
||
| 88 | * @param string $address |
||
| 89 | * |
||
| 90 | * @return DataResponse |
||
| 91 | * @throws FederatedLinkCreationException |
||
| 92 | */ |
||
| 93 | public function requestedLink($apiVersion, $token, $uniqueId, $sourceName, $linkTo, $address) { |
||
| 94 | |||
| 95 | if ($uniqueId === '' || !$this->configService->isFederatedCirclesAllowed()) { |
||
| 96 | return $this->federatedFail('federated_not_allowed'); |
||
| 97 | } |
||
| 98 | |||
| 99 | try { |
||
| 100 | Circles::compareVersion($apiVersion); |
||
| 101 | $circle = $this->circlesService->infoCircleByName($linkTo); |
||
| 102 | $link = new FederatedLink(); |
||
| 103 | $link->setToken($token) |
||
| 104 | ->setUniqueId($uniqueId) |
||
| 105 | ->setRemoteCircleName($sourceName) |
||
| 106 | ->setAddress($address); |
||
| 107 | |||
| 108 | $this->federatedService->initiateLink($circle, $link); |
||
|
|
|||
| 109 | |||
| 110 | return $this->federatedSuccess( |
||
| 111 | ['status' => $link->getStatus(), 'uniqueId' => $circle->getUniqueId(true)], $link |
||
| 112 | ); |
||
| 113 | } catch (CircleDoesNotExistException $e) { |
||
| 114 | return $this->federatedFail('circle_does_not_exist'); |
||
| 115 | } catch (Exception $e) { |
||
| 116 | return $this->federatedFail($e->getMessage()); |
||
| 117 | } |
||
| 118 | } |
||
| 119 | |||
| 120 | |||
| 121 | /** |
||
| 122 | * initFederatedDelivery() |
||
| 123 | * |
||
| 124 | * Note: this function will close the request mid-run from the client but will still |
||
| 125 | * running its process. |
||
| 126 | * Called by locally, the function will get the SharingFrame by its uniqueId from the database, |
||
| 127 | * assign him some Headers and will deliver it to each remotes linked to the circle the Payload |
||
| 128 | * belongs to. A status response is sent to free the client process before starting to |
||
| 129 | * broadcast the item to other federated links. |
||
| 130 | * |
||
| 131 | * @PublicPage |
||
| 132 | * @NoCSRFRequired |
||
| 133 | * |
||
| 134 | * @param array $apiVersion |
||
| 135 | * @param string $circleId |
||
| 136 | * @param string $frameId |
||
| 137 | * |
||
| 138 | * @return DataResponse |
||
| 139 | */ |
||
| 140 | View Code Duplication | public function initFederatedDelivery($apiVersion, $circleId, $frameId) { |
|
| 141 | |||
| 142 | if ($frameId === '' || !$this->configService->isFederatedCirclesAllowed()) { |
||
| 143 | return $this->federatedFail('federated_not_allowed'); |
||
| 144 | } |
||
| 145 | |||
| 146 | try { |
||
| 147 | Circles::compareVersion($apiVersion); |
||
| 148 | $frame = $this->sharesService->getFrameFromUniqueId($circleId, $frameId); |
||
| 149 | } catch (Exception $e) { |
||
| 150 | return $this->federatedFail($e->getMessage()); |
||
| 151 | } |
||
| 152 | |||
| 153 | // We don't want to keep the connection up |
||
| 154 | $this->asyncAndLeaveClientOutOfThis('done'); |
||
| 155 | |||
| 156 | $this->federatedService->updateFrameWithCloudId($frame); |
||
| 157 | $this->federatedService->sendRemoteShare($frame); |
||
| 158 | |||
| 159 | exit(); |
||
| 160 | } |
||
| 161 | |||
| 162 | |||
| 163 | /** |
||
| 164 | * receiveFederatedDelivery() |
||
| 165 | * |
||
| 166 | * Note: this function will close the request mid-run from the client but will still |
||
| 167 | * running its process. |
||
| 168 | * Called by a remote circle to broadcast a Share item, the function will save the item |
||
| 169 | * in the database and broadcast it locally. A status response is sent to the remote to free |
||
| 170 | * the remote process before starting to broadcast the item to other federated links. |
||
| 171 | * |
||
| 172 | * @PublicPage |
||
| 173 | * @NoCSRFRequired |
||
| 174 | * |
||
| 175 | * @param array $apiVersion |
||
| 176 | * @param string $token |
||
| 177 | * @param string $uniqueId |
||
| 178 | * @param string $item |
||
| 179 | * |
||
| 180 | * @return DataResponse |
||
| 181 | */ |
||
| 182 | View Code Duplication | public function receiveFederatedDelivery($apiVersion, $token, $uniqueId, $item) { |
|
| 183 | |||
| 184 | if ($uniqueId === '' || !$this->configService->isFederatedCirclesAllowed()) { |
||
| 185 | return $this->federatedFail('federated_not_allowed'); |
||
| 186 | } |
||
| 187 | |||
| 188 | try { |
||
| 189 | Circles::compareVersion($apiVersion); |
||
| 190 | $frame = SharingFrame::fromJSON($item); |
||
| 191 | $this->federatedService->receiveFrame($token, $uniqueId, $frame); |
||
| 192 | } catch (SharingFrameAlreadyExistException $e) { |
||
| 193 | return $this->federatedSuccess(); |
||
| 194 | } catch (Exception $e) { |
||
| 195 | return $this->federatedFail($e->getMessage()); |
||
| 196 | } |
||
| 197 | |||
| 198 | $this->asyncAndLeaveClientOutOfThis('done'); |
||
| 199 | $this->federatedService->sendRemoteShare($frame); |
||
| 200 | exit(); |
||
| 201 | } |
||
| 202 | |||
| 203 | |||
| 204 | /** |
||
| 205 | * updateLink(); |
||
| 206 | * |
||
| 207 | * Update the current status of a link, based on UniqueId and Token. |
||
| 208 | * |
||
| 209 | * @PublicPage |
||
| 210 | * @NoCSRFRequired |
||
| 211 | * |
||
| 212 | * @param array $apiVersion |
||
| 213 | * @param string $token |
||
| 214 | * @param string $uniqueId |
||
| 215 | * @param $status |
||
| 216 | * |
||
| 217 | * @return DataResponse |
||
| 218 | */ |
||
| 219 | public function updateLink($apiVersion, $token, $uniqueId, $status) { |
||
| 220 | |||
| 221 | if ($uniqueId === '' || !$this->configService->isFederatedCirclesAllowed()) { |
||
| 222 | return $this->federatedFail('federated_not_allowed'); |
||
| 223 | } |
||
| 224 | |||
| 225 | try { |
||
| 226 | Circles::compareVersion($apiVersion); |
||
| 227 | $link = $this->federatedService->updateLinkFromRemote($token, $uniqueId, $status); |
||
| 228 | } catch (Exception $e) { |
||
| 229 | return $this->federatedFail($e->getMessage()); |
||
| 230 | } |
||
| 231 | |||
| 232 | return $this->federatedSuccess( |
||
| 233 | ['status' => 1, 'link' => $link], $link |
||
| 234 | ); |
||
| 235 | } |
||
| 236 | |||
| 237 | |||
| 238 | /** |
||
| 239 | * Hacky way to async the rest of the process without keeping client on hold. |
||
| 240 | * |
||
| 241 | * @param string $result |
||
| 242 | */ |
||
| 243 | private function asyncAndLeaveClientOutOfThis($result = '') { |
||
| 257 | |||
| 258 | /** |
||
| 259 | * send a positive response to a request with an array of data, and confirm |
||
| 260 | * the identity of the link with a token |
||
| 261 | * |
||
| 262 | * @param array $data |
||
| 263 | * @param FederatedLink $link |
||
| 264 | * |
||
| 265 | * @return DataResponse |
||
| 266 | */ |
||
| 267 | private function federatedSuccess(array $data = array(), FederatedLink $link = null) { |
||
| 279 | |||
| 280 | |||
| 281 | /** |
||
| 282 | * send a negative response to a request, with a reason of the failure. |
||
| 283 | * |
||
| 284 | * @param string $reason |
||
| 285 | * |
||
| 286 | * @return DataResponse |
||
| 287 | */ |
||
| 288 | private function federatedFail($reason) { |
||
| 299 | } |
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: