Complex classes like Adapter 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 Adapter, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 16 | class Adapter extends AbstractAdapter |
||
| 17 | { |
||
| 18 | /** |
||
| 19 | * @var Client |
||
| 20 | */ |
||
| 21 | protected $client; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @var array |
||
| 25 | */ |
||
| 26 | protected $config = []; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @var array |
||
| 30 | */ |
||
| 31 | protected $regionMap = [ |
||
| 32 | 'cn-east' => 'ap-shanghai', |
||
| 33 | 'cn-sorth' => 'ap-guangzhou', |
||
| 34 | 'cn-north' => 'ap-beijing-1', |
||
| 35 | 'cn-south-2' => 'ap-guangzhou-2', |
||
| 36 | 'cn-southwest' => 'ap-chengdu', |
||
| 37 | 'sg' => 'ap-singapore', |
||
| 38 | 'tj' => 'ap-beijing-1', |
||
| 39 | 'bj' => 'ap-beijing', |
||
| 40 | 'sh' => 'ap-shanghai', |
||
| 41 | 'gz' => 'ap-guangzhou', |
||
| 42 | 'cd' => 'ap-chengdu', |
||
| 43 | 'sgp' => 'ap-singapore', |
||
| 44 | ]; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Adapter constructor. |
||
| 48 | * |
||
| 49 | * @param Client $client |
||
| 50 | * @param array $config |
||
| 51 | */ |
||
| 52 | public function __construct(Client $client, array $config) |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @return string |
||
| 62 | */ |
||
| 63 | 18 | public function getBucket() |
|
| 67 | |||
| 68 | /** |
||
| 69 | * @return string |
||
| 70 | */ |
||
| 71 | 2 | public function getAppId() |
|
| 75 | |||
| 76 | /** |
||
| 77 | * @return string |
||
| 78 | */ |
||
| 79 | 2 | public function getRegion() |
|
| 84 | |||
| 85 | /** |
||
| 86 | * @param $path |
||
| 87 | * |
||
| 88 | * @return string |
||
| 89 | */ |
||
| 90 | 2 | public function getSourcePath($path) |
|
| 96 | |||
| 97 | /** |
||
| 98 | * @param string $path |
||
| 99 | * |
||
| 100 | * @return string |
||
| 101 | */ |
||
| 102 | 6 | public function getUrl($path) |
|
| 103 | { |
||
| 104 | 6 | if ($this->config['cdn']) { |
|
| 105 | 2 | return $this->applyPathPrefix($path); |
|
| 106 | } |
||
| 107 | |||
| 108 | $objectUrl = $this->client->getObjectUrl( |
||
| 109 | $this->getBucket(), $path, null, |
||
| 110 | 4 | ['Scheme' => isset($this->config['scheme']) ? $this->config['scheme'] : 'http'] |
|
| 111 | ); |
||
| 112 | |||
| 113 | 4 | $url = parse_url($objectUrl); |
|
| 114 | |||
| 115 | return sprintf('%s://%s%s', $url['scheme'], $url['host'], urldecode($url['path'])); |
||
| 116 | } |
||
| 117 | |||
| 118 | /** |
||
| 119 | * @param string $path |
||
| 120 | * @param \DateTimeInterface $expiration |
||
| 121 | * @param array $options |
||
| 122 | * |
||
| 123 | * @return string |
||
| 124 | */ |
||
| 125 | public function getTemporaryUrl($path, DateTimeInterface $expiration, array $options = []) |
||
| 142 | |||
| 143 | /** |
||
| 144 | * @param string $path |
||
| 145 | * @param string $contents |
||
| 146 | * @param Config $config |
||
| 147 | * |
||
| 148 | * @return array|bool |
||
| 149 | */ |
||
| 150 | 2 | public function write($path, $contents, Config $config) |
|
| 154 | |||
| 155 | /** |
||
| 156 | * @param string $path |
||
| 157 | * @param resource $resource |
||
| 158 | * @param Config $config |
||
| 159 | * |
||
| 160 | * @return array|bool |
||
| 161 | */ |
||
| 162 | 2 | public function writeStream($path, $resource, Config $config) |
|
| 166 | |||
| 167 | /** |
||
| 168 | * @param string $path |
||
| 169 | * @param string $contents |
||
| 170 | * @param Config $config |
||
| 171 | * |
||
| 172 | * @return array|bool |
||
| 173 | */ |
||
| 174 | 3 | public function update($path, $contents, Config $config) |
|
| 178 | |||
| 179 | /** |
||
| 180 | * @param string $path |
||
| 181 | * @param resource $resource |
||
| 182 | * @param Config $config |
||
| 183 | * |
||
| 184 | * @return array|bool |
||
| 185 | */ |
||
| 186 | 1 | public function updateStream($path, $resource, Config $config) |
|
| 190 | |||
| 191 | /** |
||
| 192 | * @param string $path |
||
| 193 | * @param string $newpath |
||
| 194 | * |
||
| 195 | * @return bool |
||
| 196 | */ |
||
| 197 | 1 | public function rename($path, $newpath) |
|
| 198 | { |
||
| 199 | 1 | $result = $this->copy($path, $newpath); |
|
| 200 | |||
| 201 | 1 | $this->delete($path); |
|
| 202 | |||
| 203 | 1 | return $result; |
|
| 204 | } |
||
| 205 | |||
| 206 | /** |
||
| 207 | * @param string $path |
||
| 208 | * @param string $newpath |
||
| 209 | * |
||
| 210 | * @return bool |
||
| 211 | */ |
||
| 212 | 2 | public function copy($path, $newpath) |
|
| 218 | |||
| 219 | /** |
||
| 220 | * @param string $path |
||
| 221 | * |
||
| 222 | * @return bool |
||
| 223 | */ |
||
| 224 | 2 | public function delete($path) |
|
| 231 | |||
| 232 | /** |
||
| 233 | * @param string $dirname |
||
| 234 | * |
||
| 235 | * @return bool |
||
| 236 | */ |
||
| 237 | 1 | public function deleteDir($dirname) |
|
| 238 | { |
||
| 239 | 1 | $response = $this->listObjects($dirname); |
|
| 240 | |||
| 241 | 1 | if (!isset($response['Contents'])) { |
|
| 242 | return true; |
||
| 243 | } |
||
| 244 | |||
| 245 | 1 | $keys = array_map(function ($item) { |
|
| 246 | 1 | return ['Key' => $item['Key']]; |
|
| 247 | 1 | }, (array) $response['Contents']); |
|
| 248 | |||
| 249 | 1 | return (bool) $this->client->deleteObjects([ |
|
| 250 | 1 | 'Bucket' => $this->getBucket(), |
|
| 251 | 1 | 'Objects' => $keys, |
|
| 252 | 1 | ]); |
|
| 253 | } |
||
| 254 | |||
| 255 | /** |
||
| 256 | * @param string $dirname |
||
| 257 | * @param Config $config |
||
| 258 | * |
||
| 259 | * @return array|bool |
||
| 260 | */ |
||
| 261 | 1 | public function createDir($dirname, Config $config) |
|
| 269 | |||
| 270 | /** |
||
| 271 | * @param string $path |
||
| 272 | * @param string $visibility |
||
| 273 | * |
||
| 274 | * @return bool |
||
| 275 | */ |
||
| 276 | 1 | public function setVisibility($path, $visibility) |
|
| 287 | |||
| 288 | /** |
||
| 289 | * @param string $path |
||
| 290 | * |
||
| 291 | * @return bool |
||
| 292 | */ |
||
| 293 | 1 | public function has($path) |
|
| 294 | { |
||
| 295 | try { |
||
| 296 | 1 | return (bool) $this->getMetadata($path); |
|
| 297 | } catch (NoSuchKeyException $e) { |
||
| 298 | return false; |
||
| 299 | } |
||
| 300 | } |
||
| 301 | |||
| 302 | /** |
||
| 303 | * @param string $path |
||
| 304 | * |
||
| 305 | * @return array|bool |
||
| 306 | */ |
||
| 307 | 1 | public function read($path) |
|
| 320 | |||
| 321 | /** |
||
| 322 | * @param string $path |
||
| 323 | * |
||
| 324 | * @return array|bool |
||
| 325 | */ |
||
| 326 | 1 | public function readStream($path) |
|
| 334 | |||
| 335 | /** |
||
| 336 | * @param string $directory |
||
| 337 | * @param bool $recursive |
||
| 338 | * |
||
| 339 | * @return array|bool |
||
| 340 | */ |
||
| 341 | 1 | public function listContents($directory = '', $recursive = false) |
|
| 342 | { |
||
| 343 | 1 | $list = []; |
|
| 344 | |||
| 345 | 1 | $response = $this->listObjects($directory, $recursive); |
|
| 346 | |||
| 347 | 1 | foreach ((array) $response->get('Contents') as $content) { |
|
| 348 | 1 | $list[] = $this->normalizeFileInfo($content); |
|
| 349 | 1 | } |
|
| 350 | |||
| 351 | 1 | return $list; |
|
| 352 | } |
||
| 353 | |||
| 354 | /** |
||
| 355 | * @param string $path |
||
| 356 | * |
||
| 357 | * @return array|bool |
||
| 358 | */ |
||
| 359 | 5 | public function getMetadata($path) |
|
| 366 | |||
| 367 | /** |
||
| 368 | * @param string $path |
||
| 369 | * |
||
| 370 | * @return array|bool |
||
| 371 | */ |
||
| 372 | 1 | public function getSize($path) |
|
| 373 | { |
||
| 374 | 1 | $meta = $this->getMetadata($path); |
|
| 375 | |||
| 376 | 1 | return isset($meta['ContentLength']) |
|
| 377 | 1 | ? ['size' => $meta['ContentLength']] : false; |
|
| 378 | } |
||
| 379 | |||
| 380 | /** |
||
| 381 | * @param string $path |
||
| 382 | * |
||
| 383 | * @return array|bool |
||
| 384 | */ |
||
| 385 | 1 | public function getMimetype($path) |
|
| 386 | { |
||
| 387 | 1 | $meta = $this->getMetadata($path); |
|
| 388 | |||
| 389 | 1 | return isset($meta['ContentType']) |
|
| 390 | 1 | ? ['mimetype' => $meta['ContentType']] : false; |
|
| 391 | } |
||
| 392 | |||
| 393 | /** |
||
| 394 | * @param string $path |
||
| 395 | * |
||
| 396 | * @return array|bool |
||
| 397 | */ |
||
| 398 | 1 | public function getTimestamp($path) |
|
| 399 | { |
||
| 400 | 1 | $meta = $this->getMetadata($path); |
|
| 401 | |||
| 402 | 1 | return isset($meta['LastModified']) |
|
| 403 | 1 | ? ['timestamp' => strtotime($meta['LastModified'])] : false; |
|
| 404 | } |
||
| 405 | |||
| 406 | /** |
||
| 407 | * @param string $path |
||
| 408 | * |
||
| 409 | * @return array|bool |
||
| 410 | */ |
||
| 411 | 1 | public function getVisibility($path) |
|
| 412 | { |
||
| 413 | 1 | $meta = $this->client->getObjectAcl([ |
|
| 414 | 1 | 'Bucket' => $this->getBucket(), |
|
| 415 | 1 | 'Key' => $path, |
|
| 416 | 1 | ]); |
|
| 417 | |||
| 418 | 1 | foreach ($meta->get('Grants') as $grant) { |
|
| 419 | 1 | if (isset($grant['Grantee']['URI']) |
|
| 420 | 1 | && $grant['Permission'] === 'READ' |
|
| 421 | 1 | && strpos($grant['Grantee']['URI'], 'global/AllUsers') !== false |
|
| 422 | 1 | ) { |
|
| 423 | return ['visibility' => AdapterInterface::VISIBILITY_PUBLIC]; |
||
| 424 | } |
||
| 425 | 1 | } |
|
| 426 | |||
| 427 | 1 | return ['visibility' => AdapterInterface::VISIBILITY_PRIVATE]; |
|
| 428 | } |
||
| 429 | |||
| 430 | /** |
||
| 431 | * @param array $content |
||
| 432 | * |
||
| 433 | * @return array |
||
| 434 | */ |
||
| 435 | 1 | private function normalizeFileInfo(array $content) |
|
| 436 | { |
||
| 437 | 1 | $path = pathinfo($content['Key']); |
|
| 438 | |||
| 439 | return [ |
||
| 440 | 1 | 'type' => 'file', |
|
| 441 | 1 | 'path' => $content['Key'], |
|
| 442 | 1 | 'timestamp' => Carbon::parse($content['LastModified'])->getTimestamp(), |
|
| 443 | 1 | 'size' => (int) $content['Size'], |
|
| 444 | 1 | 'dirname' => (string) $path['dirname'], |
|
| 445 | 1 | 'basename' => (string) $path['basename'], |
|
| 446 | 1 | 'extension' => isset($path['extension']) ? $path['extension'] : '', |
|
| 447 | 1 | 'filename' => (string) $path['filename'], |
|
| 448 | 1 | ]; |
|
| 449 | } |
||
| 450 | |||
| 451 | /** |
||
| 452 | * @param string $directory |
||
| 453 | * @param bool $recursive |
||
| 454 | * |
||
| 455 | * @return mixed |
||
| 456 | */ |
||
| 457 | 2 | private function listObjects($directory = '', $recursive = false) |
|
| 465 | } |
||
| 466 |