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 |
||
| 11 | class DropboxClient |
||
| 12 | { |
||
| 13 | const THUMBNAIL_FORMAT_JPEG = 'jpeg'; |
||
| 14 | const THUMBNAIL_FORMAT_PNG = 'png'; |
||
| 15 | |||
| 16 | const THUMBNAIL_SIZE_XS = 'w32h32'; |
||
| 17 | const THUMBNAIL_SIZE_S = 'w64h64'; |
||
| 18 | const THUMBNAIL_SIZE_M = 'w128h128'; |
||
| 19 | const THUMBNAIL_SIZE_L = 'w640h480'; |
||
| 20 | const THUMBNAIL_SIZE_XL = 'w1024h768'; |
||
| 21 | |||
| 22 | /** @var \GuzzleHttp\Client */ |
||
| 23 | protected $client; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * Dropbox OAuth access token. |
||
| 27 | * |
||
| 28 | * @var string |
||
| 29 | */ |
||
| 30 | protected $accessToken; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Dropbox API v2 Url. |
||
| 34 | * |
||
| 35 | * @var string |
||
| 36 | */ |
||
| 37 | protected $apiUrl; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Dropbox content API v2 url for uploading content. |
||
| 41 | * |
||
| 42 | * @var string |
||
| 43 | */ |
||
| 44 | protected $apiContentUrl; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Dropbox API v2 endpoint. |
||
| 48 | * |
||
| 49 | * @var string |
||
| 50 | */ |
||
| 51 | protected $apiEndpoint; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @var mixed |
||
| 55 | */ |
||
| 56 | protected $content; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Collection containing Dropbox API request data. |
||
| 60 | * |
||
| 61 | * @var \Illuminate\Support\Collection |
||
| 62 | */ |
||
| 63 | protected $request; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * DropboxClient constructor. |
||
| 67 | * |
||
| 68 | * @param string $token |
||
| 69 | * @param \GuzzleHttp\Client $client |
||
| 70 | */ |
||
| 71 | public function __construct($token, HttpClient $client = null) |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Set Http Client. |
||
| 83 | * |
||
| 84 | * @param \GuzzleHttp\Client $client |
||
| 85 | */ |
||
| 86 | protected function setClient(HttpClient $client = null) |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Set Dropbox OAuth access token. |
||
| 101 | * |
||
| 102 | * @param string $token |
||
| 103 | */ |
||
| 104 | protected function setAccessToken($token) |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Copy a file or folder to a different location in the user's Dropbox. |
||
| 111 | * |
||
| 112 | * If the source path is a folder all its contents will be copied. |
||
| 113 | * |
||
| 114 | * @param string $fromPath |
||
| 115 | * @param string $toPath |
||
| 116 | * |
||
| 117 | * @return \Psr\Http\Message\ResponseInterface |
||
| 118 | * |
||
| 119 | * @link https://www.dropbox.com/developers/documentation/http/documentation#files-copy |
||
| 120 | */ |
||
| 121 | View Code Duplication | public function copy($fromPath, $toPath) |
|
| 132 | |||
| 133 | /** |
||
| 134 | * Create a folder at a given path. |
||
| 135 | * |
||
| 136 | * @param string $path |
||
| 137 | * |
||
| 138 | * @return \Psr\Http\Message\ResponseInterface |
||
| 139 | * |
||
| 140 | * @link https://www.dropbox.com/developers/documentation/http/documentation#files-create_folder |
||
| 141 | */ |
||
| 142 | public function createFolder($path) |
||
| 155 | |||
| 156 | /** |
||
| 157 | * Delete the file or folder at a given path. |
||
| 158 | * |
||
| 159 | * If the path is a folder, all its contents will be deleted too. |
||
| 160 | * A successful response indicates that the file or folder was deleted. |
||
| 161 | * |
||
| 162 | * @param string $path |
||
| 163 | * |
||
| 164 | * @return \Psr\Http\Message\ResponseInterface |
||
| 165 | * |
||
| 166 | * @link https://www.dropbox.com/developers/documentation/http/documentation#files-delete |
||
| 167 | */ |
||
| 168 | public function delete($path) |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Download a file from a user's Dropbox. |
||
| 181 | * |
||
| 182 | * @param string $path |
||
| 183 | * |
||
| 184 | * @return resource |
||
| 185 | * |
||
| 186 | * @link https://www.dropbox.com/developers/documentation/http/documentation#files-download |
||
| 187 | */ |
||
| 188 | public function download($path) |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Returns the metadata for a file or folder. |
||
| 203 | * |
||
| 204 | * Note: Metadata for the root folder is unsupported. |
||
| 205 | * |
||
| 206 | * @param string $path |
||
| 207 | * |
||
| 208 | * @return \Psr\Http\Message\ResponseInterface |
||
| 209 | * |
||
| 210 | * @link https://www.dropbox.com/developers/documentation/http/documentation#files-get_metadata |
||
| 211 | */ |
||
| 212 | public function getMetaData($path) |
||
| 222 | |||
| 223 | /** |
||
| 224 | * Get a temporary link to stream content of a file. |
||
| 225 | * |
||
| 226 | * This link will expire in four hours and afterwards you will get 410 Gone. |
||
| 227 | * Content-Type of the link is determined automatically by the file's mime type. |
||
| 228 | * |
||
| 229 | * @param string $path |
||
| 230 | * |
||
| 231 | * @return string |
||
| 232 | * |
||
| 233 | * @link https://www.dropbox.com/developers/documentation/http/documentation#files-get_temporary_link |
||
| 234 | */ |
||
| 235 | View Code Duplication | public function getTemporaryLink($path) |
|
| 247 | |||
| 248 | /** |
||
| 249 | * Get a thumbnail for an image. |
||
| 250 | * |
||
| 251 | * This method currently supports files with the following file extensions: |
||
| 252 | * jpg, jpeg, png, tiff, tif, gif and bmp. |
||
| 253 | * |
||
| 254 | * Photos that are larger than 20MB in size won't be converted to a thumbnail. |
||
| 255 | * |
||
| 256 | * @param string $path |
||
| 257 | * @param string $format |
||
| 258 | * @param string $size |
||
| 259 | * |
||
| 260 | * @return string |
||
| 261 | */ |
||
| 262 | public function getThumbnail($path, $format = 'jpeg', $size = 'w64h64') |
||
| 276 | |||
| 277 | /** |
||
| 278 | * Starts returning the contents of a folder. |
||
| 279 | * |
||
| 280 | * If the result's ListFolderResult.has_more field is true, call |
||
| 281 | * list_folder/continue with the returned ListFolderResult.cursor to retrieve more entries. |
||
| 282 | * |
||
| 283 | * Note: auth.RateLimitError may be returned if multiple list_folder or list_folder/continue calls |
||
| 284 | * with same parameters are made simultaneously by same API app for same user. If your app implements |
||
| 285 | * retry logic, please hold off the retry until the previous request finishes. |
||
| 286 | * |
||
| 287 | * @param string $path |
||
| 288 | * @param bool $recursive |
||
| 289 | * |
||
| 290 | * @return \Psr\Http\Message\ResponseInterface |
||
| 291 | * |
||
| 292 | * @link https://www.dropbox.com/developers/documentation/http/documentation#files-list_folder |
||
| 293 | */ |
||
| 294 | View Code Duplication | public function listFolder($path = '', $recursive = false) |
|
| 305 | |||
| 306 | /** |
||
| 307 | * Once a cursor has been retrieved from list_folder, use this to paginate through all files and |
||
| 308 | * retrieve updates to the folder, following the same rules as documented for list_folder. |
||
| 309 | * |
||
| 310 | * @param string $cursor |
||
| 311 | * |
||
| 312 | * @return \Psr\Http\Message\ResponseInterface |
||
| 313 | * |
||
| 314 | * @link https://www.dropbox.com/developers/documentation/http/documentation#files-list_folder-continue |
||
| 315 | */ |
||
| 316 | public function listFolderContinue($cursor = '') |
||
| 326 | |||
| 327 | /** |
||
| 328 | * Move a file or folder to a different location in the user's Dropbox. |
||
| 329 | * |
||
| 330 | * If the source path is a folder all its contents will be moved. |
||
| 331 | * |
||
| 332 | * @param string $fromPath |
||
| 333 | * @param string $toPath |
||
| 334 | * |
||
| 335 | * @return \Psr\Http\Message\ResponseInterface |
||
| 336 | * |
||
| 337 | * @link https://www.dropbox.com/developers/documentation/http/documentation#files-move |
||
| 338 | */ |
||
| 339 | View Code Duplication | public function move($fromPath, $toPath) |
|
| 350 | |||
| 351 | /** |
||
| 352 | * Create a new file with the contents provided in the request. |
||
| 353 | * |
||
| 354 | * Do not use this to upload a file larger than 150 MB. Instead, create an upload session with upload_session/start. |
||
| 355 | * |
||
| 356 | * @link https://www.dropbox.com/developers/documentation/http/documentation#files-upload |
||
| 357 | * |
||
| 358 | * @param string $path |
||
| 359 | * @param string|resource $contents |
||
| 360 | * @param string|array $mode |
||
| 361 | * |
||
| 362 | * @return array |
||
| 363 | */ |
||
| 364 | public function upload($path, $contents, $mode = 'add') |
||
| 382 | |||
| 383 | /** |
||
| 384 | * Set Dropbox API request data. |
||
| 385 | * |
||
| 386 | * @param array $request |
||
| 387 | */ |
||
| 388 | protected function setupRequest($request) |
||
| 392 | |||
| 393 | /** |
||
| 394 | * Perform Dropbox API request. |
||
| 395 | * |
||
| 396 | * @throws \Exception |
||
| 397 | * |
||
| 398 | * @return \Psr\Http\Message\ResponseInterface |
||
| 399 | */ |
||
| 400 | protected function doDropboxApiRequest() |
||
| 412 | |||
| 413 | /** |
||
| 414 | * Setup headers for Dropbox API request. |
||
| 415 | * |
||
| 416 | * @return array |
||
| 417 | */ |
||
| 418 | protected function setupDropboxHeaders() |
||
| 432 | |||
| 433 | /** |
||
| 434 | * Perform Dropbox API request. |
||
| 435 | * |
||
| 436 | * @throws \Exception |
||
| 437 | * |
||
| 438 | * @return \Psr\Http\Message\ResponseInterface |
||
| 439 | */ |
||
| 440 | protected function doDropboxApiContentRequest() |
||
| 453 | |||
| 454 | /** |
||
| 455 | * Normalize path. |
||
| 456 | * |
||
| 457 | * @param string $path |
||
| 458 | * |
||
| 459 | * @return string |
||
| 460 | */ |
||
| 461 | protected function normalizePath($path) |
||
| 467 | |||
| 468 | /** |
||
| 469 | * Catch Dropbox API request exception. |
||
| 470 | * |
||
| 471 | * @param HttpClientException $exception |
||
| 472 | * |
||
| 473 | * @return \Exception |
||
| 474 | */ |
||
| 475 | protected function determineException(HttpClientException $exception) |
||
| 483 | } |
||
| 484 |