Completed
Push — master ( f0ef68...f72b2d )
by Raza
02:00
created

DropboxAdapter::has()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
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 2
        } catch (\Exception $e) {
69 1
            return false;
70
        }
71
72 1
        return true;
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 2
        } catch (\Exception $e) {
86 1
            return false;
87
        }
88
89 1
        return true;
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 1
        } catch (\Exception $e) {
102
            return false;
103
        }
104
105 1
        return true;
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
123
        try {
124 1
            $object = $this->client->createFolder($path);
125 1
        } catch (\Exception $e) {
126 1
            return false;
127
        }
128
129 1
        return $this->normalizeResponse($object);
130
    }
131
132
    /**
133
     * {@inheritdoc}
134
     */
135 2
    public function has($path)
136
    {
137 2
        return $this->getMetadata($path);
138
    }
139
140
    /**
141
     * {@inheritdoc}
142
     */
143 1
    public function read($path)
144
    {
145 1
        if (!$object = $this->readStream($path)) {
146
            return false;
147
        }
148
149 1
        $object['contents'] = stream_get_contents($object['stream']);
150 1
        fclose($object['stream']);
151 1
        unset($object['stream']);
152
153 1
        return $object;
154
    }
155
156
    /**
157
     * {@inheritdoc}
158
     */
159 2 View Code Duplication
    public function readStream($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...
160
    {
161 2
        $path = $this->applyPathPrefix($path);
162
163
        try {
164 2
            $stream = $this->client->download($path);
165
166 2
            return compact('stream');
167
        } catch (\Exception $e) {
168
            //
169
        }
170
171
        return false;
172
    }
173
174
    /**
175
     * {@inheritdoc}
176
     */
177 1
    public function listContents($directory = '', $recursive = false)
178
    {
179
        try {
180 1
            $location = $this->applyPathPrefix($directory);
181
182 1
            $result = $this->client->listFolder($location, $recursive);
183
184 1
            return array_map(function ($entry) {
185 1
                return $this->normalizeResponse($entry);
186 1
            }, $result['entries']);
187
        } catch (\Exception $exception) {
188
            //
189
        }
190
191
        return [];
192
    }
193
194
    /**
195
     * {@inheritdoc}
196
     */
197 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...
198
    {
199 5
        $path = $this->applyPathPrefix($path);
200
201
        try {
202 5
            $object = $this->client->getMetadata($path);
203
204 4
            return $this->normalizeResponse($object);
205 1
        } catch (\Exception  $e) {
206
            //
207
        }
208
209 1
        return false;
210
    }
211
212
    /**
213
     * {@inheritdoc}
214
     */
215 1
    public function getSize($path)
216
    {
217 1
        return $this->getMetadata($path);
218
    }
219
220
    /**
221
     * {@inheritdoc}
222
     */
223
    public function getMimetype($path)
224
    {
225
        return ['mimetype' => MimeType::detectByFilename($path)];
226
    }
227
228
    /**
229
     * {@inheritdoc}
230
     */
231 1
    public function getTimestamp($path)
232
    {
233 1
        return $this->getMetadata($path);
234
    }
235
236
    /**
237
     * {@inheritdoc}
238
     */
239
    public function getTemporaryLink($path)
240
    {
241
        try {
242
            return $this->client->getTemporaryLink($path);
243
        } catch (\Exception $exception) {
244
            //
245
        }
246
247
        return '';
248
    }
249
250
    /**
251
     * {@inheritdoc}
252
     */
253
    public function getThumbnail($path, $format = 'jpeg', $size = 'w64h64')
254
    {
255
        try {
256
            return $this->client->getThumbnail($path, $format, $size);
257
        } catch (\Exception $exception) {
258
            //
259
        }
260
261
        return '';
262
    }
263
264
    /**
265
     * {@inheritdoc}
266
     */
267 18
    public function applyPathPrefix($path)
268
    {
269 18
        $path = parent::applyPathPrefix($path);
270
271 18
        return '/'.trim($path, '/');
272
    }
273
274
    /**
275
     * @return DropboxClient
276
     */
277 1
    public function getClient()
278
    {
279 1
        return $this->client;
280
    }
281
282
    /**
283
     * @param string          $path
284
     * @param resource|string $contents
285
     * @param string          $mode
286
     *
287
     * @throws \Exception
288
     *
289
     * @return array|false file metadata
290
     */
291 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...
292
    {
293 4
        $path = $this->applyPathPrefix($path);
294
295
        try {
296 4
            $object = $this->client->upload($path, $contents, $mode);
297
298 4
            return $this->normalizeResponse($object);
299
        } catch (\Exception $e) {
300
            //
301
        }
302
303
        return false;
304
    }
305
}
306