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 GuzzleHttp\Client; |
||
7 | use GuzzleHttp\Psr7\StreamWrapper; |
||
8 | use Psr\Http\Message\StreamInterface; |
||
9 | |||
10 | class DropboxClient |
||
11 | { |
||
12 | protected $accessToken; |
||
13 | |||
14 | protected $client; |
||
15 | |||
16 | public function __construct(string $accessToken) |
||
17 | { |
||
18 | $this->accessToken = $accessToken; |
||
19 | |||
20 | $this->client = new Client([ |
||
21 | 'base_uri' => 'https://api.dropboxapi.com/2/', |
||
22 | 'headers' => [ |
||
23 | 'Authorization' => "Bearer {$this->accessToken}", |
||
24 | ], |
||
25 | ]); |
||
26 | } |
||
27 | |||
28 | View Code Duplication | public function move(string $path, string $newPath): array |
|
29 | { |
||
30 | $path = $this->normalizePath($path); |
||
31 | $newPath = $this->normalizePath($newPath); |
||
32 | |||
33 | $response = $this->client->post('files/move', [ |
||
34 | 'json' => [ |
||
35 | 'from_path' => $path, |
||
36 | 'to_path' => $newPath, |
||
37 | ] |
||
38 | ]); |
||
39 | |||
40 | return json_decode($response->getBody(), true); |
||
41 | } |
||
42 | |||
43 | View Code Duplication | public function copy(string $path, string $newPath): array |
|
0 ignored issues
–
show
|
|||
44 | { |
||
45 | $path = $this->normalizePath($path); |
||
46 | $newPath = $this->normalizePath($newPath); |
||
47 | |||
48 | $response = $this->client->post('files/copy', [ |
||
49 | 'json' => [ |
||
50 | 'from_path' => $path, |
||
51 | 'to_path' => $newPath, |
||
52 | ] |
||
53 | ]); |
||
54 | |||
55 | return json_decode($response->getBody(), true); |
||
56 | } |
||
57 | |||
58 | View Code Duplication | public function delete(string $path): array |
|
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...
|
|||
59 | { |
||
60 | $path = $this->normalizePath($path); |
||
61 | |||
62 | $response = $this->client->post('files/delete', [ |
||
63 | 'json' => [ |
||
64 | 'path' => $path, |
||
65 | ] |
||
66 | ]); |
||
67 | |||
68 | return json_decode($response->getBody(), true); |
||
69 | } |
||
70 | |||
71 | /** |
||
72 | * @param string $path |
||
73 | * |
||
74 | * @return mixed |
||
75 | */ |
||
76 | public function createFolder(string $path) |
||
77 | { |
||
78 | $path = $this->normalizePath($path); |
||
79 | |||
80 | $response = $this->client->post('files/create_folder', [ |
||
81 | 'json' => [ |
||
82 | 'path' => $path, |
||
83 | ] |
||
84 | ]); |
||
85 | |||
86 | $result = json_decode($response->getBody(), true); |
||
87 | |||
88 | $result['.tag'] = 'folder'; |
||
89 | |||
90 | return $result; |
||
91 | } |
||
92 | |||
93 | View Code Duplication | public function getMetadata(string $path) |
|
94 | { |
||
95 | $path = $this->normalizePath($path); |
||
96 | |||
97 | $response = $this->client->post('files/get_metadata', [ |
||
98 | 'json' => [ |
||
99 | 'path' => $path, |
||
100 | ] |
||
101 | ]); |
||
102 | |||
103 | return json_decode($response->getBody(), true); |
||
104 | } |
||
105 | |||
106 | public function getFile(string $path) |
||
107 | { |
||
108 | $dropboxApiArguments = [ |
||
109 | 'path' => $this->normalizePath($path), |
||
110 | ]; |
||
111 | |||
112 | $response = $this->client->post('https://content.dropboxapi.com/2/files/download', [ |
||
113 | 'headers' => [ |
||
114 | 'Dropbox-API-Arg' => json_encode($dropboxApiArguments), |
||
115 | ], |
||
116 | ]); |
||
117 | |||
118 | return StreamWrapper::getResource($response->getBody()); |
||
119 | } |
||
120 | |||
121 | View Code Duplication | public function listContents($path = '', $recursive = false): array |
|
122 | { |
||
123 | $path = $this->normalizePath($path); |
||
124 | |||
125 | $response = $this->client->post('files/list_folder', [ |
||
126 | 'json' => [ |
||
127 | 'path' => $path, |
||
128 | 'recursive' => $recursive, |
||
129 | ] |
||
130 | ]); |
||
131 | |||
132 | return json_decode($response->getBody(), true); |
||
133 | } |
||
134 | |||
135 | View Code Duplication | public function getTemporaryLink(string $path): string |
|
136 | { |
||
137 | $path = $this->normalizePath($path); |
||
138 | |||
139 | $response = $this->client->post('files/get_temporary_link', [ |
||
140 | 'json' => [ |
||
141 | 'path' => $path, |
||
142 | ], |
||
143 | ]); |
||
144 | |||
145 | $body = json_decode($response->getBody(), true); |
||
146 | |||
147 | return $body['link']; |
||
148 | } |
||
149 | |||
150 | public function getThumbnail(string $path, string $format = 'jpeg', string $size = 'w64h64'): string |
||
151 | { |
||
152 | $dropboxApiArguments = [ |
||
153 | 'path' => $this->normalizePath($path), |
||
154 | 'format' => $format, |
||
155 | 'size' => $size |
||
156 | ]; |
||
157 | |||
158 | $response = $this->client->post('https://content.dropboxapi.com/2/files/get_thumbnail', [ |
||
159 | 'headers' => [ |
||
160 | 'Dropbox-API-Arg' => json_encode($dropboxApiArguments), |
||
161 | ], |
||
162 | ]); |
||
163 | |||
164 | return (string) $response->getBody(); |
||
165 | } |
||
166 | |||
167 | /** |
||
168 | * @param string $path |
||
169 | * @param string $mode |
||
170 | * @param string|resource $contents |
||
171 | * |
||
172 | * @return array|false |
||
173 | */ |
||
174 | public function upload(string $path, string $mode, $contents) |
||
175 | { |
||
176 | $dropboxApiArguments = [ |
||
177 | 'path' => $this->normalizePath($path), |
||
178 | 'mode' => $mode, |
||
179 | 'autorename' => true, |
||
180 | ]; |
||
181 | |||
182 | $response = $this->client->post('https://content.dropboxapi.com/2/files/upload', [ |
||
183 | 'headers' => [ |
||
184 | 'Dropbox-API-Arg' => json_encode($dropboxApiArguments), |
||
185 | 'Content-Type' => 'application/octet-stream', |
||
186 | ], |
||
187 | 'body' => $contents, |
||
188 | ]); |
||
189 | |||
190 | $metadata = json_decode($response->getBody(), true); |
||
191 | |||
192 | $metadata['.tag'] = 'file'; |
||
193 | |||
194 | return $metadata; |
||
195 | } |
||
196 | |||
197 | public function normalizePath(string $path): string |
||
198 | { |
||
199 | $path = trim($path,'/'); |
||
200 | |||
201 | if ($path === '') { |
||
202 | return ''; |
||
203 | } |
||
204 | |||
205 | return '/'.$path; |
||
206 | } |
||
207 | } |
||
208 |
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.