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 |
||
| 48 | class SharingFrameService { |
||
| 49 | |||
| 50 | /** @var string */ |
||
| 51 | private $userId; |
||
| 52 | |||
| 53 | /** @var ConfigService */ |
||
| 54 | private $configService; |
||
| 55 | |||
| 56 | /** @var SharingFrameRequest */ |
||
| 57 | private $sharingFrameRequest; |
||
| 58 | |||
| 59 | /** @var CirclesRequest */ |
||
| 60 | private $circlesRequest; |
||
| 61 | |||
| 62 | /** @var FederatedLinksRequest */ |
||
| 63 | private $federatedLinksRequest; |
||
| 64 | |||
| 65 | /** @var BroadcastService */ |
||
| 66 | private $broadcastService; |
||
| 67 | |||
| 68 | /** @var FederatedLinkService */ |
||
| 69 | private $federatedLinkService; |
||
| 70 | |||
| 71 | /** @var IClientService */ |
||
| 72 | private $clientService; |
||
| 73 | |||
| 74 | /** @var MiscService */ |
||
| 75 | private $miscService; |
||
| 76 | |||
| 77 | |||
| 78 | /** |
||
| 79 | * SharingFrameService constructor. |
||
| 80 | * |
||
| 81 | * @param string $userId |
||
| 82 | * @param ConfigService $configService |
||
| 83 | * @param SharingFrameRequest $sharingFrameRequest |
||
| 84 | * @param CirclesRequest $circlesRequest |
||
| 85 | * @param FederatedLinksRequest $federatedLinksRequest |
||
| 86 | * @param BroadcastService $broadcastService |
||
| 87 | * @param FederatedLinkService $federatedLinkService |
||
| 88 | * @param IClientService $clientService |
||
| 89 | * @param MiscService $miscService |
||
| 90 | */ |
||
| 91 | View Code Duplication | public function __construct( |
|
|
|
|||
| 92 | $userId, |
||
| 93 | ConfigService $configService, |
||
| 94 | SharingFrameRequest $sharingFrameRequest, |
||
| 95 | CirclesRequest $circlesRequest, |
||
| 96 | FederatedLinksRequest $federatedLinksRequest, |
||
| 97 | BroadcastService $broadcastService, |
||
| 98 | FederatedLinkService $federatedLinkService, |
||
| 99 | IClientService $clientService, |
||
| 100 | MiscService $miscService |
||
| 101 | ) { |
||
| 102 | $this->userId = $userId; |
||
| 103 | $this->configService = $configService; |
||
| 104 | $this->sharingFrameRequest = $sharingFrameRequest; |
||
| 105 | $this->circlesRequest = $circlesRequest; |
||
| 106 | $this->federatedLinksRequest = $federatedLinksRequest; |
||
| 107 | $this->broadcastService = $broadcastService; |
||
| 108 | $this->federatedLinkService = $federatedLinkService; |
||
| 109 | $this->clientService = $clientService; |
||
| 110 | $this->miscService = $miscService; |
||
| 111 | } |
||
| 112 | |||
| 113 | |||
| 114 | /** |
||
| 115 | * createFrame() |
||
| 116 | * |
||
| 117 | * Save the Frame containing the Payload. |
||
| 118 | * The Payload will be shared locally, and spread it live if a Broadcaster is set. |
||
| 119 | * Function will also initiate the federated broadcast to linked circles. |
||
| 120 | * |
||
| 121 | * @param string $circleUniqueId |
||
| 122 | * @param SharingFrame $frame |
||
| 123 | * @param string|null $broadcast |
||
| 124 | * |
||
| 125 | * @throws Exception |
||
| 126 | * @throws MemberDoesNotExistException |
||
| 127 | */ |
||
| 128 | View Code Duplication | public function createFrame($circleUniqueId, SharingFrame $frame, $broadcast = null) { |
|
| 145 | |||
| 146 | |||
| 147 | /** |
||
| 148 | * Generate Headers and few more thing like UniqueId and Author. |
||
| 149 | * Check if the source is NOT Circles. |
||
| 150 | * |
||
| 151 | * @param SharingFrame $frame |
||
| 152 | * @param Circle $circle |
||
| 153 | * @param $broadcast |
||
| 154 | */ |
||
| 155 | private function generateHeaders(SharingFrame $frame, Circle $circle, $broadcast) { |
||
| 171 | |||
| 172 | |||
| 173 | /** |
||
| 174 | * return all SharingFrame from a circle regarding a userId. |
||
| 175 | * |
||
| 176 | * @param string $circleUniqueId |
||
| 177 | * |
||
| 178 | * @return SharingFrame[] |
||
| 179 | */ |
||
| 180 | public function getFrameFromCircle($circleUniqueId) { |
||
| 181 | return $this->forceGetFrameFromCircle($circleUniqueId, $this->userId); |
||
| 182 | } |
||
| 183 | |||
| 184 | |||
| 185 | /** |
||
| 186 | * return all SharingFrame from a circle. |
||
| 187 | * |
||
| 188 | * Warning, result won't be filtered regarding current user session. |
||
| 189 | * Please use getFrameFromCircle(); |
||
| 190 | * |
||
| 191 | * @param string $circleUniqueId |
||
| 192 | * @param $viewerId |
||
| 193 | * |
||
| 194 | * @return SharingFrame[] |
||
| 195 | */ |
||
| 196 | public function forceGetFrameFromCircle($circleUniqueId, $viewerId) { |
||
| 197 | |||
| 198 | if ($viewerId !== '') { |
||
| 199 | $circle = $this->circlesRequest->getCircle($circleUniqueId, $viewerId); |
||
| 200 | $circle->getViewer() |
||
| 201 | ->hasToBeMember(); |
||
| 202 | } |
||
| 203 | |||
| 204 | return $this->sharingFrameRequest->getSharingFramesFromCircle($circleUniqueId); |
||
| 205 | } |
||
| 206 | |||
| 207 | |||
| 208 | /** |
||
| 209 | * @param string $circleUniqueId |
||
| 210 | * @param string $frameUniqueId |
||
| 211 | * |
||
| 212 | * @return null|SharingFrame |
||
| 213 | * @throws SharingFrameAlreadyDeliveredException |
||
| 214 | * @throws SharingFrameDoesNotExistException |
||
| 215 | */ |
||
| 216 | public function getFrameFromUniqueId($circleUniqueId, $frameUniqueId) { |
||
| 217 | if ($frameUniqueId === '') { |
||
| 218 | throw new SharingFrameDoesNotExistException('unknown_share'); |
||
| 219 | } |
||
| 220 | |||
| 221 | try { |
||
| 222 | $frame = $this->sharingFrameRequest->getSharingFrame($circleUniqueId, $frameUniqueId); |
||
| 223 | if ($frame->getCloudId() !== null) { |
||
| 224 | throw new SharingFrameAlreadyDeliveredException('share_already_delivered'); |
||
| 225 | } |
||
| 226 | } catch (SharingFrameDoesNotExistException $e) { |
||
| 227 | throw new SharingFrameDoesNotExistException('unknown_share'); |
||
| 228 | } |
||
| 229 | |||
| 230 | return $frame; |
||
| 231 | } |
||
| 232 | |||
| 233 | |||
| 234 | /** |
||
| 235 | * @param string $token |
||
| 236 | * @param string $uniqueId |
||
| 237 | * @param SharingFrame $frame |
||
| 238 | * |
||
| 239 | * @return bool |
||
| 240 | * @throws Exception |
||
| 241 | */ |
||
| 242 | public function receiveFrame($token, $uniqueId, SharingFrame &$frame) { |
||
| 243 | try { |
||
| 244 | $link = $this->federatedLinksRequest->getLinkFromToken((string)$token, (string)$uniqueId); |
||
| 245 | $circle = $this->circlesRequest->forceGetCircle($link->getCircleId()); |
||
| 246 | } catch (CircleDoesNotExistException $e) { |
||
| 247 | throw new CircleDoesNotExistException('unknown_circle'); |
||
| 248 | } catch (Exception $e) { |
||
| 249 | throw $e; |
||
| 250 | } |
||
| 251 | |||
| 252 | try { |
||
| 253 | $this->sharingFrameRequest->getSharingFrame($link->getCircleId(), $frame->getUniqueId()); |
||
| 254 | throw new SharingFrameAlreadyExistException('shares_is_already_known'); |
||
| 255 | } catch (SharingFrameDoesNotExistException $e) { |
||
| 256 | } |
||
| 257 | |||
| 258 | $frame->setCircle($circle); |
||
| 259 | $this->sharingFrameRequest->saveSharingFrame($frame); |
||
| 260 | |||
| 261 | return true; |
||
| 262 | } |
||
| 263 | |||
| 264 | |||
| 265 | /** |
||
| 266 | * @param string $circleUniqueId |
||
| 267 | * @param string $frameUniqueId |
||
| 268 | * |
||
| 269 | * @return bool |
||
| 270 | * @throws Exception |
||
| 271 | */ |
||
| 272 | public function initiateShare($circleUniqueId, $frameUniqueId) { |
||
| 293 | |||
| 294 | |||
| 295 | /** |
||
| 296 | * @param string $remote |
||
| 297 | * |
||
| 298 | * @return string |
||
| 299 | */ |
||
| 300 | private function generatePayloadDeliveryURL($remote) { |
||
| 303 | |||
| 304 | |||
| 305 | /** |
||
| 306 | * @param SharingFrame $frame |
||
| 307 | * |
||
| 308 | * @throws Exception |
||
| 309 | */ |
||
| 310 | public function forwardSharingFrame(SharingFrame $frame) { |
||
| 328 | |||
| 329 | |||
| 330 | /** |
||
| 331 | * @param Circle $circle |
||
| 332 | * @param SharingFrame $frame |
||
| 333 | * @param FederatedLink[] $links |
||
| 334 | */ |
||
| 335 | private function forwardSharingFrameToFederatedLinks(Circle $circle, SharingFrame $frame, $links) { |
||
| 336 | |||
| 337 | $args = [ |
||
| 338 | 'apiVersion' => Circles::version(), |
||
| 339 | 'uniqueId' => $circle->getUniqueId(true), |
||
| 340 | 'item' => json_encode($frame) |
||
| 341 | ]; |
||
| 342 | |||
| 343 | foreach ($links AS $link) { |
||
| 344 | $args['token'] = $link->getToken(true); |
||
| 345 | $this->deliverSharingFrameToLink($link, $args); |
||
| 346 | } |
||
| 347 | } |
||
| 348 | |||
| 349 | |||
| 350 | /** |
||
| 351 | * sendRemoteShareToLinks(); |
||
| 352 | * |
||
| 353 | * @param FederatedLink $link |
||
| 354 | * @param array $args |
||
| 355 | */ |
||
| 356 | private function deliverSharingFrameToLink($link, $args) { |
||
| 379 | |||
| 380 | |||
| 381 | /** |
||
| 382 | * @param SharingFrame $frame |
||
| 383 | */ |
||
| 384 | public function updateFrameWithCloudId(SharingFrame $frame) { |
||
| 385 | $frame->setCloudId($this->configService->getLocalAddress()); |
||
| 388 | |||
| 389 | |||
| 390 | } |
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.