These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace Spatie\FlysystemDropbox; |
||
4 | |||
5 | |||
6 | use League\Flysystem\Adapter\AbstractAdapter; |
||
7 | use League\Flysystem\Adapter\Polyfill\NotSupportingVisibilityTrait; |
||
8 | use League\Flysystem\Config; |
||
9 | use Exception; |
||
10 | use League\Flysystem\Util; |
||
11 | use LogicException; |
||
12 | |||
13 | class DropboxAdapter extends AbstractAdapter |
||
14 | { |
||
15 | use NotSupportingVisibilityTrait; |
||
16 | |||
17 | /** @var \Spatie\FlysystemDropbox\DropboxClient */ |
||
18 | protected $client; |
||
19 | |||
20 | public function __construct(DropboxClient $client, string $prefix = null) |
||
21 | { |
||
22 | $this->client = $client; |
||
23 | $this->setPathPrefix($prefix); |
||
24 | } |
||
25 | |||
26 | /** |
||
27 | * {@inheritdoc} |
||
28 | */ |
||
29 | public function write($path, $contents, Config $config) |
||
30 | { |
||
31 | return $this->upload($path, $contents, 'add'); |
||
32 | } |
||
33 | |||
34 | /** |
||
35 | * {@inheritdoc} |
||
36 | */ |
||
37 | public function writeStream($path, $resource, Config $config) |
||
38 | { |
||
39 | return $this->upload($path, $resource, 'add'); |
||
40 | } |
||
41 | |||
42 | /** |
||
43 | * {@inheritdoc} |
||
44 | */ |
||
45 | public function update($path, $contents, Config $config) |
||
46 | { |
||
47 | return $this->upload($path, $contents, 'overwrite'); |
||
48 | } |
||
49 | |||
50 | /** |
||
51 | * {@inheritdoc} |
||
52 | */ |
||
53 | public function updateStream($path, $resource, Config $config) |
||
54 | { |
||
55 | return $this->upload($path, $resource, 'overwrite'); |
||
56 | } |
||
57 | |||
58 | /** |
||
59 | * {@inheritdoc} |
||
60 | */ |
||
61 | public function rename($path, $newPath): bool |
||
62 | { |
||
63 | $path = $this->applyPathPrefix($path); |
||
64 | $newPath = $this->applyPathPrefix($newPath); |
||
65 | |||
66 | try { |
||
67 | $this->client->move($path, $newPath); |
||
68 | } catch (Exception $e) { |
||
69 | return false; |
||
70 | } |
||
71 | |||
72 | return true; |
||
73 | } |
||
74 | |||
75 | /** |
||
76 | * {@inheritdoc} |
||
77 | */ |
||
78 | View Code Duplication | public function copy($path, $newpath): bool |
|
0 ignored issues
–
show
|
|||
79 | { |
||
80 | $path = $this->applyPathPrefix($path); |
||
81 | $newpath = $this->applyPathPrefix($newpath); |
||
82 | |||
83 | try { |
||
84 | $this->client->copy($path, $newpath); |
||
85 | } catch (Exception $e) { |
||
86 | return false; |
||
87 | } |
||
88 | |||
89 | return true; |
||
90 | } |
||
91 | |||
92 | /** |
||
93 | * {@inheritdoc} |
||
94 | */ |
||
95 | public function delete($path): bool |
||
96 | { |
||
97 | $location = $this->applyPathPrefix($path); |
||
98 | |||
99 | try { |
||
100 | $this->client->delete($location); |
||
101 | } catch (Exception $e) { |
||
102 | return false; |
||
103 | } |
||
104 | |||
105 | return true; |
||
106 | } |
||
107 | |||
108 | /** |
||
109 | * {@inheritdoc} |
||
110 | */ |
||
111 | public function deleteDir($dirname): bool |
||
112 | { |
||
113 | return $this->delete($dirname); |
||
114 | } |
||
115 | |||
116 | /** |
||
117 | * {@inheritdoc} |
||
118 | */ |
||
119 | View Code Duplication | public function createDir($dirname, Config $config) |
|
0 ignored issues
–
show
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 | $path = $this->applyPathPrefix($dirname); |
||
122 | |||
123 | $object = $this->client->createFolder($path); |
||
124 | |||
125 | if ($object === null) { |
||
126 | return false; |
||
127 | } |
||
128 | |||
129 | return $this->normalizeResponse($object); |
||
130 | } |
||
131 | |||
132 | /** |
||
133 | * {@inheritdoc} |
||
134 | */ |
||
135 | public function has($path) |
||
136 | { |
||
137 | return $this->getMetadata($path); |
||
138 | } |
||
139 | |||
140 | /** |
||
141 | * {@inheritdoc} |
||
142 | */ |
||
143 | public function read($path) |
||
144 | { |
||
145 | if (! $object = $this->readStream($path)) { |
||
146 | return false; |
||
147 | } |
||
148 | |||
149 | $object['contents'] = stream_get_contents($object['stream']); |
||
150 | fclose($object['stream']); |
||
151 | unset($object['stream']); |
||
152 | |||
153 | return $object; |
||
154 | } |
||
155 | |||
156 | /** |
||
157 | * {@inheritdoc} |
||
158 | */ |
||
159 | public function readStream($path) |
||
160 | { |
||
161 | $path = $this->applyPathPrefix($path); |
||
162 | |||
163 | $stream = $this->client->getFile($path); |
||
164 | |||
165 | return compact('stream'); |
||
166 | } |
||
167 | |||
168 | /** |
||
169 | * {@inheritdoc} |
||
170 | */ |
||
171 | public function listContents($directory = '', $recursive = false): array |
||
172 | { |
||
173 | $listing = []; |
||
174 | |||
175 | $location = $this->applyPathPrefix($directory); |
||
176 | |||
177 | $result = $this->client->listContents($location, $recursive); |
||
178 | |||
179 | if (!count($result['entries'])) { |
||
180 | return []; |
||
181 | } |
||
182 | |||
183 | foreach ($result['entries'] as $object) { |
||
184 | $path = $this->removePathPrefix($object['path_display']); |
||
185 | $listing[] = $this->normalizeResponse($object, $path); |
||
0 ignored issues
–
show
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
Loading history...
|
|||
186 | } |
||
187 | |||
188 | return $listing; |
||
189 | } |
||
190 | |||
191 | /** |
||
192 | * {@inheritdoc} |
||
193 | */ |
||
194 | View Code Duplication | public function getMetadata($path) |
|
0 ignored issues
–
show
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...
|
|||
195 | { |
||
196 | $path = $this->applyPathPrefix($path); |
||
197 | |||
198 | try { |
||
199 | $object = $this->client->getMetadata($path); |
||
200 | } catch(Exception $e) { |
||
201 | return false; |
||
202 | } |
||
203 | |||
204 | return $this->normalizeResponse($object); |
||
205 | } |
||
206 | |||
207 | /** |
||
208 | * {@inheritdoc} |
||
209 | */ |
||
210 | public function getSize($path) |
||
211 | { |
||
212 | return $this->getMetadata($path); |
||
213 | } |
||
214 | |||
215 | /** |
||
216 | * {@inheritdoc} |
||
217 | */ |
||
218 | public function getMimetype($path) |
||
219 | { |
||
220 | throw new LogicException('The Dropbox API v2 does not support mimetypes. Path: ' . $path); |
||
221 | } |
||
222 | |||
223 | /** |
||
224 | * {@inheritdoc} |
||
225 | */ |
||
226 | public function getTimestamp($path) |
||
227 | { |
||
228 | return $this->getMetadata($path); |
||
229 | } |
||
230 | |||
231 | public function getTemporaryLink(string $path): string |
||
232 | { |
||
233 | return $this->client->getTemporaryLink($path); |
||
234 | } |
||
235 | |||
236 | public function getThumbnail(string $path, string $format = 'jpeg', string $size = 'w64h64') |
||
237 | { |
||
238 | return $this->client->getThumbnail($path, $format, $size); |
||
239 | } |
||
240 | |||
241 | /** |
||
242 | * {@inheritdoc} |
||
243 | */ |
||
244 | public function applyPathPrefix($path): string |
||
245 | { |
||
246 | $path = parent::applyPathPrefix($path); |
||
247 | |||
248 | return '/' . ltrim(rtrim($path, '/'), '/'); |
||
249 | } |
||
250 | |||
251 | public function getClient(): DropboxClient |
||
252 | { |
||
253 | return $this->client; |
||
254 | } |
||
255 | |||
256 | /** |
||
257 | * @param string $path |
||
258 | * @param resource|string $contents |
||
259 | * @param string $mode |
||
260 | * |
||
261 | * @return array|false file metadata |
||
262 | */ |
||
263 | protected function upload(string $path, $contents, string $mode) |
||
264 | { |
||
265 | $path = $this->applyPathPrefix($path); |
||
266 | |||
267 | $object = $this->client->upload($path, $mode, $contents); |
||
268 | |||
269 | return $this->normalizeResponse($object); |
||
0 ignored issues
–
show
It seems like
$object defined by $this->client->upload($path, $mode, $contents) on line 267 can also be of type false ; however, Spatie\FlysystemDropbox\...er::normalizeResponse() does only seem to accept array , did you maybe forget to handle an error condition?
This check looks for type mismatches where the missing type is Consider the follow example <?php
function getDate($date)
{
if ($date !== null) {
return new DateTime($date);
}
return false;
}
This function either returns a new
Loading history...
|
|||
270 | } |
||
271 | |||
272 | protected function normalizeResponse(array $response): array |
||
273 | { |
||
274 | $result = ['path' => ltrim($this->removePathPrefix($response['path_display']), '/')]; |
||
275 | |||
276 | if (isset($response['server_modified'])) { |
||
277 | $result['timestamp'] = strtotime($response['server_modified']); |
||
278 | } |
||
279 | |||
280 | if (isset($response['size'])) { |
||
281 | $result['bytes'] = $response['size']; |
||
282 | } |
||
283 | |||
284 | $result['type'] = $response['.tag'] === 'folder' ? 'dir' : 'file'; |
||
285 | |||
286 | return $result; |
||
287 | } |
||
288 | } |
||
289 |
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.