Test Failed
Push — master ( 0291bb...2df046 )
by frey
06:26 queued 01:06
created

Adapter   F

Complexity

Total Complexity 60

Size/Duplication

Total Lines 547
Duplicated Lines 0 %

Test Coverage

Coverage 81.35%

Importance

Changes 0
Metric Value
eloc 173
dl 0
loc 547
ccs 157
cts 193
cp 0.8135
rs 3.6
c 0
b 0
f 0
wmc 60

32 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getBucketWithAppId() 0 3 1
A getAppId() 0 3 1
A getBucket() 0 6 1
A getSourcePath() 0 4 1
A getRegion() 0 4 2
A write() 0 5 1
A rename() 0 7 1
A copy() 0 5 1
A updateStream() 0 3 1
A delete() 0 5 1
A getTemporaryUrl() 0 16 2
A update() 0 3 1
A writeStream() 0 9 1
A getUrl() 0 19 3
A getMetadata() 0 6 1
A getVisibility() 0 17 5
A getTimestamp() 0 6 2
A listObjects() 0 6 3
A getMimetype() 0 6 2
A getHttpClient() 0 5 1
A normalizeFileInfo() 0 13 3
A readStream() 0 15 3
A has() 0 6 2
A read() 0 20 5
A deleteDir() 0 24 3
A listContents() 0 11 2
A createDir() 0 6 1
A prepareUploadConfig() 0 13 3
A setVisibility() 0 6 1
A getSize() 0 6 2
A normalizeVisibility() 0 9 2

How to fix   Complexity   

Complex Class

Complex classes like Adapter 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.

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 Adapter, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace Freyo\Flysystem\QcloudCOSv5;
4
5
use Carbon\Carbon;
6
use DateTimeInterface;
7
use League\Flysystem\Adapter\AbstractAdapter;
8
use League\Flysystem\Adapter\CanOverwriteFiles;
9
use League\Flysystem\AdapterInterface;
10
use League\Flysystem\Config;
11
use Qcloud\Cos\Client;
12
use Qcloud\Cos\Exception\NoSuchKeyException;
13
14
/**
15
 * Class Adapter.
16
 */
17
class Adapter extends AbstractAdapter implements CanOverwriteFiles
18
{
19
    /**
20
     * @var Client
21
     */
22
    protected $client;
23
24
    /**
25
     * @var array
26
     */
27
    protected $config = [];
28
29
    /**
30
     * @var array
31
     */
32
    protected $regionMap = [
33
        'cn-east'      => 'ap-shanghai',
34
        'cn-sorth'     => 'ap-guangzhou',
35
        'cn-north'     => 'ap-beijing-1',
36
        'cn-south-2'   => 'ap-guangzhou-2',
37
        'cn-southwest' => 'ap-chengdu',
38
        'sg'           => 'ap-singapore',
39
        'tj'           => 'ap-beijing-1',
40
        'bj'           => 'ap-beijing',
41
        'sh'           => 'ap-shanghai',
42
        'gz'           => 'ap-guangzhou',
43
        'cd'           => 'ap-chengdu',
44
        'sgp'          => 'ap-singapore',
45
    ];
46
47
    /**
48
     * Adapter constructor.
49
     *
50
     * @param Client $client
51
     * @param array  $config
52
     */
53
    public function __construct(Client $client, array $config)
54
    {
55
        $this->client = $client;
56
        $this->config = $config;
57
58
        $this->setPathPrefix($config['cdn']);
59
    }
60
61
    /**
62
     * @return string
63
     */
64 2
    public function getBucketWithAppId()
65 1
    {
66 2
        return $this->getBucket().'-'.$this->getAppId();
67
    }
68
69
    /**
70
     * @return string
71
     */
72 19
    public function getBucket()
73
    {
74 19
        return preg_replace(
75
            "/-{$this->getAppId()}$/",
76
            '',
77
            $this->config['bucket']
78
        );
79
    }
80 19
81
    /**
82 19
     * @return string
83
     */
84
    public function getAppId()
85
    {
86
        return $this->config['credentials']['appId'];
87
    }
88 2
89
    /**
90 2
     * @return string
91 2
     */
92
    public function getRegion()
93
    {
94
        return array_key_exists($this->config['region'], $this->regionMap)
95
            ? $this->regionMap[$this->config['region']] : $this->config['region'];
96
    }
97
98
    /**
99 2
     * @param $path
100
     *
101 2
     * @return string
102 2
     */
103 2
    public function getSourcePath($path)
104
    {
105
        return sprintf('%s.cos.%s.myqcloud.com/%s',
106
            $this->getBucketWithAppId(), $this->getRegion(), $path
107
        );
108
    }
109
110
    /**
111 3
     * @param string $path
112
     *
113 3
     * @return string
114 3
     */
115
    public function getUrl($path)
116
    {
117
        if ($this->config['cdn']) {
118
            return $this->applyPathPrefix($path);
119
        }
120
121
        $options = [
122
            'Scheme' => isset($this->config['scheme']) ? $this->config['scheme'] : 'http',
123
        ];
124
125
        $objectUrl = $this->client->getObjectUrl(
126
            $this->getBucket(), $path, null, $options
127
        );
128
129
        $url = parse_url($objectUrl);
130
131
        return sprintf(
132
            '%s://%s%s',
133
            $url['scheme'], $url['host'], rawurldecode($url['path'])
134
        );
135
    }
136
137 5
    /**
138
     * @param string             $path
139 1
     * @param \DateTimeInterface $expiration
140 1
     * @param array              $options
141
     *
142 1
     * @return string
143 5
     */
144 1
    public function getTemporaryUrl($path, DateTimeInterface $expiration, array $options = [])
145
    {
146 5
        $options = array_merge(
147
            $options,
148 1
            ['Scheme' => isset($this->config['scheme']) ? $this->config['scheme'] : 'http']
149 1
        );
150
151
        $objectUrl = $this->client->getObjectUrl(
152
            $this->getBucket(), $path, $expiration->format('c'), $options
153
        );
154
155
        $url = parse_url($objectUrl);
156
157
        return sprintf(
158
            '%s://%s%s?%s',
159
            $url['scheme'], $url['host'], rawurldecode($url['path']), $url['query']
160
        );
161
    }
162 2
163
    /**
164 2
     * @param string $path
165
     * @param string $contents
166 2
     * @param Config $config
167
     *
168
     * @return array|false
169
     */
170
    public function write($path, $contents, Config $config)
171
    {
172
        $options = $this->prepareUploadConfig($config);
173
174
        return $this->client->upload($this->getBucket(), $path, $contents, $options);
175
    }
176 2
177
    /**
178 2
     * @param string   $path
179
     * @param resource $resource
180 2
     * @param Config   $config
181 2
     *
182 2
     * @return array|false
183 2
     */
184
    public function writeStream($path, $resource, Config $config)
185 2
    {
186
        $options = $this->prepareUploadConfig($config);
187
188
        return $this->client->upload(
189
            $this->getBucket(),
190
            $path,
191
            stream_get_contents($resource, -1, 0),
192
            $options
193
        );
194
    }
195 1
196
    /**
197 1
     * @param string $path
198
     * @param string $contents
199
     * @param Config $config
200
     *
201
     * @return array|false
202
     */
203
    public function update($path, $contents, Config $config)
204
    {
205
        return $this->write($path, $contents, $config);
206
    }
207 3
208
    /**
209 3
     * @param string   $path
210
     * @param resource $resource
211
     * @param Config   $config
212
     *
213
     * @return array|false
214
     */
215
    public function updateStream($path, $resource, Config $config)
216
    {
217
        return $this->writeStream($path, $resource, $config);
218 1
    }
219
220 1
    /**
221
     * @param string $path
222 1
     * @param string $newpath
223
     *
224 1
     * @return bool
225
     */
226
    public function rename($path, $newpath)
227
    {
228
        $result = $this->copy($path, $newpath);
229
230
        $this->delete($path);
231
232
        return $result;
233 2
    }
234
235 2
    /**
236
     * @param string $path
237 2
     * @param string $newpath
238
     *
239
     * @return bool
240
     */
241
    public function copy($path, $newpath)
242
    {
243
        $source = $this->getSourcePath($path);
244
245 2
        return (bool) $this->client->copy($this->getBucket(), $newpath, $source);
246
    }
247 2
248 2
    /**
249 2
     * @param string $path
250 2
     *
251
     * @return bool
252
     */
253
    public function delete($path)
254
    {
255
        return (bool) $this->client->deleteObject([
256
            'Bucket' => $this->getBucket(),
257
            'Key'    => $path,
258 1
        ]);
259
    }
260 1
261
    /**
262 1
     * @param string $dirname
263
     *
264
     * @return bool
265
     */
266 1
    public function deleteDir($dirname)
267 1
    {
268 1
        $response = $this->listObjects($dirname);
269
270 1
        if (!isset($response['Contents'])) {
271 1
            return true;
272 1
        }
273 1
274
        $keys = array_map(function ($item) {
275
            return ['Key' => $item['Key']];
276
        }, (array) $response['Contents']);
277
278
        // ignore directory
279
        $keys = array_filter($keys, function ($item) {
280
            return substr($item['Key'], -1) !== '/';
281
        });
282 1
283
        if (empty($keys)) {
284 1
            return true;
285 1
        }
286 1
287 1
        return (bool) $this->client->deleteObjects([
288 1
            'Bucket' => $this->getBucket(),
289
            'Key' => $keys,
290
        ]);
291
    }
292
293
    /**
294
     * @param string $dirname
295
     * @param Config $config
296
     *
297 1
     * @return array|false
298
     */
299 1
    public function createDir($dirname, Config $config)
300 1
    {
301 1
        return $this->client->putObject([
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->client->pu...e . '/', 'Body' => '')) returns the type Guzzle\Http\Message\Response which is incompatible with the documented return type array|false.
Loading history...
302 1
            'Bucket' => $this->getBucket(),
303 1
            'Key'    => $dirname.'/',
304
            'Body'   => '',
305
        ]);
306
    }
307
308
    /**
309
     * @param string $path
310
     * @param string $visibility
311 1
     *
312
     * @return bool
313
     */
314 1
    public function setVisibility($path, $visibility)
315
    {
316
        return (bool) $this->client->PutObjectAcl([
0 ignored issues
show
Bug Best Practice introduced by
The expression return (bool)$this->clie...sibility($visibility))) returns the type boolean which is incompatible with the return type mandated by League\Flysystem\AdapterInterface::setVisibility() of array|false.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
317
            'Bucket' => $this->getBucket(),
318
            'Key'    => $path,
319
            'ACL'    => $this->normalizeVisibility($visibility),
320
        ]);
321
    }
322
323
    /**
324
     * @param string $path
325 1
     *
326
     * @return bool
327
     */
328 1
    public function has($path)
329
    {
330
        try {
331
            return (bool) $this->getMetadata($path);
332
        } catch (NoSuchKeyException $e) {
333
            return false;
334 1
        }
335 1
    }
336 1
337 1
    /**
338
     * @param string $path
339
     *
340 1
     * @return array|bool
341
     */
342
    public function read($path)
343
    {
344
        try {
345
            if (isset($this->config['read_from_cdn']) && $this->config['read_from_cdn']) {
346
                $response = $this->getHttpClient()
347
                                 ->get($this->applyPathPrefix($path))
348
                                 ->getBody()
349
                                 ->getContents();
350
            } else {
351 1
                $response = $this->client->getObject([
352
                    'Bucket' => $this->getBucket(),
353 1
                    'Key'    => $path,
354 1
                ])->get('Body');
0 ignored issues
show
Bug introduced by
The method get() does not exist on Guzzle\Http\Message\Response. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

354
                ])->/** @scrutinizer ignore-call */ get('Body');

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
355 1
            }
356 1
357 1
            return ['contents' => (string) $response];
358
        } catch (NoSuchKeyException $e) {
359
            return false;
360
        } catch (\GuzzleHttp\Exception\ClientException $e) {
361
            return false;
362
        }
363
    }
364
365 1
    /**
366
     * @return \GuzzleHttp\Client
367
     */
368 1
    public function getHttpClient()
369
    {
370 1
        return new \GuzzleHttp\Client([
371 1
            'timeout'         => $this->config['timeout'],
372 1
            'connect_timeout' => $this->config['connect_timeout'],
373 1
        ]);
374
    }
375 1
376
    /**
377
     * @param string $path
378
     *
379
     * @return array|bool
380
     */
381
    public function readStream($path)
382
    {
383
        try {
384
            $temporaryUrl = $this->getTemporaryUrl($path, Carbon::now()->addMinutes(5));
385
386
            $stream = $this->getHttpClient()
387
                           ->get($temporaryUrl, ['stream' => true])
388
                           ->getBody()
389 1
                           ->detach();
390
391 1
            return ['stream' => $stream];
392
        } catch (NoSuchKeyException $e) {
393 1
            return false;
394
        } catch (\GuzzleHttp\Exception\ClientException $e) {
395 1
            return false;
396 1
        }
397 1
    }
398
399 1
    /**
400
     * @param string $directory
401
     * @param bool   $recursive
402
     *
403
     * @return array|bool
404
     */
405
    public function listContents($directory = '', $recursive = false)
406
    {
407 5
        $list = [];
408
409 5
        $response = $this->listObjects($directory, $recursive);
410 5
411 5
        foreach ((array) $response->get('Contents') as $content) {
412 5
            $list[] = $this->normalizeFileInfo($content);
413
        }
414
415
        return $list;
416
    }
417
418
    /**
419
     * @param string $path
420 1
     *
421
     * @return array|bool
422 1
     */
423
    public function getMetadata($path)
424 1
    {
425 1
        return $this->client->headObject([
426
            'Bucket' => $this->getBucket(),
427
            'Key'    => $path,
428
        ])->toArray();
0 ignored issues
show
Bug introduced by
The method toArray() does not exist on Guzzle\Http\Message\Response. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

428
        ])->/** @scrutinizer ignore-call */ toArray();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
429
    }
430
431
    /**
432
     * @param string $path
433 1
     *
434
     * @return array|bool
435 1
     */
436
    public function getSize($path)
437 1
    {
438 1
        $meta = $this->getMetadata($path);
439
440
        return isset($meta['ContentLength'])
441
            ? ['size' => $meta['ContentLength']] : false;
442
    }
443
444
    /**
445
     * @param string $path
446 1
     *
447
     * @return array|bool
448 1
     */
449
    public function getMimetype($path)
450 1
    {
451 1
        $meta = $this->getMetadata($path);
452
453
        return isset($meta['ContentType'])
454
            ? ['mimetype' => $meta['ContentType']] : false;
455
    }
456
457
    /**
458
     * @param string $path
459 1
     *
460
     * @return array|bool
461 1
     */
462 1
    public function getTimestamp($path)
463 1
    {
464 1
        $meta = $this->getMetadata($path);
465
466 1
        return isset($meta['LastModified'])
467 1
            ? ['timestamp' => strtotime($meta['LastModified'])] : false;
468 1
    }
469 1
470 1
    /**
471
     * @param string $path
472
     *
473 1
     * @return array|bool
474
     */
475 1
    public function getVisibility($path)
476
    {
477
        $meta = $this->client->getObjectAcl([
478
            'Bucket' => $this->getBucket(),
479
            'Key'    => $path,
480
        ]);
481
482
        foreach ($meta->get('Grants') as $grant) {
483 1
            if (isset($grant['Grantee']['URI'])
484
                && $grant['Permission'] === 'READ'
485 1
                && strpos($grant['Grantee']['URI'], 'global/AllUsers') !== false
486
            ) {
487
                return ['visibility' => AdapterInterface::VISIBILITY_PUBLIC];
488 1
            }
489 1
        }
490 1
491 1
        return ['visibility' => AdapterInterface::VISIBILITY_PRIVATE];
492 1
    }
493 1
494 1
    /**
495 1
     * @param array $content
496 1
     *
497
     * @return array
498
     */
499
    private function normalizeFileInfo(array $content)
500
    {
501
        $path = pathinfo($content['Key']);
502
503
        return [
504
            'type'      => substr($content['Key'], -1) === '/' ? 'dir' : 'file',
505 2
            'path'      => $content['Key'],
506
            'timestamp' => Carbon::parse($content['LastModified'])->getTimestamp(),
507 2
            'size'      => (int) $content['Size'],
508 2
            'dirname'   => (string) $path['dirname'],
509 2
            'basename'  => (string) $path['basename'],
510 2
            'extension' => isset($path['extension']) ? $path['extension'] : '',
511 2
            'filename'  => (string) $path['filename'],
512
        ];
513
    }
514
515
    /**
516
     * @param string $directory
517
     * @param bool   $recursive
518
     *
519 4
     * @return mixed
520
     */
521 4
    private function listObjects($directory = '', $recursive = false)
522
    {
523 4
        return $this->client->listObjects([
524
            'Bucket'    => $this->getBucket(),
525
            'Prefix'    => ((string) $directory === '') ? '' : ($directory.'/'),
526
            'Delimiter' => $recursive ? '' : '/',
527 4
        ]);
528
    }
529
530
    /**
531 4
     * @param Config $config
532
     *
533
     * @return array
534
     */
535
    private function prepareUploadConfig(Config $config)
536
    {
537
        $options = [];
538
539 1
        if ($config->has('params')) {
540
            $options['params'] = $config->get('params');
541
        }
542 1
543
        if ($config->has('visibility')) {
544
            $options['params']['ACL'] = $this->normalizeVisibility($config->get('visibility'));
545
        }
546
547 1
        return $options;
548
    }
549
550
    /**
551
     * @param $visibility
552
     *
553
     * @return string
554
     */
555
    private function normalizeVisibility($visibility)
556
    {
557
        switch ($visibility) {
558
            case AdapterInterface::VISIBILITY_PUBLIC:
559
                $visibility = 'public-read';
560
                break;
561
        }
562
563
        return $visibility;
564
    }
565
}
566