Test Failed
Push — master ( 5cb5e4...15a3de )
by Raza
01:57
created

DropboxAdapter   A

Complexity

Total Complexity 32

Size/Duplication

Total Lines 281
Duplicated Lines 19.22 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 88.17%

Importance

Changes 0
Metric Value
wmc 32
lcom 1
cbo 5
dl 54
loc 281
ccs 82
cts 93
cp 0.8817
rs 9.84
c 0
b 0
f 0

23 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A write() 0 4 1
A writeStream() 0 4 1
A update() 0 4 1
A updateStream() 0 4 1
A rename() 0 13 2
A copy() 13 13 2
A deleteDir() 0 4 1
A delete() 0 12 2
A createDir() 14 14 2
A has() 0 4 1
A read() 0 12 2
A readStream() 0 14 2
A listContents() 0 17 2
A getMetadata() 12 12 2
A getSize() 0 4 1
A getMimetype() 0 4 1
A getTimestamp() 0 4 1
A getTemporaryLink() 0 4 1
A getThumbnail() 0 4 1
A applyPathPrefix() 0 6 1
A getClient() 0 4 1
A upload() 12 12 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Srmklive\Dropbox\Adapter;
4
5
use League\Flysystem\Adapter\AbstractAdapter;
6
use League\Flysystem\Adapter\Polyfill\NotSupportingVisibilityTrait;
7
use League\Flysystem\Config;
8
use Srmklive\Dropbox\Client\DropboxClient;
9
use Srmklive\Dropbox\MimeType;
10
use Srmklive\Dropbox\ParseResponse;
11
12
class DropboxAdapter extends AbstractAdapter
13
{
14
    use NotSupportingVisibilityTrait, ParseResponse;
15
16
    /** @var \Srmklive\Dropbox\Client\DropboxClient */
17
    protected $client;
18
19 19
    public function __construct(DropboxClient $client, $prefix = '')
20
    {
21 19
        $this->client = $client;
22
23 19
        $this->setPathPrefix($prefix);
24 19
    }
25
26
    /**
27
     * {@inheritdoc}
28
     */
29 1
    public function write($path, $contents, Config $config)
30
    {
31 1
        return $this->upload($path, $contents, 'add');
32
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37 1
    public function writeStream($path, $resource, Config $config)
38
    {
39 1
        return $this->upload($path, $resource, 'add');
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45 1
    public function update($path, $contents, Config $config)
46
    {
47 1
        return $this->upload($path, $contents, 'overwrite');
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53 1
    public function updateStream($path, $resource, Config $config)
54
    {
55 1
        return $this->upload($path, $resource, 'overwrite');
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61 2
    public function rename($path, $newPath)
62
    {
63 2
        $path = $this->applyPathPrefix($path);
64 2
        $newPath = $this->applyPathPrefix($newPath);
65
66
        try {
67 2
            $this->client->move($path, $newPath);
68
69 1
            return true;
70 1
        } catch (\Exception $e) {
71 1
            return false;
72
        }
73
    }
74
75
    /**
76
     * {@inheritdoc}
77
     */
78 2 View Code Duplication
    public function copy($path, $newpath)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
79
    {
80 2
        $path = $this->applyPathPrefix($path);
81 2
        $newpath = $this->applyPathPrefix($newpath);
82
83
        try {
84 2
            $this->client->copy($path, $newpath);
85
86 1
            return true;
87 1
        } catch (\Exception $e) {
88 1
            return false;
89
        }
90
    }
91
92
    /**
93
     * {@inheritdoc}
94
     */
95 1
    public function delete($path)
96
    {
97 1
        $location = $this->applyPathPrefix($path);
98
99
        try {
100 1
            $this->client->delete($location);
101
102 1
            return true;
103
        } catch (\Exception $e) {
104
            return false;
105
        }
106
    }
107
108
    /**
109
     * {@inheritdoc}
110
     */
111 1
    public function deleteDir($dirname)
112
    {
113 1
        return $this->delete($dirname);
114
    }
115
116
    /**
117
     * {@inheritdoc}
118
     */
119 1 View Code Duplication
    public function createDir($dirname, Config $config)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
120
    {
121 1
        $path = $this->applyPathPrefix($dirname);
122 1
        $response = false;
123
124
        try {
125 1
            $object = $this->client->createFolder($path);
126
127 1
            $response = $this->normalizeResponse($object);
128 1
        } catch (\Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
129
        }
130
131 1
        return $response;
132
    }
133
134
    /**
135
     * {@inheritdoc}
136
     */
137 2
    public function has($path)
138
    {
139 2
        return $this->getMetadata($path);
140
    }
141
142
    /**
143
     * {@inheritdoc}
144
     */
145 1
    public function read($path)
146
    {
147 1
        if (!$object = $this->readStream($path)) {
148
            return false;
149
        }
150
151 1
        $object['contents'] = stream_get_contents($object['stream']);
152 1
        fclose($object['stream']);
153 1
        unset($object['stream']);
154
155 1
        return $object;
156
    }
157
158
    /**
159
     * {@inheritdoc}
160
     */
161 2
    public function readStream($path)
162
    {
163 2
        $path = $this->applyPathPrefix($path);
164 2
        $response = false;
165
166
        try {
167 2
            $stream = $this->client->download($path);
168
169 2
            $response = compact('stream');
170 2
        } catch (\Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
171
        }
172
173 2
        return $response;
174
    }
175
176
    /**
177
     * {@inheritdoc}
178
     */
179 1
    public function listContents($directory = '', $recursive = false)
180
    {
181 1
        $response = [];
182
183
        try {
184 1
            $location = $this->applyPathPrefix($directory);
185
186 1
            $result = $this->client->listFolder($location, $recursive);
187
188 1
            $response = array_map(function ($entry) {
189 1
                return $this->normalizeResponse($entry);
190 1
            }, $result['entries']);
191 1
        } catch (\Exception $exception) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
192
        }
193
194 1
        return $response;
195
    }
196
197
    /**
198
     * {@inheritdoc}
199
     */
200 5 View Code Duplication
    public function getMetadata($path)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
201
    {
202 5
        $path = $this->applyPathPrefix($path);
203
204
        try {
205 5
            $object = $this->client->getMetadata($path);
206
207 4
            return $this->normalizeResponse($object);
208 1
        } catch (\Exception  $e) {
209 1
            return false;
210
        }
211
    }
212
213
    /**
214
     * {@inheritdoc}
215
     */
216 1
    public function getSize($path)
217
    {
218 1
        return $this->getMetadata($path);
219
    }
220
221
    /**
222
     * {@inheritdoc}
223
     */
224
    public function getMimetype($path)
225
    {
226
        return ['mimetype' => MimeType::detectByFilename($path)];
227
    }
228
229
    /**
230
     * {@inheritdoc}
231
     */
232 1
    public function getTimestamp($path)
233
    {
234 1
        return $this->getMetadata($path);
235
    }
236
237
    /**
238
     * {@inheritdoc}
239
     */
240
    public function getTemporaryLink($path)
241
    {
242
        return $this->client->getTemporaryLink($path);
243
    }
244
245
    /**
246
     * {@inheritdoc}
247
     */
248
    public function getThumbnail($path, $format = 'jpeg', $size = 'w64h64')
249
    {
250
        return $this->client->getThumbnail($path, $format, $size);
251
    }
252
253
    /**
254
     * {@inheritdoc}
255
     */
256 18
    public function applyPathPrefix($path)
257
    {
258 18
        $path = parent::applyPathPrefix($path);
259
260 18
        return '/'.trim($path, '/');
261
    }
262
263
    /**
264
     * @return DropboxClient
265
     */
266 1
    public function getClient()
267
    {
268 1
        return $this->client;
269
    }
270
271
    /**
272
     * @param string          $path
273
     * @param resource|string $contents
274
     * @param string          $mode
275
     *
276
     * @throws \Exception
277
     *
278
     * @return array|false file metadata
279
     */
280 4 View Code Duplication
    protected function upload($path, $contents, $mode)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
281
    {
282 4
        $path = $this->applyPathPrefix($path);
283
284
        try {
285 4
            $object = $this->client->upload($path, $contents, $mode);
286
287 4
            return $this->normalizeResponse($object);
288
        } catch (\Exception $e) {
289
            return false;
290
        }
291
    }
292
}
293