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 (#30)
by
unknown
01:36
created

Client::readChunk()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.2
c 0
b 0
f 0
cc 4
eloc 9
nc 3
nop 2

1 Method

Rating   Name   Duplication   Size   Complexity  
A Client::revokeToken() 0 4 1
1
<?php
2
3
namespace Spatie\Dropbox;
4
5
use Exception;
6
use GuzzleHttp\Psr7;
7
use GuzzleHttp\Psr7\StreamWrapper;
8
use Psr\Http\Message\StreamInterface;
9
use GuzzleHttp\Client as GuzzleClient;
10
use Psr\Http\Message\ResponseInterface;
11
use GuzzleHttp\Exception\ClientException;
12
use Spatie\Dropbox\Exceptions\BadRequest;
13
14
class Client
15
{
16
    const THUMBNAIL_FORMAT_JPEG = 'jpeg';
17
    const THUMBNAIL_FORMAT_PNG = 'png';
18
19
    const THUMBNAIL_SIZE_XS = 'w32h32';
20
    const THUMBNAIL_SIZE_S = 'w64h64';
21
    const THUMBNAIL_SIZE_M = 'w128h128';
22
    const THUMBNAIL_SIZE_L = 'w640h480';
23
    const THUMBNAIL_SIZE_XL = 'w1024h768';
24
25
    const MAX_CHUNK_SIZE = 150 * 1024 * 1024;
26
27
    /** @var string */
28
    protected $accessToken;
29
30
    /** @var \GuzzleHttp\Client */
31
    protected $client;
32
33
    /** @var int */
34
    protected $maxChunkSize;
35
36
    /**
37
     * @param string            $accessToken
38
     * @param GuzzleClient|null $client
39
     * @param int               $maxChunkSize This is also the amount of memory that will be used for storing data
40
     */
41
    public function __construct(string $accessToken, GuzzleClient $client = null, int $maxChunkSize = self::MAX_CHUNK_SIZE /*4 * 1024 * 1024*/)
42
    {
43
        $this->accessToken = $accessToken;
44
45
        $this->client = $client ?? new GuzzleClient([
46
                'headers' => [
47
                    'Authorization' => "Bearer {$this->accessToken}",
48
                ],
49
            ]);
50
51
        $this->maxChunkSize = ($maxChunkSize < self::MAX_CHUNK_SIZE ? $maxChunkSize : self::MAX_CHUNK_SIZE);
52
    }
53
54
    /**
55
     * Copy a file or folder to a different location in the user's Dropbox.
56
     *
57
     * If the source path is a folder all its contents will be copied.
58
     *
59
     * @link https://www.dropbox.com/developers/documentation/http/documentation#files-copy_v2
60
     */
61 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...
62
    {
63
        $parameters = [
64
            'from_path' => $this->normalizePath($fromPath),
65
            'to_path' => $this->normalizePath($toPath),
66
        ];
67
68
        return $this->rpcEndpointRequest('files/copy_v2', $parameters);
69
    }
70
71
    /**
72
     * Create a folder at a given path.
73
     *
74
     * @link https://www.dropbox.com/developers/documentation/http/documentation#files-create_folder
75
     */
76
    public function createFolder(string $path): array
77
    {
78
        $parameters = [
79
            'path' => $this->normalizePath($path),
80
        ];
81
82
        $object = $this->rpcEndpointRequest('files/create_folder', $parameters);
83
84
        $object['.tag'] = 'folder';
85
86
        return $object;
87
    }
88
89
    /**
90
     * Create a shared link with custom settings.
91
     *
92
     * If no settings are given then the default visibility is RequestedVisibility.public.
93
     * The resolved visibility, though, may depend on other aspects such as team and
94
     * shared folder settings). Only for paid users.
95
     *
96
     * @link https://www.dropbox.com/developers/documentation/http/documentation#sharing-create_shared_link_with_settings
97
     */
98 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...
99
    {
100
        $parameters = [
101
            'path' => $this->normalizePath($path),
102
            'settings' => $settings,
103
        ];
104
105
        return $this->rpcEndpointRequest('sharing/create_shared_link_with_settings', $parameters);
106
    }
107
108
    /**
109
     * List shared links.
110
     *
111
     * For empty path returns a list of all shared links. For non-empty path
112
     * returns a list of all shared links with access to the given path.
113
     *
114
     * If direct_only is set true, only direct links to the path will be returned, otherwise
115
     * it may return link to the path itself and parent folders as described on docs.
116
     *
117
     * @link https://www.dropbox.com/developers/documentation/http/documentation#sharing-list_shared_links
118
     */
119
    public function listSharedLinks(string $path = null, bool $direct_only = false, string $cursor = null): array
120
    {
121
        $parameters = [
122
            'path' => $path ? $this->normalizePath($path) : null,
123
            'cursor' => $cursor,
124
            'direct_only' => $direct_only,
125
        ];
126
127
        $body = $this->rpcEndpointRequest('sharing/list_shared_links', $parameters);
128
129
        return $body['links'];
130
    }
131
132
    /**
133
     * Delete the file or folder at a given path.
134
     *
135
     * If the path is a folder, all its contents will be deleted too.
136
     * A successful response indicates that the file or folder was deleted.
137
     *
138
     * @link https://www.dropbox.com/developers/documentation/http/documentation#files-delete
139
     */
140
    public function delete(string $path): array
141
    {
142
        $parameters = [
143
            'path' => $this->normalizePath($path),
144
        ];
145
146
        return $this->rpcEndpointRequest('files/delete', $parameters);
147
    }
148
149
    /**
150
     * Download a file from a user's Dropbox.
151
     *
152
     * @param string $path
153
     *
154
     * @return resource
155
     *
156
     * @link https://www.dropbox.com/developers/documentation/http/documentation#files-download
157
     */
158
    public function download(string $path)
159
    {
160
        $arguments = [
161
            'path' => $this->normalizePath($path),
162
        ];
163
164
        $response = $this->contentEndpointRequest('files/download', $arguments);
165
166
        return StreamWrapper::getResource($response->getBody());
167
    }
168
169
    /**
170
     * Returns the metadata for a file or folder.
171
     *
172
     * Note: Metadata for the root folder is unsupported.
173
     *
174
     * @link https://www.dropbox.com/developers/documentation/http/documentation#files-get_metadata
175
     */
176
    public function getMetadata(string $path): array
177
    {
178
        $parameters = [
179
            'path' => $this->normalizePath($path),
180
        ];
181
182
        return $this->rpcEndpointRequest('files/get_metadata', $parameters);
183
    }
184
185
    /**
186
     * Get a temporary link to stream content of a file.
187
     *
188
     * This link will expire in four hours and afterwards you will get 410 Gone.
189
     * Content-Type of the link is determined automatically by the file's mime type.
190
     *
191
     * @link https://www.dropbox.com/developers/documentation/http/documentation#files-get_temporary_link
192
     */
193 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...
194
    {
195
        $parameters = [
196
            'path' => $this->normalizePath($path),
197
        ];
198
199
        $body = $this->rpcEndpointRequest('files/get_temporary_link', $parameters);
200
201
        return $body['link'];
202
    }
203
204
    /**
205
     * Get a thumbnail for an image.
206
     *
207
     * This method currently supports files with the following file extensions:
208
     * jpg, jpeg, png, tiff, tif, gif and bmp.
209
     *
210
     * Photos that are larger than 20MB in size won't be converted to a thumbnail.
211
     *
212
     * @link https://www.dropbox.com/developers/documentation/http/documentation#files-get_thumbnail
213
     */
214
    public function getThumbnail(string $path, string $format = 'jpeg', string $size = 'w64h64'): string
215
    {
216
        $arguments = [
217
            'path' => $this->normalizePath($path),
218
            'format' => $format,
219
            'size' => $size,
220
        ];
221
222
        $response = $this->contentEndpointRequest('files/get_thumbnail', $arguments);
223
224
        return (string) $response->getBody();
225
    }
226
227
    /**
228
     * Starts returning the contents of a folder.
229
     *
230
     * If the result's ListFolderResult.has_more field is true, call
231
     * list_folder/continue with the returned ListFolderResult.cursor to retrieve more entries.
232
     *
233
     * Note: auth.RateLimitError may be returned if multiple list_folder or list_folder/continue calls
234
     * with same parameters are made simultaneously by same API app for same user. If your app implements
235
     * retry logic, please hold off the retry until the previous request finishes.
236
     *
237
     * @link https://www.dropbox.com/developers/documentation/http/documentation#files-list_folder
238
     */
239 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...
240
    {
241
        $parameters = [
242
            'path' => $this->normalizePath($path),
243
            'recursive' => $recursive,
244
        ];
245
246
        return $this->rpcEndpointRequest('files/list_folder', $parameters);
247
    }
248
249
    /**
250
     * Once a cursor has been retrieved from list_folder, use this to paginate through all files and
251
     * retrieve updates to the folder, following the same rules as documented for list_folder.
252
     *
253
     * @link https://www.dropbox.com/developers/documentation/http/documentation#files-list_folder-continue
254
     */
255
    public function listFolderContinue(string $cursor = ''): array
256
    {
257
        return $this->rpcEndpointRequest('files/list_folder/continue', compact('cursor'));
258
    }
259
260
    /**
261
     * Move a file or folder to a different location in the user's Dropbox.
262
     *
263
     * If the source path is a folder all its contents will be moved.
264
     *
265
     * @link https://www.dropbox.com/developers/documentation/http/documentation#files-move_v2
266
     */
267 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...
268
    {
269
        $parameters = [
270
            'from_path' => $this->normalizePath($fromPath),
271
            'to_path' => $this->normalizePath($toPath),
272
        ];
273
274
        return $this->rpcEndpointRequest('files/move_v2', $parameters);
275
    }
276
277
    /**
278
     * The file should be uploaded in chunks if it size exceeds the 150 MB threshold
279
     * or if the resource size could not be determined (eg. a popen() stream).
280
     *
281
     * @param string|resource $contents
282
     *
283
     * @return bool
284
     */
285
    protected function shouldUploadChunked($contents): bool
286
    {
287
        $size = is_string($contents) ? strlen($contents) : fstat($contents)['size'];
288
289
        if ($this->isPipe($contents)) {
290
            return true;
291
        }
292
293
        if ($size === null) {
294
            return true;
295
        }
296
297
        return $size > $this->maxChunkSize;
298
    }
299
300
    /**
301
     * Check if the contents is a pipe stream (not seekable, no size defined).
302
     *
303
     * @param string|resource $contents
304
     *
305
     * @return bool
306
     */
307
    protected function isPipe($contents): bool
308
    {
309
        return is_resource($contents) ? (fstat($contents)['mode'] & 010000) != 0 : false;
310
    }
311
312
    /**
313
     * Create a new file with the contents provided in the request.
314
     *
315
     * Do not use this to upload a file larger than 150 MB. Instead, create an upload session with upload_session/start.
316
     *
317
     * @link https://www.dropbox.com/developers/documentation/http/documentation#files-upload
318
     *
319
     * @param string $path
320
     * @param string|resource $contents
321
     * @param string|array $mode
322
     *
323
     * @return array
324
     */
325
    public function upload(string $path, $contents, $mode = 'add'): array
326
    {
327
        if ($this->shouldUploadChunked($contents)) {
328
            return $this->uploadChunked($path, $contents, $mode);
0 ignored issues
show
Bug introduced by
It seems like $mode defined by parameter $mode on line 325 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...
329
        }
330
331
        $arguments = [
332
            'path' => $this->normalizePath($path),
333
            'mode' => $mode,
334
        ];
335
336
        $response = $this->contentEndpointRequest('files/upload', $arguments, $contents);
337
338
        $metadata = json_decode($response->getBody(), true);
339
340
        $metadata['.tag'] = 'file';
341
342
        return $metadata;
343
    }
344
345
    /**
346
     * Upload file split in chunks. This allows uploading large files, since
347
     * Dropbox API v2 limits the content size to 150MB.
348
     *
349
     * The chunk size will affect directly the memory usage, so be careful.
350
     * Large chunks tends to speed up the upload, while smaller optimizes memory usage.
351
     *
352
     * @param string          $path
353
     * @param string|resource $contents
354
     * @param string          $mode
355
     * @param int             $chunkSize
356
     *
357
     * @return array
358
     */
359
    public function uploadChunked(string $path, $contents, $mode = 'add', $chunkSize = null): array
360
    {
361
        if ($chunkSize === null || $chunkSize > $this->maxChunkSize) {
362
            $chunkSize = $this->maxChunkSize;
363
        }
364
365
        $stream = Psr7\stream_for($contents);
366
367
        $chunk_stream = new Psr7\LimitStream($stream, $chunkSize, $stream->tell());
368
        $cursor = $this->uploadSessionStart($chunk_stream);
369
370
        while (! $stream->eof()) {
371
            $chunk_stream = new Psr7\LimitStream($stream, $chunkSize, $stream->tell());
372
            $cursor = $this->uploadSessionAppend($chunk_stream, $cursor);
373
        }
374
375
        return $this->uploadSessionFinish('', $cursor, $path, $mode);
376
    }
377
378
    /**
379
     * Upload sessions allow you to upload a single file in one or more requests,
380
     * for example where the size of the file is greater than 150 MB.
381
     * This call starts a new upload session with the given data.
382
     *
383
     * @link https://www.dropbox.com/developers/documentation/http/documentation#files-upload_session-start
384
     *
385
     * @param string|StreamInterface $contents
386
     * @param bool   $close
387
     *
388
     * @return UploadSessionCursor
389
     */
390
    public function uploadSessionStart($contents, bool $close = false): UploadSessionCursor
391
    {
392
        $arguments = compact('close');
393
394
        $response = json_decode(
395
            $this->contentEndpointRequest('files/upload_session/start', $arguments, $contents)->getBody(),
396
            true
397
        );
398
399
        return new UploadSessionCursor($response['session_id'], ($contents instanceof StreamInterface ? $contents->tell() : strlen($contents)));
400
    }
401
402
    /**
403
     * Append more data to an upload session.
404
     * When the parameter close is set, this call will close the session.
405
     * A single request should not upload more than 150 MB.
406
     *
407
     * @link https://www.dropbox.com/developers/documentation/http/documentation#files-upload_session-append_v2
408
     *
409
     * @param string|StreamInterface $contents
410
     * @param UploadSessionCursor $cursor
411
     * @param bool                $close
412
     *
413
     * @return \Spatie\Dropbox\UploadSessionCursor
414
     */
415
    public function uploadSessionAppend($contents, UploadSessionCursor $cursor, bool $close = false): UploadSessionCursor
416
    {
417
        $arguments = compact('cursor', 'close');
418
419
        $pos = $contents instanceof StreamInterface ? $contents->tell() : 0;
420
        $this->contentEndpointRequest('files/upload_session/append_v2', $arguments, $contents);
421
422
        $cursor->offset += $contents instanceof StreamInterface ? ($contents->tell() - $pos) : strlen($contents);
423
424
        return $cursor;
425
    }
426
427
    /**
428
     * Finish an upload session and save the uploaded data to the given file path.
429
     * A single request should not upload more than 150 MB.
430
     *
431
     * @link https://www.dropbox.com/developers/documentation/http/documentation#files-upload_session-finish
432
     *
433
     * @param string|StreamInterface              $contents
434
     * @param \Spatie\Dropbox\UploadSessionCursor $cursor
435
     * @param string                              $path
436
     * @param string|array                        $mode
437
     * @param bool                                $autorename
438
     * @param bool                                $mute
439
     *
440
     * @return array
441
     */
442
    public function uploadSessionFinish($contents, UploadSessionCursor $cursor, string $path, $mode = 'add', $autorename = false, $mute = false): array
443
    {
444
        $arguments = compact('cursor');
445
        $arguments['commit'] = compact('path', 'mode', 'autorename', 'mute');
446
447
        $response = $this->contentEndpointRequest(
448
            'files/upload_session/finish',
449
            $arguments,
450
            ($contents == '') ? null : $contents
451
        );
452
453
        $metadata = json_decode($response->getBody(), true);
454
455
        $metadata['.tag'] = 'file';
456
457
        return $metadata;
458
    }
459
460
    /**
461
     * Get Account Info for current authenticated user.
462
     *
463
     * @link https://www.dropbox.com/developers/documentation/http/documentation#users-get_current_account
464
     *
465
     * @return array
466
     */
467
    public function getAccountInfo(): array
468
    {
469
        return $this->rpcEndpointRequest('users/get_current_account');
470
    }
471
472
    /**
473
     * Revoke current access token.
474
     *
475
     * @link https://www.dropbox.com/developers/documentation/http/documentation#auth-token-revoke
476
     */
477
    public function revokeToken()
478
    {
479
        $this->rpcEndpointRequest('auth/token/revoke');
480
    }
481
482
    protected function normalizePath(string $path): string
483
    {
484
        $path = trim($path, '/');
485
486
        return ($path === '') ? '' : '/'.$path;
487
    }
488
489
    /**
490
     * @param string $endpoint
491
     * @param array $arguments
492
     * @param string|resource|StreamInterface $body
493
     *
494
     * @return \Psr\Http\Message\ResponseInterface
495
     *
496
     * @throws \Exception
497
     */
498
    public function contentEndpointRequest(string $endpoint, array $arguments, $body = ''): ResponseInterface
499
    {
500
        $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...
501
502
        if ($body !== '') {
503
            $headers['Content-Type'] = 'application/octet-stream';
504
        }
505
506
        try {
507
            $response = $this->client->post("https://content.dropboxapi.com/2/{$endpoint}", [
508
                'headers' => $headers,
509
                'body' => $body,
510
            ]);
511
        } catch (ClientException $exception) {
512
            throw $this->determineException($exception);
513
        }
514
515
        return $response;
516
    }
517
518
    public function rpcEndpointRequest(string $endpoint, array $parameters = null): array
519
    {
520
        try {
521
            $options = [];
522
523
            if ($parameters) {
524
                $options['json'] = $parameters;
525
            }
526
527
            $response = $this->client->post("https://api.dropboxapi.com/2/{$endpoint}", $options);
528
        } catch (ClientException $exception) {
529
            throw $this->determineException($exception);
530
        }
531
532
        $response = json_decode($response->getBody(), true);
533
534
        return $response ?? [];
535
    }
536
537
    protected function determineException(ClientException $exception): Exception
538
    {
539
        if (in_array($exception->getResponse()->getStatusCode(), [400, 409])) {
540
            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...
541
        }
542
543
        return $exception;
544
    }
545
}
546