Total Complexity | 41 |
Total Lines | 403 |
Duplicated Lines | 0 % |
Changes | 3 | ||
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 |
||
10 | class BaiduBosAdapter extends AbstractAdapter |
||
11 | { |
||
12 | use UtilTrait; |
||
13 | |||
14 | /** |
||
15 | * @var Client |
||
16 | */ |
||
17 | protected $client; |
||
18 | |||
19 | /** |
||
20 | * BaiduBosAdapter constructor. |
||
21 | * |
||
22 | * @param Client $client |
||
23 | */ |
||
24 | public function __construct(Client $client) |
||
25 | { |
||
26 | $this->client = $client; |
||
27 | } |
||
28 | |||
29 | /** |
||
30 | * @return Client |
||
31 | */ |
||
32 | public function getClient() |
||
33 | { |
||
34 | return $this->client; |
||
35 | } |
||
36 | |||
37 | /** |
||
38 | * Write a new file. |
||
39 | * |
||
40 | * @param string $path |
||
41 | * @param string $contents |
||
42 | * @param Config $config Config object |
||
43 | * |
||
44 | * @return array|false false on failure file meta data on success |
||
45 | */ |
||
46 | public function write($path, $contents, Config $config) |
||
47 | { |
||
48 | try { |
||
49 | $this->client |
||
50 | ->putObject($path, $contents, $this->extractOptions($config)); |
||
51 | } catch (Exception $exception) { |
||
52 | return false; |
||
53 | } |
||
54 | |||
55 | return $this->client->getObjectMeta($path); |
||
56 | } |
||
57 | |||
58 | /** |
||
59 | * Write a new file using a stream. |
||
60 | * |
||
61 | * @param string $path |
||
62 | * @param resource $resource |
||
63 | * @param Config $config Config object |
||
64 | * |
||
65 | * @return array|false false on failure file meta data on success |
||
66 | */ |
||
67 | public function writeStream($path, $resource, Config $config) |
||
68 | { |
||
69 | return $this->write($path, stream_get_contents($resource), $config); |
||
70 | } |
||
71 | |||
72 | /** |
||
73 | * Update a file. |
||
74 | * |
||
75 | * @param string $path |
||
76 | * @param string $contents |
||
77 | * @param Config $config Config object |
||
78 | * |
||
79 | * @return array|false false on failure file meta data on success |
||
80 | */ |
||
81 | public function update($path, $contents, Config $config) |
||
82 | { |
||
83 | return $this->write($path, $contents, $config); |
||
84 | } |
||
85 | |||
86 | /** |
||
87 | * Update a file using a stream. |
||
88 | * |
||
89 | * @param string $path |
||
90 | * @param resource $resource |
||
91 | * @param Config $config Config object |
||
92 | * |
||
93 | * @return array|false false on failure file meta data on success |
||
94 | */ |
||
95 | public function updateStream($path, $resource, Config $config) |
||
96 | { |
||
97 | return $this->writeStream($path, $resource, $config); |
||
98 | } |
||
99 | |||
100 | /** |
||
101 | * Rename a file. |
||
102 | * |
||
103 | * @param string $path |
||
104 | * @param string $newPath |
||
105 | * |
||
106 | * @return bool |
||
107 | */ |
||
108 | public function rename($path, $newPath) |
||
109 | { |
||
110 | try { |
||
111 | $this->client->copyObject($path, $newPath); |
||
112 | $this->client->deleteObject($path); |
||
113 | } catch (Exception $exception) { |
||
114 | return false; |
||
115 | } |
||
116 | |||
117 | return true; |
||
118 | } |
||
119 | |||
120 | /** |
||
121 | * Copy a file. |
||
122 | * |
||
123 | * @param string $path |
||
124 | * @param string $newPath |
||
125 | * |
||
126 | * @return bool |
||
127 | */ |
||
128 | public function copy($path, $newPath) |
||
129 | { |
||
130 | try { |
||
131 | $this->client->copyObject($path, $newPath); |
||
132 | } catch (Exception $exception) { |
||
133 | return false; |
||
134 | } |
||
135 | |||
136 | return true; |
||
137 | } |
||
138 | |||
139 | /** |
||
140 | * Delete a file. |
||
141 | * |
||
142 | * @param string $path |
||
143 | * |
||
144 | * @return bool |
||
145 | */ |
||
146 | public function delete($path) |
||
147 | { |
||
148 | try { |
||
149 | $this->client->deleteObject($path); |
||
150 | } catch (Exception $exception) { |
||
151 | return false; |
||
152 | } |
||
153 | |||
154 | return true; |
||
155 | } |
||
156 | |||
157 | /** |
||
158 | * Delete a directory. |
||
159 | * |
||
160 | * @param string $dirname |
||
161 | * |
||
162 | * @return bool |
||
163 | */ |
||
164 | public function deleteDir($dirname) |
||
165 | { |
||
166 | try { |
||
167 | $this->client->deleteObject(rtrim($dirname, '/') . '/'); |
||
168 | } catch (Exception $exception) { |
||
169 | return false; |
||
170 | } |
||
171 | |||
172 | return true; |
||
173 | } |
||
174 | |||
175 | /** |
||
176 | * Create a directory. |
||
177 | * |
||
178 | * @param string $dirname directory name |
||
179 | * @param Config $config |
||
180 | * |
||
181 | * @return bool |
||
182 | */ |
||
183 | public function createDir($dirname, Config $config) |
||
184 | { |
||
185 | try { |
||
186 | $this->client->putObject( |
||
187 | rtrim($dirname, '/') . '/', |
||
188 | '', |
||
189 | $this->extractOptions($config) |
||
190 | ); |
||
191 | } catch (Exception $exception) { |
||
192 | return false; |
||
193 | } |
||
194 | |||
195 | return true; |
||
|
|||
196 | } |
||
197 | |||
198 | /** |
||
199 | * Check whether a file exists. |
||
200 | * |
||
201 | * @param string $path |
||
202 | * |
||
203 | * @return bool |
||
204 | */ |
||
205 | public function has($path) |
||
206 | { |
||
207 | try { |
||
208 | $this->client->getObjectMeta($path); |
||
209 | } catch (Exception $exception) { |
||
210 | return false; |
||
211 | } |
||
212 | |||
213 | return true; |
||
214 | } |
||
215 | |||
216 | /** |
||
217 | * Read a file. |
||
218 | * |
||
219 | * @param string $path |
||
220 | * |
||
221 | * @return array|false |
||
222 | */ |
||
223 | public function read($path) |
||
224 | { |
||
225 | try { |
||
226 | $contents = $this->client->getObject($path); |
||
227 | } catch (Exception $exception) { |
||
228 | return false; |
||
229 | } |
||
230 | |||
231 | return compact('path', 'contents'); |
||
232 | } |
||
233 | |||
234 | /** |
||
235 | * Read a file as a stream. |
||
236 | * |
||
237 | * @param string $path |
||
238 | * |
||
239 | * @return array|false |
||
240 | */ |
||
241 | public function readStream($path) |
||
242 | { |
||
243 | $result = $this->read($path); |
||
244 | if ($result === false) { |
||
245 | return false; |
||
246 | } |
||
247 | |||
248 | $stream = fopen('php://temp', 'w+b'); |
||
249 | fputs($stream, $result['contents']); |
||
250 | rewind($stream); |
||
251 | |||
252 | return compact('path', 'stream'); |
||
253 | } |
||
254 | |||
255 | /** |
||
256 | * List contents of a directory. |
||
257 | * |
||
258 | * @param string $directory |
||
259 | * @param bool $recursive |
||
260 | * |
||
261 | * @return array |
||
262 | */ |
||
263 | public function listContents($directory = '', $recursive = false) |
||
264 | { |
||
265 | $options = $this->buildListDirOptions($directory, $recursive); |
||
266 | $result = $this->client->listObjects($options); |
||
267 | |||
268 | $contents = []; |
||
269 | foreach ($result['contents'] as $row) { |
||
270 | if ($row['key'] === $directory) { |
||
271 | continue; |
||
272 | } |
||
273 | |||
274 | $contents[] = $this->normalizeContent($row); |
||
275 | } |
||
276 | |||
277 | return $contents; |
||
278 | } |
||
279 | |||
280 | /** |
||
281 | * Get all the meta data of a file or directory. |
||
282 | * |
||
283 | * @param string $path |
||
284 | * |
||
285 | * @return array|false |
||
286 | */ |
||
287 | public function getMetadata($path) |
||
288 | { |
||
289 | try { |
||
290 | $meta = $this->client->getObjectMeta($path); |
||
291 | } catch (Exception $exception) { |
||
292 | return false; |
||
293 | } |
||
294 | |||
295 | return $this->normalizeMeta($meta, $path); |
||
296 | } |
||
297 | |||
298 | /** |
||
299 | * Get the size of a file. |
||
300 | * |
||
301 | * @param string $path |
||
302 | * |
||
303 | * @return array|false |
||
304 | */ |
||
305 | public function getSize($path) |
||
306 | { |
||
307 | return $this->getMetadata($path); |
||
308 | } |
||
309 | |||
310 | /** |
||
311 | * Get the mime-type of a file. |
||
312 | * |
||
313 | * @param string $path |
||
314 | * |
||
315 | * @return array|false |
||
316 | */ |
||
317 | public function getMimeType($path) |
||
318 | { |
||
319 | return $this->getMetadata($path); |
||
320 | } |
||
321 | |||
322 | /** |
||
323 | * Get the timestamp of a file. |
||
324 | * |
||
325 | * @param string $path |
||
326 | * |
||
327 | * @return array|false |
||
328 | */ |
||
329 | public function getTimestamp($path) |
||
330 | { |
||
331 | return $this->getMetadata($path); |
||
332 | } |
||
333 | |||
334 | /** |
||
335 | * Get the visibility of a file. |
||
336 | * |
||
337 | * @param string $path |
||
338 | * |
||
339 | * @return array|false |
||
340 | */ |
||
341 | public function getVisibility($path) |
||
357 | } |
||
358 | |||
359 | /** |
||
360 | * Set the visibility for a file. |
||
361 | * |
||
362 | * @param string $path |
||
363 | * @param string $visibility |
||
364 | * |
||
365 | * @return array|false file meta data |
||
366 | */ |
||
367 | public function setVisibility($path, $visibility) |
||
376 | } |
||
377 | |||
378 | /** |
||
379 | * Get object acl, if not set, return bucket acl |
||
380 | * |
||
381 | * @param $path |
||
382 | * |
||
383 | * @return array|false |
||
384 | */ |
||
385 | protected function getObjectAcl($path) |
||
386 | { |
||
387 | try { |
||
388 | $result = $this->client->getObjectAcl($path); |
||
389 | return $result['accessControlList']; |
||
390 | } catch (Exception $exception) { |
||
391 | if ($exception->getCode() == 404) { |
||
392 | return $this->getBucketAcl(); |
||
393 | } |
||
394 | } |
||
395 | |||
396 | return false; |
||
397 | } |
||
398 | |||
399 | /** |
||
400 | * Get bucket acl |
||
401 | * |
||
402 | * @return array|false |
||
403 | */ |
||
404 | protected function getBucketAcl() |
||
413 | } |
||
414 | } |
||
415 |
In the issue above, the returned value is violating the contract defined by the mentioned interface.
Let's take a look at an example: