Passed
Push — master ( bf8804...0647e8 )
by Raza
01:40
created

DropboxAdapter   A

Complexity

Total Complexity 32

Size/Duplication

Total Lines 265
Duplicated Lines 23.02 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 87.91%

Importance

Changes 0
Metric Value
wmc 32
lcom 1
cbo 4
dl 61
loc 265
ccs 80
cts 91
cp 0.8791
rs 9.6
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 delete() 0 12 2
A deleteDir() 0 4 1
A createDir() 12 12 2
A has() 0 4 1
A read() 0 12 2
A readStream() 12 12 2
A listContents() 0 16 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 LogicException;
9
use Srmklive\Dropbox\Client\DropboxClient;
10
use Srmklive\Dropbox\Exceptions\BadRequest;
11
use Srmklive\Dropbox\ParseResponse;
12
13
class DropboxAdapter extends AbstractAdapter
14
{
15
    use NotSupportingVisibilityTrait, ParseResponse;
16
17
    /** @var \Srmklive\Dropbox\Client\DropboxClient */
18
    protected $client;
19
20 19
    public function __construct(DropboxClient $client, $prefix = '')
21
    {
22 19
        $this->client = $client;
23
24 19
        $this->setPathPrefix($prefix);
25 19
    }
26
27
    /**
28
     * {@inheritdoc}
29
     */
30 1
    public function write($path, $contents, Config $config)
31
    {
32 1
        return $this->upload($path, $contents, 'add');
33
    }
34
35
    /**
36
     * {@inheritdoc}
37
     */
38 1
    public function writeStream($path, $resource, Config $config)
39
    {
40 1
        return $this->upload($path, $resource, 'add');
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46 1
    public function update($path, $contents, Config $config)
47
    {
48 1
        return $this->upload($path, $contents, 'overwrite');
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54 1
    public function updateStream($path, $resource, Config $config)
55
    {
56 1
        return $this->upload($path, $resource, 'overwrite');
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62 2
    public function rename($path, $newPath)
63
    {
64 2
        $path = $this->applyPathPrefix($path);
65 2
        $newPath = $this->applyPathPrefix($newPath);
66
67
        try {
68 2
            $this->client->move($path, $newPath);
69 2
        } catch (BadRequest $e) {
70 1
            return false;
71
        }
72
73 1
        return true;
74
    }
75
76
    /**
77
     * {@inheritdoc}
78
     */
79 2 View Code Duplication
    public function copy($path, $newpath)
80
    {
81 2
        $path = $this->applyPathPrefix($path);
82 2
        $newpath = $this->applyPathPrefix($newpath);
83
84
        try {
85 2
            $this->client->copy($path, $newpath);
86 2
        } catch (BadRequest $e) {
87 1
            return false;
88
        }
89
90 1
        return true;
91
    }
92
93
    /**
94
     * {@inheritdoc}
95
     */
96 1
    public function delete($path)
97
    {
98 1
        $location = $this->applyPathPrefix($path);
99
100
        try {
101 1
            $this->client->delete($location);
102 1
        } catch (BadRequest $e) {
103
            return false;
104
        }
105
106 1
        return true;
107
    }
108
109
    /**
110
     * {@inheritdoc}
111
     */
112 1
    public function deleteDir($dirname)
113
    {
114 1
        return $this->delete($dirname);
115
    }
116
117
    /**
118
     * {@inheritdoc}
119
     */
120 1 View Code Duplication
    public function createDir($dirname, Config $config)
121
    {
122 1
        $path = $this->applyPathPrefix($dirname);
123
124
        try {
125 1
            $object = $this->client->createFolder($path);
126 1
        } catch (BadRequest $e) {
127 1
            return false;
128
        }
129
130 1
        return $this->normalizeResponse($object);
131
    }
132
133
    /**
134
     * {@inheritdoc}
135
     */
136 2
    public function has($path)
137
    {
138 2
        return $this->getMetadata($path);
139
    }
140
141
    /**
142
     * {@inheritdoc}
143
     */
144 1
    public function read($path)
145
    {
146 1
        if (!$object = $this->readStream($path)) {
147
            return false;
148
        }
149
150 1
        $object['contents'] = stream_get_contents($object['stream']);
151 1
        fclose($object['stream']);
152 1
        unset($object['stream']);
153
154 1
        return $object;
155
    }
156
157
    /**
158
     * {@inheritdoc}
159
     */
160 2 View Code Duplication
    public function readStream($path)
161
    {
162 2
        $path = $this->applyPathPrefix($path);
163
164
        try {
165 2
            $stream = $this->client->download($path);
166 2
        } catch (BadRequest $e) {
167
            return false;
168
        }
169
170 2
        return compact('stream');
171
    }
172
173
    /**
174
     * {@inheritdoc}
175
     */
176 1
    public function listContents($directory = '', $recursive = false)
177
    {
178 1
        $location = $this->applyPathPrefix($directory);
179
180 1
        $result = $this->client->listFolder($location, $recursive);
181
182 1
        if (!count($result['entries'])) {
183
            return [];
184
        }
185
186 1
        return array_map(function ($entry) {
187 1
            $path = $this->removePathPrefix($entry['path_display']);
188
189 1
            return $this->normalizeResponse($entry, $path);
0 ignored issues
show
Unused Code introduced by
The call to DropboxAdapter::normalizeResponse() has too many arguments starting with $path.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
190 1
        }, $result['entries']);
191
    }
192
193
    /**
194
     * {@inheritdoc}
195
     */
196 5 View Code Duplication
    public function getMetadata($path)
197
    {
198 5
        $path = $this->applyPathPrefix($path);
199
200
        try {
201 5
            $object = $this->client->getMetadata($path);
202 5
        } catch (BadRequest $e) {
203 1
            return false;
204
        }
205
206 4
        return $this->normalizeResponse($object);
207
    }
208
209
    /**
210
     * {@inheritdoc}
211
     */
212 1
    public function getSize($path)
213
    {
214 1
        return $this->getMetadata($path);
215
    }
216
217
    /**
218
     * {@inheritdoc}
219
     */
220
    public function getMimetype($path)
221
    {
222
        throw new LogicException("The Dropbox API v2 does not support mimetypes. Given path: `{$path}`.");
223
    }
224
225
    /**
226
     * {@inheritdoc}
227
     */
228 1
    public function getTimestamp($path)
229
    {
230 1
        return $this->getMetadata($path);
231
    }
232
233
    public function getTemporaryLink($path)
234
    {
235
        return $this->client->getTemporaryLink($path);
236
    }
237
238
    public function getThumbnail($path, $format = 'jpeg', $size = 'w64h64')
239
    {
240
        return $this->client->getThumbnail($path, $format, $size);
241
    }
242
243
    /**
244
     * {@inheritdoc}
245
     */
246 18
    public function applyPathPrefix($path)
247
    {
248 18
        $path = parent::applyPathPrefix($path);
249
250 18
        return '/'.trim($path, '/');
251
    }
252
253 1
    public function getClient()
254
    {
255 1
        return $this->client;
256
    }
257
258
    /**
259
     * @param string          $path
260
     * @param resource|string $contents
261
     * @param string          $mode
262
     *
263
     * @return array|false file metadata
264
     */
265 4 View Code Duplication
    protected function upload($path, $contents, $mode)
266
    {
267 4
        $path = $this->applyPathPrefix($path);
268
269
        try {
270 4
            $object = $this->client->upload($path, $contents, $mode);
271 4
        } catch (BadRequest $e) {
272
            return false;
273
        }
274
275 4
        return $this->normalizeResponse($object);
276
    }
277
}
278