Total Complexity | 40 |
Total Lines | 448 |
Duplicated Lines | 0 % |
Changes | 4 | ||
Bugs | 0 | Features | 0 |
Complex classes like BaiduBosAdapter often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use BaiduBosAdapter, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
22 | class BaiduBosAdapter implements FilesystemAdapter |
||
23 | { |
||
24 | use UtilTrait; |
||
25 | |||
26 | /** |
||
27 | * @var Client |
||
28 | */ |
||
29 | protected Client $client; |
||
30 | |||
31 | /** |
||
32 | * BaiduBosAdapter constructor. |
||
33 | * |
||
34 | * @param Client $client |
||
35 | */ |
||
36 | public function __construct(Client $client) |
||
37 | { |
||
38 | $this->client = $client; |
||
39 | } |
||
40 | |||
41 | /** |
||
42 | * Write a new file. |
||
43 | * |
||
44 | * @param string $path |
||
45 | * @param string $contents |
||
46 | * @param Config $config |
||
47 | * |
||
48 | * @return void |
||
49 | * @throws UnableToWriteFile |
||
50 | */ |
||
51 | public function write(string $path, string $contents, Config $config): void |
||
52 | { |
||
53 | try { |
||
54 | $this->client |
||
55 | ->putObject($path, $contents, $this->extractOptions($config)); |
||
56 | } catch (Throwable $e) { |
||
57 | throw UnableToWriteFile::atLocation($path, $e->getMessage(), $e); |
||
58 | } |
||
59 | } |
||
60 | |||
61 | /** |
||
62 | * Write a new file using a stream. |
||
63 | * |
||
64 | * @param string $path |
||
65 | * @param resource $contents |
||
66 | * @param Config $config |
||
67 | * |
||
68 | * @return void |
||
69 | * @throws UnableToWriteFile |
||
70 | */ |
||
71 | public function writeStream(string $path, $contents, Config $config): void |
||
72 | { |
||
73 | $this->write($path, stream_get_contents($contents), $config); |
||
74 | } |
||
75 | |||
76 | /** |
||
77 | * Rename/Move a file. |
||
78 | * |
||
79 | * @param string $source |
||
80 | * @param string $destination |
||
81 | * @param Config $config |
||
82 | * |
||
83 | * @return void |
||
84 | * @throws UnableToMoveFile |
||
85 | */ |
||
86 | public function move( |
||
87 | string $source, |
||
88 | string $destination, |
||
89 | Config $config |
||
90 | ): void |
||
91 | { |
||
92 | try { |
||
93 | $this->client->copyObject($source, $destination); |
||
94 | $this->client->deleteObject($source); |
||
95 | } catch (Throwable $e) { |
||
96 | throw UnableToMoveFile::fromLocationTo($source, $destination, $e); |
||
97 | } |
||
98 | } |
||
99 | |||
100 | |||
101 | /** |
||
102 | * Copy a file. |
||
103 | * |
||
104 | * @param string $source |
||
105 | * @param string $destination |
||
106 | * @param Config $config |
||
107 | * |
||
108 | * @return void |
||
109 | * @throws UnableToCopyFile |
||
110 | */ |
||
111 | public function copy( |
||
112 | string $source, |
||
113 | string $destination, |
||
114 | Config $config |
||
115 | ): void |
||
116 | { |
||
117 | try { |
||
118 | $this->client->copyObject($source, $destination); |
||
119 | } catch (Throwable $e) { |
||
120 | throw UnableToCopyFile::fromLocationTo( |
||
121 | $source, |
||
122 | $e->getMessage(), |
||
123 | $e |
||
124 | ); |
||
125 | } |
||
126 | } |
||
127 | |||
128 | /** |
||
129 | * Delete a file. |
||
130 | * |
||
131 | * @param string $path |
||
132 | * |
||
133 | * @return void |
||
134 | * @throws UnableToDeleteFile |
||
135 | */ |
||
136 | public function delete(string $path): void |
||
137 | { |
||
138 | try { |
||
139 | $this->client->deleteObject($path); |
||
140 | } catch (Throwable $e) { |
||
141 | throw UnableToDeleteFile::atLocation($path, $e->getMessage(), $e); |
||
142 | } |
||
143 | } |
||
144 | |||
145 | /** |
||
146 | * Check whether a file exists. |
||
147 | * |
||
148 | * @param string $path |
||
149 | * |
||
150 | * @return bool |
||
151 | */ |
||
152 | public function fileExists(string $path): bool |
||
153 | { |
||
154 | try { |
||
155 | $this->client->getObjectMeta($path); |
||
156 | } catch (Throwable $e) { |
||
157 | error_log($e->getMessage()); |
||
158 | return false; |
||
159 | } |
||
160 | |||
161 | return true; |
||
162 | } |
||
163 | |||
164 | /** |
||
165 | * Check whether a dir exists. |
||
166 | * |
||
167 | * @param string $path |
||
168 | * |
||
169 | * @return bool |
||
170 | */ |
||
171 | public function directoryExists(string $path): bool |
||
172 | { |
||
173 | return $this->fileExists($path); |
||
174 | } |
||
175 | |||
176 | |||
177 | /** |
||
178 | * Create a directory. |
||
179 | * |
||
180 | * @param string $path |
||
181 | * @param Config $config |
||
182 | * |
||
183 | * @return void |
||
184 | * @throws UnableToCreateDirectory |
||
185 | */ |
||
186 | public function createDirectory(string $path, Config $config): void |
||
187 | { |
||
188 | try { |
||
189 | $this->client->putObject( |
||
190 | rtrim($path, '/') . '/', |
||
191 | '', |
||
192 | $this->extractOptions($config) |
||
193 | ); |
||
194 | } catch (Throwable $e) { |
||
195 | throw UnableToCreateDirectory::atLocation( |
||
196 | $path, |
||
197 | $e->getMessage(), |
||
198 | $e |
||
199 | ); |
||
200 | } |
||
201 | |||
202 | } |
||
203 | |||
204 | /** |
||
205 | * Get the mime-type of a file. |
||
206 | * |
||
207 | * @param string $path |
||
208 | * |
||
209 | * @return FileAttributes |
||
210 | * @throws UnableToRetrieveMetadata |
||
211 | */ |
||
212 | public function mimeType(string $path): FileAttributes |
||
213 | { |
||
214 | try { |
||
215 | $meta = $this->getMetadata($path); |
||
216 | $mimeType = $meta['mimeType']; |
||
217 | } catch (Throwable $e) { |
||
218 | throw UnableToRetrieveMetadata::mimeType( |
||
219 | $path, |
||
220 | $e->getMessage(), |
||
221 | $e |
||
222 | ); |
||
223 | } |
||
224 | |||
225 | return new FileAttributes($path, null, null, null, $mimeType); |
||
226 | } |
||
227 | |||
228 | |||
229 | /** |
||
230 | * Get the lastModified of a file. |
||
231 | * |
||
232 | * @param string $path |
||
233 | * |
||
234 | * @return FileAttributes |
||
235 | * @throws UnableToRetrieveMetadata |
||
236 | */ |
||
237 | public function lastModified(string $path): FileAttributes |
||
238 | { |
||
239 | try { |
||
240 | $meta = $this->getMetadata($path); |
||
241 | $lastModified = $meta['lastModified']; |
||
242 | } catch (Throwable $e) { |
||
243 | throw UnableToRetrieveMetadata::lastModified( |
||
244 | $path, |
||
245 | $e->getMessage(), |
||
246 | $e |
||
247 | ); |
||
248 | } |
||
249 | |||
250 | return new FileAttributes($path, null, null, $lastModified); |
||
251 | } |
||
252 | |||
253 | /** |
||
254 | * Get the size of a file. |
||
255 | * |
||
256 | * @param string $path |
||
257 | * |
||
258 | * @return FileAttributes |
||
259 | * @throws UnableToRetrieveMetadata |
||
260 | */ |
||
261 | public function fileSize(string $path): FileAttributes |
||
262 | { |
||
263 | try { |
||
264 | $meta = $this->getMetadata($path); |
||
265 | $fileSize = $meta['fileSize']; |
||
266 | } catch (Throwable $e) { |
||
267 | throw UnableToRetrieveMetadata::fileSize( |
||
268 | $path, |
||
269 | $e->getMessage(), |
||
270 | $e |
||
271 | ); |
||
272 | } |
||
273 | |||
274 | return new FileAttributes($path, $fileSize); |
||
275 | } |
||
276 | |||
277 | |||
278 | /** |
||
279 | * Delete a directory. |
||
280 | * |
||
281 | * @param string $path |
||
282 | * |
||
283 | * @return void |
||
284 | * @throws UnableToDeleteDirectory |
||
285 | */ |
||
286 | public function deleteDirectory(string $path): void |
||
287 | { |
||
288 | try { |
||
289 | $this->client->deleteObject(rtrim($path, '/') . '/'); |
||
290 | } catch (Throwable $e) { |
||
291 | throw UnableToDeleteDirectory::atLocation( |
||
292 | $path, |
||
293 | $e->getMessage(), |
||
294 | $e |
||
295 | ); |
||
296 | } |
||
297 | } |
||
298 | |||
299 | |||
300 | /** |
||
301 | * Read a file. |
||
302 | * |
||
303 | * @param string $path |
||
304 | * |
||
305 | * @return string |
||
306 | * @throws UnableToReadFile |
||
307 | */ |
||
308 | public function read(string $path): string |
||
309 | { |
||
310 | try { |
||
311 | $contents = $this->client->getObject($path); |
||
312 | } catch (Throwable $e) { |
||
313 | throw UnableToReadFile::fromLocation($path, $e->getMessage(), $e); |
||
314 | } |
||
315 | |||
316 | return $contents; |
||
317 | } |
||
318 | |||
319 | /** |
||
320 | * Read a file as a stream. |
||
321 | * |
||
322 | * @param string $path |
||
323 | * |
||
324 | * @return resource |
||
325 | * @throws UnableToReadFile |
||
326 | */ |
||
327 | public function readStream(string $path) |
||
328 | { |
||
329 | $contents = $this->read($path); |
||
330 | $stream = fopen('php://temp', 'w+b'); |
||
331 | fputs($stream, $contents); |
||
332 | rewind($stream); |
||
333 | |||
334 | return $stream; |
||
335 | } |
||
336 | |||
337 | /** |
||
338 | * List contents of a directory. |
||
339 | * |
||
340 | * @param string $path |
||
341 | * @param bool $deep |
||
342 | * |
||
343 | * @return iterable |
||
344 | * @throws UnableToListContents |
||
345 | */ |
||
346 | public function listContents(string $path, bool $deep): iterable |
||
347 | { |
||
348 | $options = $this->buildListDirOptions($path, $deep); |
||
349 | try { |
||
350 | $result = $this->client->listObjects($options); |
||
351 | } catch (Throwable $e) { |
||
352 | throw UnableToListContents::atLocation($path, $e->getMessage(), $e); |
||
|
|||
353 | } |
||
354 | |||
355 | $prefixes = isset($result['commonPrefixes']) |
||
356 | ? array_map(function ($item) { |
||
357 | return ['key' => $item['prefix']]; |
||
358 | }, $result['commonPrefixes']) |
||
359 | : []; |
||
360 | |||
361 | return array_map(function ($content) { |
||
362 | return $this->normalizeContent($content); |
||
363 | }, array_merge($result['contents'], $prefixes)); |
||
364 | } |
||
365 | |||
366 | /** |
||
367 | * Get the visibility of a file. |
||
368 | * |
||
369 | * @param string $path |
||
370 | * |
||
371 | * @return FileAttributes |
||
372 | */ |
||
373 | |||
374 | public function visibility(string $path): FileAttributes |
||
375 | { |
||
376 | try { |
||
377 | $acl = $this->getObjectAcl($path); |
||
378 | } catch (Throwable $e) { |
||
379 | throw UnableToRetrieveMetadata::visibility( |
||
380 | $path, |
||
381 | $e->getMessage(), |
||
382 | $e |
||
383 | ); |
||
384 | } |
||
385 | |||
386 | $permissions = $this->extractPermissions($acl); |
||
387 | |||
388 | if (in_array('READ', $permissions)) { |
||
389 | $visibility = 'public'; |
||
390 | } else { |
||
391 | $visibility = 'private'; |
||
392 | } |
||
393 | |||
394 | return new FileAttributes($path, null, $visibility); |
||
395 | } |
||
396 | |||
397 | |||
398 | /** |
||
399 | * Set the visibility for a file. |
||
400 | * |
||
401 | * @param string $path |
||
402 | * @param string $visibility |
||
403 | * |
||
404 | * @return void |
||
405 | */ |
||
406 | public function setVisibility(string $path, string $visibility): void |
||
407 | { |
||
408 | if ($visibility === 'public') { |
||
409 | $visibility = 'public-read'; |
||
410 | } |
||
411 | |||
412 | try { |
||
413 | $this->client->putObjectAcl($path, $visibility); |
||
414 | } catch (Throwable $e) { |
||
415 | throw UnableToSetVisibility::atLocation( |
||
416 | $path, |
||
417 | $e->getMessage(), |
||
418 | $e |
||
419 | ); |
||
420 | } |
||
421 | } |
||
422 | |||
423 | /** |
||
424 | * Get all the meta data of a file or directory. |
||
425 | * |
||
426 | * @param string $path |
||
427 | * |
||
428 | * @return array |
||
429 | * @throws Exception |
||
430 | */ |
||
431 | protected function getMetadata(string $path): array |
||
435 | } |
||
436 | |||
437 | /** |
||
438 | * Get object acl, if not set, return bucket acl |
||
439 | * |
||
440 | * @param string $path |
||
441 | * |
||
442 | * @return array |
||
443 | * @throws Throwable |
||
444 | * @throws Exception |
||
445 | */ |
||
446 | protected function getObjectAcl(string $path): array |
||
447 | { |
||
448 | try { |
||
449 | $result = $this->client->getObjectAcl($path); |
||
450 | return $result['accessControlList']; |
||
451 | } catch (Throwable $exception) { |
||
452 | if ($exception->getCode() == 404) { |
||
453 | return $this->getBucketAcl(); |
||
454 | } |
||
455 | |||
456 | throw $exception; |
||
457 | } |
||
458 | } |
||
459 | |||
460 | /** |
||
461 | * Get bucket acl |
||
462 | * |
||
463 | * @return array |
||
464 | * @throws Exception |
||
465 | */ |
||
466 | protected function getBucketAcl(): array |
||
470 | } |
||
471 | } |
||
472 |