Passed
Push — master ( d6d755...da854f )
by Carlos
01:46
created

QiniuAdapter::has()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

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