Passed
Push — master ( 08ce17...8d8d30 )
by Thomas
02:05
created

BaiduBosAdapter   B

Complexity

Total Complexity 43

Size/Duplication

Total Lines 414
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 108
c 3
b 0
f 0
dl 0
loc 414
rs 8.96
wmc 43

23 Methods

Rating   Name   Duplication   Size   Complexity  
A getVisibility() 0 16 3
A getMetadata() 0 9 2
A read() 0 9 2
A rename() 0 10 2
A setVisibility() 0 9 2
A listContents() 0 26 5
A readStream() 0 12 2
A update() 0 3 1
A getBucketAcl() 0 9 2
A updateStream() 0 3 1
A __construct() 0 3 1
A delete() 0 9 2
A getMimeType() 0 3 1
A writeStream() 0 3 1
A write() 0 10 2
A has() 0 9 2
A getObjectAcl() 0 12 3
A getSize() 0 3 1
A deleteDir() 0 9 2
A createDir() 0 13 2
A getClient() 0 3 1
A getTimestamp() 0 3 1
A copy() 0 9 2

How to fix   Complexity   

Complex Class

Complex classes like BaiduBosAdapter 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 BaiduBosAdapter, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace Sulao\Flysystem\BaiduBos;
4
5
use Exception;
6
use League\Flysystem\Adapter\AbstractAdapter;
7
use League\Flysystem\Config;
8
use League\Flysystem\Util;
9
use Sulao\BaiduBos\Client;
10
11
class BaiduBosAdapter extends AbstractAdapter
12
{
13
    use UtilTrait;
14
15
    /**
16
     * @var Client
17
     */
18
    protected $client;
19
20
    /**
21
     * BaiduBosAdapter constructor.
22
     *
23
     * @param Client $client
24
     */
25
    public function __construct(Client $client)
26
    {
27
        $this->client = $client;
28
    }
29
30
    /**
31
     * @return Client
32
     */
33
    public function getClient()
34
    {
35
        return $this->client;
36
    }
37
38
    /**
39
     * Write a new file.
40
     *
41
     * @param string $path
42
     * @param string $contents
43
     * @param Config $config   Config object
44
     *
45
     * @return array|false false on failure file meta data on success
46
     */
47
    public function write($path, $contents, Config $config)
48
    {
49
        try {
50
            $this->client
51
                ->putObject($path, $contents, $this->extractOptions($config));
52
        } catch (Exception $exception) {
53
            return  false;
54
        }
55
56
        return $this->client->getObjectMeta($path);
57
    }
58
59
    /**
60
     * Write a new file using a stream.
61
     *
62
     * @param string   $path
63
     * @param resource $resource
64
     * @param Config   $config   Config object
65
     *
66
     * @return array|false false on failure file meta data on success
67
     */
68
    public function writeStream($path, $resource, Config $config)
69
    {
70
        return $this->write($path, stream_get_contents($resource), $config);
71
    }
72
73
    /**
74
     * Update a file.
75
     *
76
     * @param string $path
77
     * @param string $contents
78
     * @param Config $config   Config object
79
     *
80
     * @return array|false false on failure file meta data on success
81
     */
82
    public function update($path, $contents, Config $config)
83
    {
84
        return $this->write($path, $contents, $config);
85
    }
86
87
    /**
88
     * Update a file using a stream.
89
     *
90
     * @param string   $path
91
     * @param resource $resource
92
     * @param Config   $config   Config object
93
     *
94
     * @return array|false false on failure file meta data on success
95
     */
96
    public function updateStream($path, $resource, Config $config)
97
    {
98
        return $this->writeStream($path, $resource, $config);
99
    }
100
101
    /**
102
     * Rename a file.
103
     *
104
     * @param string $path
105
     * @param string $newPath
106
     *
107
     * @return bool
108
     */
109
    public function rename($path, $newPath)
110
    {
111
        try {
112
            $this->client->copyObject($path, $newPath);
113
            $this->client->deleteObject($path);
114
        } catch (Exception $exception) {
115
            return false;
116
        }
117
118
        return true;
119
    }
120
121
    /**
122
     * Copy a file.
123
     *
124
     * @param string $path
125
     * @param string $newPath
126
     *
127
     * @return bool
128
     */
129
    public function copy($path, $newPath)
130
    {
131
        try {
132
            $this->client->copyObject($path, $newPath);
133
        } catch (Exception $exception) {
134
            return false;
135
        }
136
137
        return true;
138
    }
139
140
    /**
141
     * Delete a file.
142
     *
143
     * @param string $path
144
     *
145
     * @return bool
146
     */
147
    public function delete($path)
148
    {
149
        try {
150
            $this->client->deleteObject($path);
151
        } catch (Exception $exception) {
152
            return false;
153
        }
154
155
        return true;
156
    }
157
158
    /**
159
     * Delete a directory.
160
     *
161
     * @param string $dirname
162
     *
163
     * @return bool
164
     */
165
    public function deleteDir($dirname)
166
    {
167
        try {
168
            $this->client->deleteObject(rtrim($dirname, '/') . '/');
169
        } catch (Exception $exception) {
170
            return false;
171
        }
172
173
        return true;
174
    }
175
176
    /**
177
     * Create a directory.
178
     *
179
     * @param string $dirname directory name
180
     * @param Config $config
181
     *
182
     * @return bool
183
     */
184
    public function createDir($dirname, Config $config)
185
    {
186
        try {
187
            $this->client->putObject(
188
                rtrim($dirname, '/') . '/',
189
                '',
190
                $this->extractOptions($config)
191
            );
192
        } catch (Exception $exception) {
193
            return false;
194
        }
195
196
        return true;
0 ignored issues
show
Bug Best Practice introduced by
The expression return true returns the type true which is incompatible with the return type mandated by League\Flysystem\AdapterInterface::createDir() 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...
197
    }
198
199
    /**
200
     * Check whether a file exists.
201
     *
202
     * @param string $path
203
     *
204
     * @return bool
205
     */
206
    public function has($path)
207
    {
208
        try {
209
            $this->client->getObjectMeta($path);
210
        } catch (Exception $exception) {
211
            return false;
212
        }
213
214
        return true;
215
    }
216
217
    /**
218
     * Read a file.
219
     *
220
     * @param string $path
221
     *
222
     * @return array|false
223
     */
224
    public function read($path)
225
    {
226
        try {
227
            $contents = $this->client->getObject($path);
228
        } catch (Exception $exception) {
229
            return false;
230
        }
231
232
        return compact('path', 'contents');
233
    }
234
235
    /**
236
     * Read a file as a stream.
237
     *
238
     * @param string $path
239
     *
240
     * @return array|false
241
     */
242
    public function readStream($path)
243
    {
244
        $result = $this->read($path);
245
        if ($result === false) {
246
            return false;
247
        }
248
249
        $stream = fopen('php://temp', 'w+b');
250
        fputs($stream, $result['contents']);
251
        rewind($stream);
252
253
        return compact('path', 'stream');
254
    }
255
256
    /**
257
     * List contents of a directory.
258
     *
259
     * @param string $directory
260
     * @param bool   $recursive
261
     *
262
     * @return array
263
     */
264
    public function listContents($directory = '', $recursive = false)
265
    {
266
        $options = [];
267
268
        if (!$recursive) {
269
            $options['query']['delimiter'] = '/';
270
        }
271
272
        $directory = trim($directory, '/');
273
        if ($directory !== '') {
274
            $directory .= '/';
275
            $options['query']['prefix'] = $directory;
276
        }
277
278
        $result = $this->client->listObjects($options);
279
280
        $contents = [];
281
        foreach ($result['contents'] as $row) {
282
            if ($row['key'] === $directory) {
283
                continue;
284
            }
285
286
            $contents[] = $this->normalizeContent($row);
287
        }
288
289
        return $contents;
290
    }
291
292
    /**
293
     * Get all the meta data of a file or directory.
294
     *
295
     * @param string $path
296
     *
297
     * @return array|false
298
     */
299
    public function getMetadata($path)
300
    {
301
        try {
302
            $meta = $this->client->getObjectMeta($path);
303
        } catch (Exception $exception) {
304
            return false;
305
        }
306
307
        return $this->normalizeMeta($meta, $path);
308
    }
309
310
    /**
311
     * Get the size of a file.
312
     *
313
     * @param string $path
314
     *
315
     * @return array|false
316
     */
317
    public function getSize($path)
318
    {
319
        return $this->getMetadata($path);
320
    }
321
322
    /**
323
     * Get the mime-type of a file.
324
     *
325
     * @param string $path
326
     *
327
     * @return array|false
328
     */
329
    public function getMimeType($path)
330
    {
331
        return $this->getMetadata($path);
332
    }
333
334
    /**
335
     * Get the timestamp of a file.
336
     *
337
     * @param string $path
338
     *
339
     * @return array|false
340
     */
341
    public function getTimestamp($path)
342
    {
343
        return $this->getMetadata($path);
344
    }
345
346
    /**
347
     * Get the visibility of a file.
348
     *
349
     * @param string $path
350
     *
351
     * @return array|false
352
     */
353
    public function getVisibility($path)
354
    {
355
        $acl = $this->getObjectAcl($path);
356
        if ($acl === false) {
357
            return false;
358
        }
359
360
        $permissions = $this->extractPermissions($acl);
361
362
        if (in_array('READ', $permissions)) {
363
            $visibility = 'public-read';
364
        } else {
365
            $visibility = 'private';
366
        }
367
368
        return compact('path', 'visibility');
369
    }
370
371
    /**
372
     * Set the visibility for a file.
373
     *
374
     * @param string $path
375
     * @param string $visibility
376
     *
377
     * @return array|false file meta data
378
     */
379
    public function setVisibility($path, $visibility)
380
    {
381
        try {
382
            $this->client->putObjectAcl($path, $visibility);
383
        } catch (Exception $exception) {
384
            return false;
385
        }
386
387
        return compact('path', 'visibility');
388
    }
389
390
    /**
391
     * Get object acl, if not set, return bucket acl
392
     *
393
     * @param $path
394
     *
395
     * @return array|false
396
     */
397
    protected function getObjectAcl($path)
398
    {
399
        try {
400
            $result = $this->client->getObjectAcl($path);
401
            return $result['accessControlList'];
402
        } catch (Exception $exception) {
403
            if ($exception->getCode() == 404) {
404
                return $this->getBucketAcl();
405
            }
406
        }
407
408
        return false;
409
    }
410
411
    /**
412
     * Get bucket acl
413
     *
414
     * @return array|false
415
     */
416
    protected function getBucketAcl()
417
    {
418
        try {
419
            $result = $this->client->getBucketAcl();
420
        } catch (Exception $exception) {
421
            return false;
422
        }
423
424
        return $result['accessControlList'];
425
    }
426
}
427