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 ( 91f40b...8fe639 )
by Freek
02:12
created

DropboxAdapter::listContents()   B

Complexity

Conditions 3
Paths 2

Size

Total Lines 25
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 13
nc 2
nop 2
1
<?php
2
3
namespace Spatie\FlysystemDropbox;
4
5
use LogicException;
6
use Spatie\Dropbox\Client;
7
use League\Flysystem\Config;
8
use Spatie\Dropbox\Exceptions\BadRequest;
9
use League\Flysystem\Adapter\AbstractAdapter;
10
use League\Flysystem\Adapter\Polyfill\NotSupportingVisibilityTrait;
11
12
class DropboxAdapter extends AbstractAdapter
13
{
14
    use NotSupportingVisibilityTrait;
15
16
    /** @var \Spatie\Dropbox\Client */
17
    protected $client;
18
19
    public function __construct(Client $client, string $prefix = '')
20
    {
21
        $this->client = $client;
22
23
        $this->setPathPrefix($prefix);
24
    }
25
26
    /**
27
     * {@inheritdoc}
28
     */
29
    public function write($path, $contents, Config $config)
30
    {
31
        return $this->upload($path, $contents, 'add');
32
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37
    public function writeStream($path, $resource, Config $config)
38
    {
39
        return $this->upload($path, $resource, 'add');
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45
    public function update($path, $contents, Config $config)
46
    {
47
        return $this->upload($path, $contents, 'overwrite');
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53
    public function updateStream($path, $resource, Config $config)
54
    {
55
        return $this->upload($path, $resource, 'overwrite');
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61
    public function rename($path, $newPath): bool
62
    {
63
        $path = $this->applyPathPrefix($path);
64
        $newPath = $this->applyPathPrefix($newPath);
65
66
        try {
67
            $this->client->move($path, $newPath);
68
        } catch (BadRequest $e) {
69
            return false;
70
        }
71
72
        return true;
73
    }
74
75
    /**
76
     * {@inheritdoc}
77
     */
78 View Code Duplication
    public function copy($path, $newpath): bool
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...
79
    {
80
        $path = $this->applyPathPrefix($path);
81
        $newpath = $this->applyPathPrefix($newpath);
82
83
        try {
84
            $this->client->copy($path, $newpath);
85
        } catch (BadRequest $e) {
86
            return false;
87
        }
88
89
        return true;
90
    }
91
92
    /**
93
     * {@inheritdoc}
94
     */
95
    public function delete($path): bool
96
    {
97
        $location = $this->applyPathPrefix($path);
98
99
        try {
100
            $this->client->delete($location);
101
        } catch (BadRequest $e) {
102
            return false;
103
        }
104
105
        return true;
106
    }
107
108
    /**
109
     * {@inheritdoc}
110
     */
111
    public function deleteDir($dirname): bool
112
    {
113
        return $this->delete($dirname);
114
    }
115
116
    /**
117
     * {@inheritdoc}
118
     */
119 View Code Duplication
    public function createDir($dirname, Config $config)
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...
120
    {
121
        $path = $this->applyPathPrefix($dirname);
122
123
        try {
124
            $object = $this->client->createFolder($path);
125
        } catch (BadRequest $e) {
126
            return false;
127
        }
128
129
        return $this->normalizeResponse($object);
130
    }
131
132
    /**
133
     * {@inheritdoc}
134
     */
135
    public function has($path)
136
    {
137
        return $this->getMetadata($path);
138
    }
139
140
    /**
141
     * {@inheritdoc}
142
     */
143
    public function read($path)
144
    {
145
        if (! $object = $this->readStream($path)) {
146
            return false;
147
        }
148
149
        $object['contents'] = stream_get_contents($object['stream']);
150
        fclose($object['stream']);
151
        unset($object['stream']);
152
153
        return $object;
154
    }
155
156
    /**
157
     * {@inheritdoc}
158
     */
159 View Code Duplication
    public function readStream($path)
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...
160
    {
161
        $path = $this->applyPathPrefix($path);
162
163
        try {
164
            $stream = $this->client->download($path);
165
        } catch (BadRequest $e) {
166
            return false;
167
        }
168
169
        return compact('stream');
170
    }
171
172
    /**
173
     * {@inheritdoc}
174
     */
175
    public function listContents($directory = '', $recursive = false): array
176
    {
177
        $location = $this->applyPathPrefix($directory);
178
179
        $result = $this->client->listFolder($location, $recursive);
180
181
        if (! count($result['entries'])) {
182
            return [];
183
        }
184
185
        $cleanedPathDisplay = $this->getCleanedPathDisplay($result['entries']);
186
187
        return array_map(function ($entry) use ($cleanedPathDisplay) {
188
            $path = $this->removePathPrefix($entry['path_display']);
189
190
            // use cleaned path display to fix path case
191
            foreach ($cleanedPathDisplay as $pathLower => $pathDisplay) {
192
                $path = preg_replace('/^'.preg_quote($pathLower, '/').'/i', $pathDisplay, $path);
193
            }
194
195
            $entry['path_display'] = $path;
196
197
            return $this->normalizeResponse($entry);
198
        }, $result['entries']);
199
    }
200
201
    /**
202
     * {@inheritdoc}
203
     */
204 View Code Duplication
    public function getMetadata($path)
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...
205
    {
206
        $path = $this->applyPathPrefix($path);
207
208
        try {
209
            $object = $this->client->getMetadata($path);
210
        } catch (BadRequest $e) {
211
            return false;
212
        }
213
214
        return $this->normalizeResponse($object);
215
    }
216
217
    /**
218
     * {@inheritdoc}
219
     */
220
    public function getSize($path)
221
    {
222
        return $this->getMetadata($path);
223
    }
224
225
    /**
226
     * {@inheritdoc}
227
     */
228
    public function getMimetype($path)
229
    {
230
        throw new LogicException("The Dropbox API v2 does not support mimetypes. Given path: `{$path}`.");
231
    }
232
233
    /**
234
     * {@inheritdoc}
235
     */
236
    public function getTimestamp($path)
237
    {
238
        return $this->getMetadata($path);
239
    }
240
241
    public function getTemporaryLink(string $path): string
242
    {
243
        return $this->client->getTemporaryLink($path);
244
    }
245
246
    public function getThumbnail(string $path, string $format = 'jpeg', string $size = 'w64h64')
247
    {
248
        return $this->client->getThumbnail($path, $format, $size);
249
    }
250
251
    /**
252
     * {@inheritdoc}
253
     */
254
    public function applyPathPrefix($path): string
255
    {
256
        $path = parent::applyPathPrefix($path);
257
258
        return '/'.trim($path, '/');
259
    }
260
261
    public function getClient(): Client
262
    {
263
        return $this->client;
264
    }
265
266
    /**
267
     * @param string $path
268
     * @param resource|string $contents
269
     * @param string $mode
270
     *
271
     * @return array|false file metadata
272
     */
273 View Code Duplication
    protected function upload(string $path, $contents, string $mode)
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...
274
    {
275
        $path = $this->applyPathPrefix($path);
276
277
        try {
278
            $object = $this->client->upload($path, $contents, $mode);
279
        } catch (BadRequest $e) {
280
            return false;
281
        }
282
283
        return $this->normalizeResponse($object);
284
    }
285
286
    protected function normalizeResponse(array $response): array
287
    {
288
        $normalizedPath = ltrim($this->removePathPrefix($response['path_display']), '/');
289
290
        $normalizedResponse = ['path' => $normalizedPath];
291
292
        if (isset($response['server_modified'])) {
293
            $normalizedResponse['timestamp'] = strtotime($response['server_modified']);
294
        }
295
296
        if (isset($response['size'])) {
297
            $normalizedResponse['size'] = $response['size'];
298
            $normalizedResponse['bytes'] = $response['size'];
299
        }
300
301
        $type = ($response['.tag'] === 'folder' ? 'dir' : 'file');
302
        $normalizedResponse['type'] = $type;
303
304
        return $normalizedResponse;
305
    }
306
307
    protected function getCleanedPathDisplay($entries)
308
    {
309
        // init temp associative array that will contains
310
        // path lower as key and path display as value
311
        $cleanedPathDisplay = [];
312
        foreach ($entries as $entry) {
313
            // we only need folder paths
314
            if ($entry['.tag'] === 'folder') {
315
                // add this folder path association to temp array
316
                $cleanedPathDisplay[$entry['path_lower']] = $entry['path_display'];
317
318
                // search for parent cleaned path display
319
                $cleanedPathDisplay = $this->addParentsToCleanedPathDisplay($entry, $cleanedPathDisplay);
320
            }
321
        }
322
323
        // reverse to get deep paths first
324
        $cleanedPathDisplay = array_reverse($cleanedPathDisplay);
325
326
        return $cleanedPathDisplay;
327
    }
328
329
    protected function addParentsToCleanedPathDisplay($entry, $cleanedPathDisplay)
330
    {
331
        // try to find parent paths that we did not know
332
        $pathParts = explode('/', $entry['path_lower']);
333
        do {
334
            // up to parent
335
            array_pop($pathParts);
336
            $parentPathLower = implode('/', $pathParts);
337
338
            // if we did not know this path
339
            // get the path display from Dropbox and add it to temp assoc array
340
            if (! array_key_exists($parentPathLower, $cleanedPathDisplay)) {
341
                $prefixedPath = $this->applyPathPrefix($parentPathLower);
342
                $metadata = $this->client->getMetadata($prefixedPath);
343
                $cleanedPathDisplay = array_merge(
344
                    [$metadata['path_lower'] => $metadata['path_display']],
345
                    $cleanedPathDisplay
346
                );
347
            } else {
348
                // if this path is known, parents will do too
349
                // so we can stop this loop by emptying path parts
350
                $pathParts = [];
351
            }
352
        } while (count($pathParts) > 2);
353
354
        return $cleanedPathDisplay;
355
    }
356
}
357