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
Push — master ( b2b704...9872a9 )
by
unknown
03:57 queued 01:45
created

Client::listFolder()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 9
Ratio 100 %

Importance

Changes 0
Metric Value
dl 9
loc 9
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 2
1
<?php
2
3
namespace Spatie\Dropbox;
4
5
use GuzzleHttp\Client as GuzzleClient;
6
use GuzzleHttp\Psr7\StreamWrapper;
7
8
class Client
9
{
10
    const THUMBNAIL_FORMAT_JPEG = 'jpeg';
11
    const THUMBNAIL_FORMAT_PNG = 'png';
12
13
    const THUMBNAIL_SIZE_XS = 'w32h32';
14
    const THUMBNAIL_SIZE_S = 'w64h64';
15
    const THUMBNAIL_SIZE_M = 'w128h128';
16
    const THUMBNAIL_SIZE_L = 'w640h480';
17
    const THUMBNAIL_SIZE_XL = 'w1024h768';
18
19
    protected $accessToken;
20
21
    protected $client;
22
23
    public function __construct(string $accessToken)
24
    {
25
        $this->accessToken = $accessToken;
26
27
        $this->client = new GuzzleClient([
28
            'headers' => [
29
                'Authorization' => "Bearer {$this->accessToken}",
30
            ],
31
        ]);
32
    }
33
34
    /**
35
     * Copy a file or folder to a different location in the user's Dropbox.
36
     * If the source path is a folder all its contents will be copied.
37
     *
38
     * @link https://www.dropbox.com/developers/documentation/http/documentation#files-copy
39
     */
40 View Code Duplication
    public function copy(string $path, string $newPath): 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...
41
    {
42
        $parameters = [
43
            'from_path' => $this->normalizePath($path),
44
            'to_path' => $this->normalizePath($newPath),
45
        ];
46
47
        return $this->requestRPC('files/copy', $parameters);
48
    }
49
50
    /**
51
     * Create a folder at a given path.Create a folder at a given path.
52
     *
53
     * @link https://www.dropbox.com/developers/documentation/http/documentation#files-create_folder
54
     */
55
    public function createFolder(string $path): array
56
    {
57
        $parameters = [
58
            'path' => $this->normalizePath($path),
59
        ];
60
61
        $object = $this->requestRPC('files/create_folder', $parameters);
62
63
        $object['.tag'] = 'folder';
64
65
        return $object;
66
    }
67
68
    /**
69
     * Delete the file or folder at a given path.
70
     * If the path is a folder, all its contents will be deleted too.
71
     * A successful response indicates that the file or folder was deleted.
72
     *
73
     * @link https://www.dropbox.com/developers/documentation/http/documentation#files-delete
74
     */
75
    public function delete(string $path): array
76
    {
77
        $parameters = [
78
            'path' => $this->normalizePath($path),
79
        ];
80
81
        return $this->requestRPC('files/delete', $parameters);
82
    }
83
84
    /**
85
     * Download a file from a user's Dropbox.
86
     *
87
     * @link https://www.dropbox.com/developers/documentation/http/documentation#files-download
88
     */
89
    public function download(string $path): resource
90
    {
91
        $dropboxApiArguments = [
92
            'path' => $this->normalizePath($path),
93
        ];
94
95
        $response = $this->client->post('https://content.dropboxapi.com/2/files/download', [
96
            'headers' => [
97
                'Dropbox-API-Arg' => json_encode($dropboxApiArguments),
98
            ],
99
        ]);
100
101
        return StreamWrapper::getResource($response->getBody());
102
    }
103
104
    /**
105
     * Returns the metadata for a file or folder.
106
     * Note: Metadata for the root folder is unsupported.
107
     *
108
     * @link https://www.dropbox.com/developers/documentation/http/documentation#files-get_metadata
109
     */
110
    public function getMetadata(string $path): array
111
    {
112
        $parameters = [
113
            'path' => $this->normalizePath($path),
114
        ];
115
116
        return $this->requestRPC('files/get_metadata', $parameters);
117
    }
118
119
    /**
120
     * Get a temporary link to stream content of a file. This link will expire in four hours and afterwards you will get 410 Gone.
121
     * Content-Type of the link is determined automatically by the file's mime type.
122
     *
123
     * @link https://www.dropbox.com/developers/documentation/http/documentation#files-get_temporary_link
124
     */
125 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...
126
    {
127
        $parameters = [
128
            'path' => $this->normalizePath($path),
129
        ];
130
131
        $body = $this->requestRPC('files/get_temporary_link', $parameters);
132
133
        return $body['link'];
134
    }
135
136
    /**
137
     * Get a thumbnail for an image.
138
     * This method currently supports files with the following file extensions: jpg, jpeg, png, tiff, tif, gif and bmp.
139
     * Photos that are larger than 20MB in size won't be converted to a thumbnail.
140
     *
141
     * @link https://www.dropbox.com/developers/documentation/http/documentation#files-get_thumbnail
142
     */
143
    public function getThumbnail(string $path, string $format = 'jpeg', string $size = 'w64h64'): string
144
    {
145
        $dropboxApiArguments = [
146
            'path' => $this->normalizePath($path),
147
            'format' => $format,
148
            'size' => $size
149
        ];
150
151
        $response = $this->client->post('https://content.dropboxapi.com/2/files/get_thumbnail', [
152
            'headers' => [
153
                'Dropbox-API-Arg' => json_encode($dropboxApiArguments),
154
            ],
155
        ]);
156
157
        return (string) $response->getBody();
158
    }
159
160
    /**
161
     * Starts returning the contents of a folder. If the result's ListFolderResult.has_more field is true, call
162
     * list_folder/continue with the returned ListFolderResult.cursor to retrieve more entries.
163
     *
164
     * Note: auth.RateLimitError may be returned if multiple list_folder or list_folder/continue calls with same parameters
165
     * are made simultaneously by same API app for same user. If your app implements retry logic, please hold off the retry
166
     * until the previous request finishes.
167
     *
168
     * @link https://www.dropbox.com/developers/documentation/http/documentation#files-list_folder
169
     */
170 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...
171
    {
172
        $parameters = [
173
            'path' => $this->normalizePath($path),
174
            'recursive' => $recursive,
175
        ];
176
177
        return $this->requestRPC('files/list_folder', $parameters);
178
    }
179
180
    /**
181
     * Move a file or folder to a different location in the user's Dropbox.
182
     * If the source path is a folder all its contents will be moved.
183
     *
184
     * @link https://www.dropbox.com/developers/documentation/http/documentation#files-move
185
     */
186 View Code Duplication
    public function move(string $path, string $newPath): 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...
187
    {
188
        $parameters = [
189
            'from_path' => $this->normalizePath($path),
190
            'to_path' => $this->normalizePath($newPath),
191
        ];
192
193
        return $this->requestRPC('files/move', $parameters);
194
    }
195
196
    /**
197
     * Create a new file with the contents provided in the request.
198
     * Do not use this to upload a file larger than 150 MB. Instead, create an upload session with upload_session/start.
199
     *
200
     * @link https://www.dropbox.com/developers/documentation/http/documentation#files-upload
201
     */
202
    public function upload(string $path, string $mode, $contents): array
203
    {
204
        $dropboxApiArguments = [
205
            'path' => $this->normalizePath($path),
206
            'mode' => $mode,
207
        ];
208
209
        $response = $this->client->post('https://content.dropboxapi.com/2/files/upload', [
210
            'headers' => [
211
                'Dropbox-API-Arg' => json_encode($dropboxApiArguments),
212
                'Content-Type' => 'application/octet-stream',
213
            ],
214
            'body' => $contents,
215
        ]);
216
217
        $metadata = json_decode($response->getBody(), true);
218
219
        $metadata['.tag'] = 'file';
220
221
        return $metadata;
222
    }
223
224
    protected function normalizePath(string $path): string
225
    {
226
        $path = trim($path,'/');
227
228
        if ($path === '') {
229
            return '';
230
        }
231
232
        return '/'.$path;
233
    }
234
235
    protected function requestRPC(string $endpoint, array $parameters): array
236
    {
237
        $response = $this->client->post("https://api.dropboxapi.com/2/{$endpoint}", [
238
            'json' => $parameters
239
        ]);
240
241
        return json_decode($response->getBody(), true);
242
    }
243
}
244