Test Failed
Pull Request — master (#8)
by
unknown
03:34
created

QiniuAdapter::fetch()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 9
rs 9.6666
ccs 1
cts 1
cp 1
cc 2
eloc 5
nc 2
nop 2
crap 2
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
        $mime = 'application/octet-stream';
60 1
        if($config->has('mime')) {
61 1
            $mime = $config->get('mime');
62
        }
63 1
64
        list($response, $error) = $this->getUploadManager()->put(
65 1
            $this->getAuthManager()->uploadToken($this->bucket),
66 1
            $path,
67
            $contents,
68
            NULL,
69 1
            $mime
70
        );
71
72
        if ($error) {
73
            return false;
74
        }
75
76
        return $response;
77
    }
78
79
    /**
80
     * Write a new file using a stream.
81 1
     *
82
     * @param string   $path
83 1
     * @param resource $resource
84
     * @param Config   $config   Config object
85 1
     *
86 1
     * @return array|false false on failure file meta data on success
87 1
     */
88
    public function writeStream($path, $resource, Config $config)
89 1
    {
90
        $contents = '';
91 1
92 1
        while (!feof($resource)) {
93
            $contents .= fread($resource, 1024);
94
        }
95 1
96
        $response = $this->write($path, $contents, $config);
97
98
        if ($response === false) {
99
            return $response;
100
        }
101
102
        return compact('path');
103
    }
104
105
    /**
106
     * Update a file.
107 1
     *
108
     * @param string $path
109 1
     * @param string $contents
110
     * @param Config $config   Config object
111 1
     *
112
     * @return array|false false on failure file meta data on success
113
     */
114
    public function update($path, $contents, Config $config)
115
    {
116
        $this->delete($path);
117
118
        return $this->write($path, $contents, $config);
119
    }
120
121
    /**
122
     * Update a file using a stream.
123 1
     *
124
     * @param string   $path
125 1
     * @param resource $resource
126
     * @param Config   $config   Config object
127 1
     *
128
     * @return array|false false on failure file meta data on success
129
     */
130
    public function updateStream($path, $resource, Config $config)
131
    {
132
        $this->delete($path);
133
134
        return $this->writeStream($path, $resource, $config);
135
    }
136
137
    /**
138 1
     * Rename a file.
139
     *
140 1
     * @param string $path
141
     * @param string $newpath
142 1
     *
143
     * @return bool
144
     */
145
    public function rename($path, $newpath)
146
    {
147
        $response = $this->getBucketManager()->rename($this->bucket, $path, $newpath);
148
149
        return is_null($response);
150
    }
151
152
    /**
153 1
     * Copy a file.
154
     *
155 1
     * @param string $path
156
     * @param string $newpath
157 1
     *
158
     * @return bool
159
     */
160
    public function copy($path, $newpath)
161
    {
162
        $response = $this->getBucketManager()->copy($this->bucket, $path, $this->bucket, $newpath);
163
164
        return is_null($response);
165
    }
166
167 1
    /**
168
     * Delete a file.
169 1
     *
170
     * @param string $path
171 1
     *
172
     * @return bool
173
     */
174
    public function delete($path)
175
    {
176
        $response = $this->getBucketManager()->delete($this->bucket, $path);
177
178
        return is_null($response);
179
    }
180
181 1
    /**
182
     * Delete a directory.
183 1
     *
184
     * @param string $dirname
185
     *
186
     * @return bool
187
     */
188
    public function deleteDir($dirname)
189
    {
190
        return true;
191
    }
192
193
    /**
194 1
     * Create a directory.
195
     *
196 1
     * @param string $dirname directory name
197
     * @param Config $config
198
     *
199
     * @return array|false
200
     */
201
    public function createDir($dirname, Config $config)
202
    {
203
        return ['path' => $dirname, 'type' => 'dir'];
204
    }
205
206 1
    /**
207
     * Check whether a file exists.
208 1
     *
209
     * @param string $path
210 1
     *
211
     * @return array|bool|null
212
     */
213
    public function has($path)
214
    {
215
        list($response, $error) = $this->getBucketManager()->stat($this->bucket, $path);
216
217
        return !$error || is_array($response);
218
    }
219
220 1
    /**
221
     * Get resource url.
222 1
     *
223
     * @param string $path
224
     *
225
     * @return string
226
     */
227
    public function getUrl($path)
228
    {
229
        return $this->normalizeHost($this->domain).$path;
230
    }
231
232 1
    /**
233
     * Read a file.
234 1
     *
235
     * @param string $path
236 1
     *
237
     * @return array|false
238
     */
239
    public function read($path)
240
    {
241
        $contents = file_get_contents($this->getUrl($path));
242
243
        return compact('contents', 'path');
244
    }
245
246 1
    /**
247
     * Read a file as a stream.
248 1
     *
249 1
     * @param string $path
250
     *
251 1
     * @return array|false
252
     */
253
    public function readStream($path)
254 1
    {
255
        if (ini_get('allow_url_fopen')) {
256
            $stream = fopen($this->normalizeHost($this->domain).$path, 'r');
257
258
            return compact('stream', 'path');
259
        }
260
261
        return false;
262
    }
263
264
    /**
265 1
     * List contents of a directory.
266
     *
267 1
     * @param string $directory
268
     * @param bool   $recursive
269 1
     *
270
     * @return array
271 1
     */
272 1
    public function listContents($directory = '', $recursive = false)
273 1
    {
274
        $list = [];
275 1
276
        $result = $this->getBucketManager()->listFiles($this->bucket, $directory);
277
278
        foreach ($result[0] as $files) {
279
            $list[] = $this->normalizeFileInfo($files);
280
        }
281
282
        return $list;
283
    }
284
285 1
    /**
286
     * Get all the meta data of a file or directory.
287 1
     *
288 1
     * @param string $path
289
     *
290 1
     * @return array|false
291
     */
292
    public function getMetadata($path)
293
    {
294
        $result = $this->getBucketManager()->stat($this->bucket, $path);
295
        $result[0]['key'] = $path;
296
297
        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...
298
    }
299
300 1
    /**
301
     * Get the size of a file.
302 1
     *
303
     * @param string $path
304
     *
305
     * @return array|false
306
     */
307
    public function getSize($path)
308
    {
309
        return $this->getMetadata($path);
310
    }
311
312 1
    /**
313
     * Fetch url to bucket
314 1
     *
315
     * @param string $path
316 1
     * @param string $url
317 1
     *
318
     * @return array|false
319
     */
320 1
    public function fetch($path, $url)
321
    {
322
        list($response, $error) = $this->getBucketManager()->fetch($url, $this->bucket, $path);
323
        if ($error) {
324
            return false;
325
        }
326
327
        return $response;
328
    }
329
330 1
    /**
331
     * Get the mimetype of a file.
332 1
     *
333
     * @param string $path
334
     *
335
     * @return array|false
336
     */
337
    public function getMimetype($path)
338
    {
339
        $response = $this->getBucketManager()->stat($this->bucket, $path);
340 1
341
        if (empty($response[0]['mimeType'])) {
342 1
            return false;
343
        }
344 1
345
        return ['mimetype' => $response[0]['mimeType']];
346
    }
347
348
    /**
349
     * Get the timestamp of a file.
350
     *
351
     * @param string $path
352 1
     *
353
     * @return array|false
354 1
     */
355
    public function getTimestamp($path)
356 1
    {
357
        return $this->getMetadata($path);
358
    }
359
360
    /**
361
     * @param \Qiniu\Storage\BucketManager $manager
362
     *
363
     * @return $this
364 1
     */
365
    public function setBucketManager(BucketManager $manager)
366 1
    {
367
        $this->bucketManager = $manager;
368 1
369
        return $this;
370
    }
371
372
    /**
373
     * @param \Qiniu\Storage\UploadManager $manager
374 1
     *
375
     * @return $this
376 1
     */
377
    public function setUploadManager(UploadManager $manager)
378
    {
379
        $this->uploadManager = $manager;
380
381
        return $this;
382 1
    }
383
384 1
    /**
385
     * @param \Qiniu\Auth $manager
386
     *
387
     * @return $this
388
     */
389
    public function setAuthManager(Auth $manager)
390 1
    {
391
        $this->authManager = $manager;
392 1
393
        return $this;
394
    }
395
396
    /**
397
     * @return \Qiniu\Storage\BucketManager
398
     */
399
    public function getBucketManager()
400 2
    {
401
        return $this->bucketManager ?: $this->bucketManager = new BucketManager($this->getAuthManager());
402
    }
403 2
404 2
    /**
405 2
     * @return \Qiniu\Auth
406 2
     */
407 2
    public function getAuthManager()
408
    {
409
        return $this->authManager ?: $this->authManager = new Auth($this->accessKey, $this->secretKey);
410
    }
411
412
    /**
413
     * @return \Qiniu\Storage\UploadManager
414
     */
415 2
    public function getUploadManager()
416
    {
417 2
        return $this->uploadManager ?: $this->uploadManager = new UploadManager();
418 2
    }
419 2
420
    /**
421 2
     * @param array $stats
422
     *
423
     * @return array
424
     */
425
    protected function normalizeFileInfo(array $stats)
426
    {
427
        return [
428
            'type' => 'file',
429
            'path' => $stats['key'],
430
            'timestamp' => floor($stats['putTime'] / 10000000),
431
            'size' => $stats['fsize'],
432
        ];
433
    }
434
435
    /**
436
     * @param string $domain
437
     *
438
     * @return string
439
     */
440
    protected function normalizeHost($domain)
441
    {
442
        if (0 !== stripos($domain, 'https://') && 0 !== stripos($domain, 'http://')) {
443
            $domain = "http://{$domain}";
444
        }
445
446
        return rtrim($domain, '/').'/';
447
    }
448
}
449