Passed
Push — master ( da854f...7450e6 )
by Carlos
02:47 queued 57s
created

QiniuAdapter   B

Complexity

Total Complexity 43

Size/Duplication

Total Lines 472
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 6

Test Coverage

Coverage 95.61%

Importance

Changes 0
Metric Value
wmc 43
lcom 2
cbo 6
dl 0
loc 472
ccs 109
cts 114
cp 0.9561
rs 8.3157
c 0
b 0
f 0

29 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A write() 0 22 3
A writeStream() 0 16 3
A update() 0 6 1
A updateStream() 0 6 1
A rename() 0 6 1
A copy() 0 6 1
A delete() 0 6 1
A deleteDir() 0 4 1
A createDir() 0 4 1
A has() 0 6 2
A getUrl() 0 4 1
A read() 0 6 1
A readStream() 0 10 2
A listContents() 0 12 2
A getMetadata() 0 7 1
A getSize() 0 4 1
A fetch() 0 10 2
A getMimeType() 0 10 2
A getTimestamp() 0 4 1
A setBucketManager() 0 6 1
A setUploadManager() 0 6 1
A setAuthManager() 0 6 1
A getBucketManager() 0 4 2
A getAuthManager() 0 4 2
A getUploadManager() 0 4 2
A getUploadToken() 0 4 1
A normalizeFileInfo() 0 9 1
A normalizeHost() 0 8 3

How to fix   Complexity   

Complex Class

Complex classes like QiniuAdapter often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use QiniuAdapter, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
/*
4
 * This file is part of the overtrue/flysystem-qiniu.
5
 * (c) overtrue <[email protected]>
6
 * This source file is subject to the MIT license that is bundled
7
 * with this source code in the file LICENSE.
8
 */
9
10
namespace Overtrue\Flysystem\Qiniu;
11
12
use League\Flysystem\Adapter\AbstractAdapter;
13
use League\Flysystem\Adapter\Polyfill\NotSupportingVisibilityTrait;
14
use League\Flysystem\Config;
15
use Qiniu\Auth;
16
use Qiniu\Storage\BucketManager;
17
use Qiniu\Storage\UploadManager;
18
19
/**
20
 * Class QiniuAdapter.
21
 *
22
 * @author overtrue <[email protected]>
23
 */
24
class QiniuAdapter extends AbstractAdapter
25
{
26
    use NotSupportingVisibilityTrait;
27
28
    /**
29
     * @var string
30
     */
31
    protected $accessKey;
32
33
    /**
34
     * @var string
35
     */
36
    protected $secretKey;
37
38
    /**
39
     * @var string
40
     */
41
    protected $bucket;
42
43
    /**
44
     * @var string
45
     */
46
    protected $domain;
47
48
    /**
49
     * @var \Qiniu\Auth
50
     */
51
    protected $authManager;
52
53
    /**
54
     * @var \Qiniu\Storage\UploadManager
55
     */
56
    protected $uploadManager;
57
58
    /**
59
     * @var \Qiniu\Storage\BucketManager
60
     */
61
    protected $bucketManager;
62
63
    /**
64
     * QiniuAdapter constructor.
65
     *
66
     * @param string $accessKey
67
     * @param string $secretKey
68
     * @param string $bucket
69
     * @param string $domain
70
     */
71 1
    public function __construct($accessKey, $secretKey, $bucket, $domain)
72
    {
73 1
        $this->accessKey = $accessKey;
74 1
        $this->secretKey = $secretKey;
75 1
        $this->bucket = $bucket;
76 1
        $this->domain = $domain;
77 1
    }
78
79
    /**
80
     * Write a new file.
81
     *
82
     * @param string $path
83
     * @param string $contents
84
     * @param Config $config   Config object
85
     *
86
     * @return array|false false on failure file meta data on success
87
     */
88 2
    public function write($path, $contents, Config $config)
89
    {
90 2
        $mime = 'application/octet-stream';
91
92 2
        if ($config->has('mime')) {
93 1
            $mime = $config->get('mime');
94 1
        }
95
96 2
        list($response, $error) = $this->getUploadManager()->put(
97 2
            $this->getAuthManager()->uploadToken($this->bucket),
98 2
            $path,
99 2
            $contents,
100 2
            null,
101
            $mime
102 2
        );
103
104 2
        if ($error) {
105 2
            return false;
106
        }
107
108 2
        return $response;
109
    }
110
111
    /**
112
     * Write a new file using a stream.
113
     *
114
     * @param string   $path
115
     * @param resource $resource
116
     * @param Config   $config   Config object
117
     *
118
     * @return array|false false on failure file meta data on success
119
     */
120 1
    public function writeStream($path, $resource, Config $config)
121
    {
122 1
        $contents = '';
123
124 1
        while (!feof($resource)) {
125 1
            $contents .= fread($resource, 1024);
126 1
        }
127
128 1
        $response = $this->write($path, $contents, $config);
129
130 1
        if ($response === false) {
131 1
            return $response;
132
        }
133
134 1
        return compact('path');
135
    }
136
137
    /**
138
     * Update a file.
139
     *
140
     * @param string $path
141
     * @param string $contents
142
     * @param Config $config   Config object
143
     *
144
     * @return array|false false on failure file meta data on success
145
     */
146 1
    public function update($path, $contents, Config $config)
147
    {
148 1
        $this->delete($path);
149
150 1
        return $this->write($path, $contents, $config);
151
    }
152
153
    /**
154
     * Update a file using a stream.
155
     *
156
     * @param string   $path
157
     * @param resource $resource
158
     * @param Config   $config   Config object
159
     *
160
     * @return array|false false on failure file meta data on success
161
     */
162 1
    public function updateStream($path, $resource, Config $config)
163
    {
164 1
        $this->delete($path);
165
166 1
        return $this->writeStream($path, $resource, $config);
167
    }
168
169
    /**
170
     * Rename a file.
171
     *
172
     * @param string $path
173
     * @param string $newPath
174
     *
175
     * @return bool
176
     */
177 1
    public function rename($path, $newPath)
178
    {
179 1
        $response = $this->getBucketManager()->rename($this->bucket, $path, $newPath);
180
181 1
        return is_null($response);
182
    }
183
184
    /**
185
     * Copy a file.
186
     *
187
     * @param string $path
188
     * @param string $newPath
189
     *
190
     * @return bool
191
     */
192 1
    public function copy($path, $newPath)
193
    {
194 1
        $response = $this->getBucketManager()->copy($this->bucket, $path, $this->bucket, $newPath);
195
196 1
        return is_null($response);
197
    }
198
199
    /**
200
     * Delete a file.
201
     *
202
     * @param string $path
203
     *
204
     * @return bool
205
     */
206 1
    public function delete($path)
207
    {
208 1
        $response = $this->getBucketManager()->delete($this->bucket, $path);
209
210 1
        return is_null($response);
211
    }
212
213
    /**
214
     * Delete a directory.
215
     *
216
     * @param string $directory
217
     *
218
     * @return bool
219
     */
220 1
    public function deleteDir($directory)
221
    {
222 1
        return true;
223
    }
224
225
    /**
226
     * Create a directory.
227
     *
228
     * @param string $directory directory name
229
     * @param Config $config
230
     *
231
     * @return array|false
232
     */
233 1
    public function createDir($directory, Config $config)
234
    {
235 1
        return ['path' => $directory, 'type' => 'dir'];
236
    }
237
238
    /**
239
     * Check whether a file exists.
240
     *
241
     * @param string $path
242
     *
243
     * @return array|bool|null
244
     */
245 1
    public function has($path)
246
    {
247 1
        list($response, $error) = $this->getBucketManager()->stat($this->bucket, $path);
248
249 1
        return !$error || is_array($response);
250
    }
251
252
    /**
253
     * Get resource url.
254
     *
255
     * @param string $path
256
     *
257
     * @return string
258
     */
259 1
    public function getUrl($path)
260
    {
261 1
        return $this->normalizeHost($this->domain).$path;
262
    }
263
264
    /**
265
     * Read a file.
266
     *
267
     * @param string $path
268
     *
269
     * @return array|false
270
     */
271 1
    public function read($path)
272
    {
273 1
        $contents = file_get_contents($this->getUrl($path));
274
275 1
        return compact('contents', 'path');
276
    }
277
278
    /**
279
     * Read a file as a stream.
280
     *
281
     * @param string $path
282
     *
283
     * @return array|false
284
     */
285 1
    public function readStream($path)
286
    {
287 1
        if (ini_get('allow_url_fopen')) {
288 1
            $stream = fopen($this->normalizeHost($this->domain).$path, 'r');
289
290 1
            return compact('stream', 'path');
291
        }
292
293 1
        return false;
294
    }
295
296
    /**
297
     * List contents of a directory.
298
     *
299
     * @param string $directory
300
     * @param bool   $recursive
301
     *
302
     * @return array
303
     */
304 1
    public function listContents($directory = '', $recursive = false)
305
    {
306 1
        $list = [];
307
308 1
        $result = $this->getBucketManager()->listFiles($this->bucket, $directory);
309
310 1
        foreach ($result[0] as $files) {
311 1
            $list[] = $this->normalizeFileInfo($files);
312 1
        }
313
314 1
        return $list;
315
    }
316
317
    /**
318
     * Get all the meta data of a file or directory.
319
     *
320
     * @param string $path
321
     *
322
     * @return array|false
323
     */
324 1
    public function getMetadata($path)
325
    {
326 1
        $result = $this->getBucketManager()->stat($this->bucket, $path);
327 1
        $result[0]['key'] = $path;
328
329 1
        return $this->normalizeFileInfo($result[0]);
0 ignored issues
show
Documentation introduced by
$result[0] is of type null|object<Qiniu\Http\Error>, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
330
    }
331
332
    /**
333
     * Get the size of a file.
334
     *
335
     * @param string $path
336
     *
337
     * @return array|false
338
     */
339 1
    public function getSize($path)
340
    {
341 1
        return $this->getMetadata($path);
342
    }
343
344
    /**
345
     * Fetch url to bucket.
346
     *
347
     * @param string $path
348
     * @param string $url
349
     *
350
     * @return array|false
351
     */
352
    public function fetch($path, $url)
353
    {
354
        list($response, $error) = $this->getBucketManager()->fetch($url, $this->bucket, $path);
355
356
        if ($error) {
357
            return false;
358
        }
359
360
        return $response;
361
    }
362
363
    /**
364
     * Get the mime-type of a file.
365
     *
366
     * @param string $path
367
     *
368
     * @return array|false
369
     */
370 1
    public function getMimeType($path)
371
    {
372 1
        $response = $this->getBucketManager()->stat($this->bucket, $path);
373
374 1
        if (empty($response[0]['mimeType'])) {
375 1
            return false;
376
        }
377
378 1
        return ['mimetype' => $response[0]['mimeType']];
379
    }
380
381
    /**
382
     * Get the timestamp of a file.
383
     *
384
     * @param string $path
385
     *
386
     * @return array|false
387
     */
388 1
    public function getTimestamp($path)
389
    {
390 1
        return $this->getMetadata($path);
391
    }
392
393
    /**
394
     * @param \Qiniu\Storage\BucketManager $manager
395
     *
396
     * @return $this
397
     */
398 1
    public function setBucketManager(BucketManager $manager)
399
    {
400 1
        $this->bucketManager = $manager;
401
402 1
        return $this;
403
    }
404
405
    /**
406
     * @param \Qiniu\Storage\UploadManager $manager
407
     *
408
     * @return $this
409
     */
410 1
    public function setUploadManager(UploadManager $manager)
411
    {
412 1
        $this->uploadManager = $manager;
413
414 1
        return $this;
415
    }
416
417
    /**
418
     * @param \Qiniu\Auth $manager
419
     *
420
     * @return $this
421
     */
422 1
    public function setAuthManager(Auth $manager)
423
    {
424 1
        $this->authManager = $manager;
425
426 1
        return $this;
427
    }
428
429
    /**
430
     * @return \Qiniu\Storage\BucketManager
431
     */
432 1
    public function getBucketManager()
433
    {
434 1
        return $this->bucketManager ?: $this->bucketManager = new BucketManager($this->getAuthManager());
435
    }
436
437
    /**
438
     * @return \Qiniu\Auth
439
     */
440 1
    public function getAuthManager()
441
    {
442 1
        return $this->authManager ?: $this->authManager = new Auth($this->accessKey, $this->secretKey);
443
    }
444
445
    /**
446
     * @return \Qiniu\Storage\UploadManager
447
     */
448 1
    public function getUploadManager()
449
    {
450 1
        return $this->uploadManager ?: $this->uploadManager = new UploadManager();
451
    }
452
453
    /**
454
     * Get the upload token.
455
     *
456
     * @param string|null $key
457
     * @param int         $expires
458
     * @param bool        $policy
459
     *
460
     * @return string
461
     */
462 1
    public function getUploadToken($key = null, $expires = 3600, $policy = true)
463
    {
464 1
        return $this->getAuthManager()->uploadToken($this->bucket, $key, $expires, $policy);
465
    }
466
467
    /**
468
     * @param array $stats
469
     *
470
     * @return array
471
     */
472 2
    protected function normalizeFileInfo(array $stats)
473
    {
474
        return [
475 2
            'type' => 'file',
476 2
            'path' => $stats['key'],
477 2
            'timestamp' => floor($stats['putTime'] / 10000000),
478 2
            'size' => $stats['fsize'],
479 2
        ];
480
    }
481
482
    /**
483
     * @param string $domain
484
     *
485
     * @return string
486
     */
487 2
    protected function normalizeHost($domain)
488
    {
489 2
        if (0 !== stripos($domain, 'https://') && 0 !== stripos($domain, 'http://')) {
490 2
            $domain = "http://{$domain}";
491 2
        }
492
493 2
        return rtrim($domain, '/').'/';
494
    }
495
}
496