Passed
Push — master ( 10e14e...fd7bbc )
by Carlos
02:56
created

QiniuAdapter::normalizeHost()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 4
nc 2
nop 1
crap 3
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
class QiniuAdapter extends AbstractAdapter
20
{
21
    use NotSupportingVisibilityTrait;
22
23
    protected $accessKey;
24
    protected $secretKey;
25
    protected $bucket;
26
    protected $domain;
27
28
    protected $authManager;
29
    protected $uploadManager;
30
    protected $bucketManager;
31
32
    /**
33
     * QiniuAdapter constructor.
34
     *
35
     * @param string $accessKey
36
     * @param string $secretKey
37
     * @param string $bucket
38
     * @param string $domain
39
     */
40 1
    public function __construct($accessKey, $secretKey, $bucket, $domain)
41
    {
42 1
        $this->accessKey = $accessKey;
43 1
        $this->secretKey = $secretKey;
44 1
        $this->bucket = $bucket;
45 1
        $this->domain = $domain;
46 1
    }
47
48
    /**
49
     * Write a new file.
50
     *
51
     * @param string $path
52
     * @param string $contents
53
     * @param Config $config   Config object
54
     *
55
     * @return array|false false on failure file meta data on success
56
     */
57 1
    public function write($path, $contents, Config $config)
58
    {
59 1
        list($response, $error) = $this->getUploadManager()->put(
60 1
                                                                $this->getAuthManager()->uploadToken($this->bucket),
61 1
                                                                $path,
62
                                                                $contents
63 1
                                                            );
64
65 1
        if ($error) {
66 1
            return false;
67
        }
68
69 1
        return $response;
70
    }
71
72
    /**
73
     * Write a new file using a stream.
74
     *
75
     * @param string   $path
76
     * @param resource $resource
77
     * @param Config   $config   Config object
78
     *
79
     * @return array|false false on failure file meta data on success
80
     */
81 1
    public function writeStream($path, $resource, Config $config)
82
    {
83 1
        $contents = '';
84
85 1
        while (!feof($resource)) {
86 1
            $contents .= fread($resource, 1024);
87 1
        }
88
89 1
        $response = $this->write($path, $contents, $config);
90
91 1
        if ($response === false) {
92 1
            return $response;
93
        }
94
95 1
        return compact('path');
96
    }
97
98
    /**
99
     * Update a file.
100
     *
101
     * @param string $path
102
     * @param string $contents
103
     * @param Config $config   Config object
104
     *
105
     * @return array|false false on failure file meta data on success
106
     */
107 1
    public function update($path, $contents, Config $config)
108
    {
109 1
        $this->delete($path);
110
111 1
        return $this->write($path, $contents, $config);
112
    }
113
114
    /**
115
     * Update a file using a stream.
116
     *
117
     * @param string   $path
118
     * @param resource $resource
119
     * @param Config   $config   Config object
120
     *
121
     * @return array|false false on failure file meta data on success
122
     */
123 1
    public function updateStream($path, $resource, Config $config)
124
    {
125 1
        $this->delete($path);
126
127 1
        return $this->writeStream($path, $resource, $config);
128
    }
129
130
    /**
131
     * Rename a file.
132
     *
133
     * @param string $path
134
     * @param string $newpath
135
     *
136
     * @return bool
137
     */
138 1
    public function rename($path, $newpath)
139
    {
140 1
        $response = $this->getBucketManager()->rename($this->bucket, $path, $newpath);
141
142 1
        return is_null($response);
143
    }
144
145
    /**
146
     * Copy a file.
147
     *
148
     * @param string $path
149
     * @param string $newpath
150
     *
151
     * @return bool
152
     */
153 1
    public function copy($path, $newpath)
154
    {
155 1
        $response = $this->getBucketManager()->copy($this->bucket, $path, $this->bucket, $newpath);
156
157 1
        return is_null($response);
158
    }
159
160
    /**
161
     * Delete a file.
162
     *
163
     * @param string $path
164
     *
165
     * @return bool
166
     */
167 1
    public function delete($path)
168
    {
169 1
        $response = $this->getBucketManager()->delete($this->bucket, $path);
170
171 1
        return is_null($response);
172
    }
173
174
    /**
175
     * Delete a directory.
176
     *
177
     * @param string $dirname
178
     *
179
     * @return bool
180
     */
181 1
    public function deleteDir($dirname)
182
    {
183 1
        return true;
184
    }
185
186
    /**
187
     * Create a directory.
188
     *
189
     * @param string $dirname directory name
190
     * @param Config $config
191
     *
192
     * @return array|false
193
     */
194 1
    public function createDir($dirname, Config $config)
195
    {
196 1
        return ['path' => $dirname, 'type' => 'dir'];
197
    }
198
199
    /**
200
     * Check whether a file exists.
201
     *
202
     * @param string $path
203
     *
204
     * @return array|bool|null
205
     */
206 1
    public function has($path)
207
    {
208 1
        list($response, $error) = $this->getBucketManager()->stat($this->bucket, $path);
209
210 1
        return !$error || is_array($response);
211
    }
212
213
    /**
214
     * Read a file.
215
     *
216
     * @param string $path
217
     *
218
     * @return array|false
219
     */
220 1
    public function read($path)
221
    {
222 1
        $contents = file_get_contents($this->normalizeHost($this->domain).$path);
223
224 1
        return compact('contents', 'path');
225
    }
226
227
    /**
228
     * Read a file as a stream.
229
     *
230
     * @param string $path
231
     *
232
     * @return array|false
233
     */
234 1
    public function readStream($path)
235
    {
236 1
        if (ini_get('allow_url_fopen')) {
237 1
            $stream = fopen($this->normalizeHost($this->domain).$path, 'r');
238
239 1
            return compact('stream', 'path');
240
        }
241
242 1
        return false;
243
    }
244
245
    /**
246
     * List contents of a directory.
247
     *
248
     * @param string $directory
249
     * @param bool   $recursive
250
     *
251
     * @return array
252
     */
253 1
    public function listContents($directory = '', $recursive = false)
254
    {
255 1
        $list = [];
256
257 1
        $result = $this->getBucketManager()->listFiles($this->bucket, $directory);
258
259 1
        foreach ($result[0] as $files) {
260 1
            $list[] = $this->normalizeFileInfo($files);
261 1
        }
262
263 1
        return $list;
264
    }
265
266
    /**
267
     * Get all the meta data of a file or directory.
268
     *
269
     * @param string $path
270
     *
271
     * @return array|false
272
     */
273 1
    public function getMetadata($path)
274
    {
275 1
        $result = $this->getBucketManager()->stat($this->bucket, $path);
276 1
        $result[0]['key'] = $path;
277
278 1
        return $this->normalizeFileInfo($result[0]);
0 ignored issues
show
Bug introduced by
It seems like $result[0] can also be of type null; however, Overtrue\Flysystem\Qiniu...er::normalizeFileInfo() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
279
    }
280
281
    /**
282
     * Get the size of a file.
283
     *
284
     * @param string $path
285
     *
286
     * @return array|false
287
     */
288 1
    public function getSize($path)
289
    {
290 1
        return $this->getMetadata($path);
291
    }
292
293
    /**
294
     * Get the mimetype of a file.
295
     *
296
     * @param string $path
297
     *
298
     * @return array|false
299
     */
300 1
    public function getMimetype($path)
301
    {
302 1
        $response = $this->getBucketManager()->stat($this->bucket, $path);
303
304 1
        if (empty($response[0]['mimeType'])) {
305 1
            return false;
306
        }
307
308 1
        return ['mimetype' => $response[0]['mimeType']];
309
    }
310
311
    /**
312
     * Get the timestamp of a file.
313
     *
314
     * @param string $path
315
     *
316
     * @return array|false
317
     */
318 1
    public function getTimestamp($path)
319
    {
320 1
        return $this->getMetadata($path);
321
    }
322
323
    /**
324
     * @param \Qiniu\Storage\BucketManager $manager
325
     *
326
     * @return $this
327
     */
328 1
    public function setBucketManager(BucketManager $manager)
329
    {
330 1
        $this->bucketManager = $manager;
331
332 1
        return $this;
333
    }
334
335
    /**
336
     * @param \Qiniu\Storage\UploadManager $manager
337
     *
338
     * @return $this
339
     */
340 1
    public function setUploadManager(UploadManager $manager)
341
    {
342 1
        $this->uploadManager = $manager;
343
344 1
        return $this;
345
    }
346
347
    /**
348
     * @param \Qiniu\Auth $manager
349
     *
350
     * @return $this
351
     */
352 1
    public function setAuthManager(Auth $manager)
353
    {
354 1
        $this->authManager = $manager;
355
356 1
        return $this;
357
    }
358
359
    /**
360
     * @return \Qiniu\Storage\BucketManager
361
     */
362 1
    public function getBucketManager()
363
    {
364 1
        return $this->bucketManager ?: $this->bucketManager = new BucketManager($this->getAuthManager());
365
    }
366
367
    /**
368
     * @return \Qiniu\Auth
369
     */
370 1
    public function getAuthManager()
371
    {
372 1
        return $this->authManager ?: $this->authManager = new Auth($this->accessKey, $this->secretKey);
373
    }
374
375
    /**
376
     * @return \Qiniu\Storage\UploadManager
377
     */
378 1
    public function getUploadManager()
379
    {
380 1
        return $this->uploadManager ?: $this->uploadManager = new UploadManager();
381
    }
382
383
    /**
384
     * @param array $stats
385
     *
386
     * @return array
387
     */
388 2
    protected function normalizeFileInfo(array $stats)
389
    {
390
        return [
391 2
            'type' => 'file',
392 2
            'path' => $stats['key'],
393 2
            'timestamp' => floor($stats['putTime'] / 10000000),
394 2
            'size' => $stats['fsize'],
395 2
        ];
396
    }
397
398
    /**
399
     * @param  string $domain
400
     *
401
     * @return string
402
     */
403 2
    protected function normalizeHost($domain)
404
    {
405 2
        if (0 !== stripos($domain, 'https://') && 0 !== stripos($domain, 'http://')) {
406 2
            $domain = "http://{$domain}";
407 2
        }
408
409 2
        return rtrim($domain, '/').'/';
410
    }
411
}
412