GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Pull Request — master (#21)
by
unknown
01:20
created

Client::getMetadata()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 1
1
<?php
2
3
namespace Spatie\Dropbox;
4
5
use Exception;
6
use GuzzleHttp\Psr7\StreamWrapper;
7
use GuzzleHttp\Client as GuzzleClient;
8
use Psr\Http\Message\ResponseInterface;
9
use GuzzleHttp\Exception\ClientException;
10
use Spatie\Dropbox\Exceptions\BadRequest;
11
12
class Client
13
{
14
    const THUMBNAIL_FORMAT_JPEG = 'jpeg';
15
    const THUMBNAIL_FORMAT_PNG = 'png';
16
17
    const THUMBNAIL_SIZE_XS = 'w32h32';
18
    const THUMBNAIL_SIZE_S = 'w64h64';
19
    const THUMBNAIL_SIZE_M = 'w128h128';
20
    const THUMBNAIL_SIZE_L = 'w640h480';
21
    const THUMBNAIL_SIZE_XL = 'w1024h768';
22
23
    const AUTO_CHUNKED_UPLOAD_THRESHOLD = 157286400;
24
    const DEFAULT_CHUNK_SIZE = 4194304;
25
26
    /** @var string */
27
    protected $accessToken;
28
29
    /** @var \GuzzleHttp\Client */
30
    protected $client;
31
32
    public function __construct(string $accessToken, GuzzleClient $client = null)
33
    {
34
        $this->accessToken = $accessToken;
35
36
        $this->client = $client ?? new GuzzleClient([
37
                'headers' => [
38
                    'Authorization' => "Bearer {$this->accessToken}",
39
                ],
40
            ]);
41
    }
42
43
    /**
44
     * Copy a file or folder to a different location in the user's Dropbox.
45
     *
46
     * If the source path is a folder all its contents will be copied.
47
     *
48
     * @link https://www.dropbox.com/developers/documentation/http/documentation#files-copy
49
     */
50 View Code Duplication
    public function copy(string $fromPath, string $toPath): array
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
51
    {
52
        $parameters = [
53
            'from_path' => $this->normalizePath($fromPath),
54
            'to_path' => $this->normalizePath($toPath),
55
        ];
56
57
        return $this->rpcEndpointRequest('files/copy', $parameters);
58
    }
59
60
    /**
61
     * Create a folder at a given path.
62
     *
63
     * @link https://www.dropbox.com/developers/documentation/http/documentation#files-create_folder
64
     */
65
    public function createFolder(string $path): array
66
    {
67
        $parameters = [
68
            'path' => $this->normalizePath($path),
69
        ];
70
71
        $object = $this->rpcEndpointRequest('files/create_folder', $parameters);
72
73
        $object['.tag'] = 'folder';
74
75
        return $object;
76
    }
77
78
    /**
79
     * Create a shared link with custom settings.
80
     *
81
     * If no settings are given then the default visibility is RequestedVisibility.public.
82
     * The resolved visibility, though, may depend on other aspects such as team and
83
     * shared folder settings). Only for paid users.
84
     *
85
     * @link https://www.dropbox.com/developers/documentation/http/documentation#sharing-create_shared_link_with_settings
86
     */
87 View Code Duplication
    public function createSharedLinkWithSettings(string $path, array $settings = [])
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
88
    {
89
        $parameters = [
90
            'path' => $this->normalizePath($path),
91
            'settings' => $settings,
92
        ];
93
94
        return $this->rpcEndpointRequest('sharing/create_shared_link_with_settings', $parameters);
95
    }
96
97
    /**
98
     * List shared links.
99
     *
100
     * For empty path returns a list of all shared links. For non-empty path
101
     * returns a list of all shared links with access to the given path.
102
     *
103
     * If direct_only is set true, only direct links to the path will be returned, otherwise
104
     * it may return link to the path itself and parent folders as described on docs.
105
     *
106
     * @link https://www.dropbox.com/developers/documentation/http/documentation#sharing-list_shared_links
107
     */
108
    public function listSharedLinks(string $path = null, bool $direct_only = false, string $cursor = null): array
109
    {
110
        $parameters = [
111
            'path' => $path ? $this->normalizePath($path) : null,
112
            'cursor' => $cursor,
113
            'direct_only' => $direct_only,
114
        ];
115
116
        $body = $this->rpcEndpointRequest('sharing/list_shared_links', $parameters);
117
118
        return $body['links'];
119
    }
120
121
    /**
122
     * Delete the file or folder at a given path.
123
     *
124
     * If the path is a folder, all its contents will be deleted too.
125
     * A successful response indicates that the file or folder was deleted.
126
     *
127
     * @link https://www.dropbox.com/developers/documentation/http/documentation#files-delete
128
     */
129
    public function delete(string $path): array
130
    {
131
        $parameters = [
132
            'path' => $this->normalizePath($path),
133
        ];
134
135
        return $this->rpcEndpointRequest('files/delete', $parameters);
136
    }
137
138
    /**
139
     * Download a file from a user's Dropbox.
140
     *
141
     * @param string $path
142
     *
143
     * @return resource
144
     *
145
     * @link https://www.dropbox.com/developers/documentation/http/documentation#files-download
146
     */
147
    public function download(string $path)
148
    {
149
        $arguments = [
150
            'path' => $this->normalizePath($path),
151
        ];
152
153
        $response = $this->contentEndpointRequest('files/download', $arguments);
154
155
        return StreamWrapper::getResource($response->getBody());
156
    }
157
158
    /**
159
     * Returns the metadata for a file or folder.
160
     *
161
     * Note: Metadata for the root folder is unsupported.
162
     *
163
     * @link https://www.dropbox.com/developers/documentation/http/documentation#files-get_metadata
164
     */
165
    public function getMetadata(string $path): array
166
    {
167
        $parameters = [
168
            'path' => $this->normalizePath($path),
169
        ];
170
171
        return $this->rpcEndpointRequest('files/get_metadata', $parameters);
172
    }
173
174
    /**
175
     * Get a temporary link to stream content of a file.
176
     *
177
     * This link will expire in four hours and afterwards you will get 410 Gone.
178
     * Content-Type of the link is determined automatically by the file's mime type.
179
     *
180
     * @link https://www.dropbox.com/developers/documentation/http/documentation#files-get_temporary_link
181
     */
182 View Code Duplication
    public function getTemporaryLink(string $path): string
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
183
    {
184
        $parameters = [
185
            'path' => $this->normalizePath($path),
186
        ];
187
188
        $body = $this->rpcEndpointRequest('files/get_temporary_link', $parameters);
189
190
        return $body['link'];
191
    }
192
193
    /**
194
     * Get a thumbnail for an image.
195
     *
196
     * This method currently supports files with the following file extensions:
197
     * jpg, jpeg, png, tiff, tif, gif and bmp.
198
     *
199
     * Photos that are larger than 20MB in size won't be converted to a thumbnail.
200
     *
201
     * @link https://www.dropbox.com/developers/documentation/http/documentation#files-get_thumbnail
202
     */
203
    public function getThumbnail(string $path, string $format = 'jpeg', string $size = 'w64h64'): string
204
    {
205
        $arguments = [
206
            'path' => $this->normalizePath($path),
207
            'format' => $format,
208
            'size' => $size,
209
        ];
210
211
        $response = $this->contentEndpointRequest('files/get_thumbnail', $arguments);
212
213
        return (string) $response->getBody();
214
    }
215
216
    /**
217
     * Starts returning the contents of a folder.
218
     *
219
     * If the result's ListFolderResult.has_more field is true, call
220
     * list_folder/continue with the returned ListFolderResult.cursor to retrieve more entries.
221
     *
222
     * Note: auth.RateLimitError may be returned if multiple list_folder or list_folder/continue calls
223
     * with same parameters are made simultaneously by same API app for same user. If your app implements
224
     * retry logic, please hold off the retry until the previous request finishes.
225
     *
226
     * @link https://www.dropbox.com/developers/documentation/http/documentation#files-list_folder
227
     */
228 View Code Duplication
    public function listFolder(string $path = '', bool $recursive = false): array
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
229
    {
230
        $parameters = [
231
            'path' => $this->normalizePath($path),
232
            'recursive' => $recursive,
233
        ];
234
235
        return $this->rpcEndpointRequest('files/list_folder', $parameters);
236
    }
237
238
    /**
239
     * Once a cursor has been retrieved from list_folder, use this to paginate through all files and
240
     * retrieve updates to the folder, following the same rules as documented for list_folder.
241
     *
242
     * @link https://www.dropbox.com/developers/documentation/http/documentation#files-list_folder-continue
243
     */
244
    public function listFolderContinue(string $cursor = ''): array
245
    {
246
        return $this->rpcEndpointRequest('files/list_folder/continue', compact('cursor'));
247
    }
248
249
    /**
250
     * Move a file or folder to a different location in the user's Dropbox.
251
     *
252
     * If the source path is a folder all its contents will be moved.
253
     *
254
     * @link https://www.dropbox.com/developers/documentation/http/documentation#files-move
255
     */
256 View Code Duplication
    public function move(string $fromPath, string $toPath): array
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
257
    {
258
        $parameters = [
259
            'from_path' => $this->normalizePath($fromPath),
260
            'to_path' => $this->normalizePath($toPath),
261
        ];
262
263
        return $this->rpcEndpointRequest('files/move', $parameters);
264
    }
265
266
    /**
267
     * Create a new file with the contents provided in the request.
268
     *
269
     * Do not use this to upload a file larger than 150 MB. Instead, create an upload session with upload_session/start.
270
     *
271
     * @link https://www.dropbox.com/developers/documentation/http/documentation#files-upload
272
     *
273
     * @param string $path
274
     * @param string|resource $contents
275
     * @param string|array $mode
276
     *
277
     * @return array
278
     */
279
    public function upload(string $path, $contents, $mode = 'add'): array
280
    {
281
        $size = is_string($contents) ? ['size' => strlen($contents)] : fstat($contents)['size'];
282
283
        // If we couldn't determine file size or it's larger then 150 MB,
284
        // we upload it using the upload_session feature.
285
        if ($size === null || $size > self::AUTO_CHUNKED_UPLOAD_THRESHOLD) {
286
            return $this->uploadChunked($path, $contents, $mode);
0 ignored issues
show
Bug introduced by
It seems like $mode defined by parameter $mode on line 279 can also be of type array; however, Spatie\Dropbox\Client::uploadChunked() does only seem to accept string, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
287
        }
288
289
        $arguments = [
290
            'path' => $this->normalizePath($path),
291
            'mode' => $mode,
292
        ];
293
294
        $response = $this->contentEndpointRequest('files/upload', $arguments, $contents);
295
296
        $metadata = json_decode($response->getBody(), true);
297
298
        $metadata['.tag'] = 'file';
299
300
        return $metadata;
301
    }
302
303
    /**
304
     * Upload file split in chunks. This allows uploading large files, since
305
     * Dropbox API v2 limits the content size to 150MB.
306
     *
307
     * The chunk size will affect directly the memory usage, so be careful.
308
     * Large chunks tends to speed up the upload, while smaller optimizes memory usage.
309
     *
310
     * @param string          $path
311
     * @param string|resource $contents
312
     * @param string          $mode
313
     * @param int             $chunkSize
314
     *
315
     * @return array
316
     */
317
    public function uploadChunked(string $path, $contents, $mode = 'add', $chunkSize = null): array
318
    {
319
        $chunkSize = $chunkSize ?? static::DEFAULT_CHUNK_SIZE;
320
        $stream = $contents;
321
322
        // This method relies on resources, so we need to convert strings to resource
323
        if (is_string($contents)) {
324
            $stream = fopen('php://memory', 'r+');
325
            fwrite($stream, $contents);
326
            rewind($stream);
327
        }
328
329
        $data = self::readChunk($stream, $chunkSize);
0 ignored issues
show
Bug introduced by
It seems like $stream defined by $contents on line 320 can also be of type string; however, Spatie\Dropbox\Client::readChunk() does only seem to accept resource, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
330
        $cursor = null;
331
332
        while (! ((strlen($data) < $chunkSize) || feof($stream))) {
333
            // Start upload session on first iteration, then just append on subsequent iterations
334
            $cursor = isset($cursor) ? $this->uploadSessionAppend($data, $cursor) : $this->uploadSessionStart($data);
335
            $data = self::readChunk($stream, $chunkSize);
0 ignored issues
show
Bug introduced by
It seems like $stream defined by $contents on line 320 can also be of type string; however, Spatie\Dropbox\Client::readChunk() does only seem to accept resource, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
336
        }
337
338
        // If there's no cursor here, our stream is small enough to a single request
339
        if (! isset($cursor)) {
340
            $cursor = $this->uploadSessionStart($data);
341
            $data = '';
342
        }
343
344
        return $this->uploadSessionFinish($data, $cursor, $path, $mode);
345
    }
346
347
    /**
348
     * Upload sessions allow you to upload a single file in one or more requests,
349
     * for example where the size of the file is greater than 150 MB.
350
     * This call starts a new upload session with the given data.
351
     *
352
     * @link https://www.dropbox.com/developers/documentation/http/documentation#files-upload_session-start
353
     *
354
     * @param string $contents
355
     * @param bool   $close
356
     *
357
     * @return UploadSessionCursor
358
     */
359
    public function uploadSessionStart($contents, bool $close = false): UploadSessionCursor
360
    {
361
        $arguments = compact('close');
362
363
        $response = json_decode(
364
            $this->contentEndpointRequest('files/upload_session/start', $arguments, $contents)->getBody(),
365
            true
366
        );
367
368
        return new UploadSessionCursor($response['session_id'], strlen($contents));
369
    }
370
371
    /**
372
     * Append more data to an upload session.
373
     * When the parameter close is set, this call will close the session.
374
     * A single request should not upload more than 150 MB.
375
     *
376
     * @link https://www.dropbox.com/developers/documentation/http/documentation#files-upload_session-append_v2
377
     *
378
     * @param string              $contents
379
     * @param UploadSessionCursor $cursor
380
     * @param bool                $close
381
     *
382
     * @return \Spatie\Dropbox\UploadSessionCursor
383
     */
384
    public function uploadSessionAppend($contents, UploadSessionCursor $cursor, bool $close = false): UploadSessionCursor
385
    {
386
        $arguments = compact('cursor', 'close');
387
388
        $this->contentEndpointRequest('files/upload_session/append_v2', $arguments, $contents);
389
390
        $cursor->offset += strlen($contents);
391
392
        return $cursor;
393
    }
394
395
    /**
396
     * Finish an upload session and save the uploaded data to the given file path.
397
     * A single request should not upload more than 150 MB.
398
     *
399
     * @link https://www.dropbox.com/developers/documentation/http/documentation#files-upload_session-finish
400
     *
401
     * @param string                              $contents
402
     * @param \Spatie\Dropbox\UploadSessionCursor $cursor
403
     * @param string                              $path
404
     * @param string|array                        $mode
405
     * @param bool                                $autorename
406
     * @param bool                                $mute
407
     *
408
     * @return array
409
     */
410
    public function uploadSessionFinish($contents, UploadSessionCursor $cursor, string $path, $mode = 'add', $autorename = false, $mute = false): array
411
    {
412
        $arguments = compact('cursor');
413
        $arguments['commit'] = compact('path', 'mode', 'autorename', 'mute');
414
415
        $response = $this->contentEndpointRequest(
416
            'files/upload_session/finish',
417
            $arguments,
418
            ($contents == '') ? null : $contents
419
        );
420
421
        return json_decode($response->getBody(), true);
422
    }
423
424
    /**
425
     * Sometimes fread() returns less than the request number of bytes (for example, when reading
426
     * from network streams).  This function repeatedly calls fread until the requested number of
427
     * bytes have been read or we've reached EOF.
428
     *
429
     * @param resource $stream
430
     * @param int      $chunkSize
431
     *
432
     * @throws Exception
433
     * @return string
434
     */
435
    protected static function readChunk($stream, int $chunkSize)
436
    {
437
        $chunk = '';
438
        while (! feof($stream) && $chunkSize > 0) {
439
            $part = fread($stream, $chunkSize);
440
            if ($part === false) {
441
                throw new Exception('Error reading from $stream.');
442
            }
443
            $chunk .= $part;
444
            $chunkSize -= strlen($part);
445
        }
446
447
        return $chunk;
448
    }
449
450
    /**
451
     * Get Account Info for current authenticated user.
452
     *
453
     * @link https://www.dropbox.com/developers/documentation/http/documentation#users-get_current_account
454
     *
455
     * @return array
456
     */
457
    public function getAccountInfo(): array
458
    {
459
        return $this->rpcEndpointRequest('users/get_current_account');
460
    }
461
462
    /**
463
     * Revoke current access token.
464
     *
465
     * @link https://www.dropbox.com/developers/documentation/http/documentation#auth-token-revoke
466
     */
467
    public function revokeToken()
468
    {
469
        $this->rpcEndpointRequest('auth/token/revoke');
470
    }
471
472
    protected function normalizePath(string $path): string
473
    {
474
        $path = trim($path, '/');
475
476
        return ($path === '') ? '' : '/'.$path;
477
    }
478
479
    /**
480
     * @param string $endpoint
481
     * @param array $arguments
482
     * @param string|resource $body
483
     *
484
     * @return \Psr\Http\Message\ResponseInterface
485
     *
486
     * @throws \Exception
487
     */
488
    public function contentEndpointRequest(string $endpoint, array $arguments, $body = ''): ResponseInterface
489
    {
490
        $headers['Dropbox-API-Arg'] = json_encode($arguments);
0 ignored issues
show
Coding Style Comprehensibility introduced by
$headers was never initialized. Although not strictly required by PHP, it is generally a good practice to add $headers = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
491
492
        if ($body !== '') {
493
            $headers['Content-Type'] = 'application/octet-stream';
494
        }
495
496
        try {
497
            $response = $this->client->post("https://content.dropboxapi.com/2/{$endpoint}", [
498
                'headers' => $headers,
499
                'body' => $body,
500
            ]);
501
        } catch (ClientException $exception) {
502
            throw $this->determineException($exception);
503
        }
504
505
        return $response;
506
    }
507
508
    public function rpcEndpointRequest(string $endpoint, array $parameters = null): array
509
    {
510
        try {
511
            $options = [];
512
513
            if ($parameters) {
514
                $options['json'] = $parameters;
515
            }
516
517
            $response = $this->client->post("https://api.dropboxapi.com/2/{$endpoint}", $options);
518
        } catch (ClientException $exception) {
519
            throw $this->determineException($exception);
520
        }
521
522
        $response = json_decode($response->getBody(), true);
523
524
        return $response ?? [];
525
    }
526
527
    protected function determineException(ClientException $exception): Exception
528
    {
529
        if (in_array($exception->getResponse()->getStatusCode(), [400, 409])) {
530
            return new BadRequest($exception->getResponse());
0 ignored issues
show
Bug introduced by
It seems like $exception->getResponse() can be null; however, __construct() does not accept null, maybe add an additional type check?

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:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
531
        }
532
533
        return $exception;
534
    }
535
}
536