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 |
||
| 17 | class Adapter extends AbstractAdapter implements CanOverwriteFiles |
||
| 18 | { |
||
| 19 | /** |
||
| 20 | * @var Client |
||
| 21 | */ |
||
| 22 | protected $client; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @var array |
||
| 26 | */ |
||
| 27 | protected $config = []; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @var array |
||
| 31 | */ |
||
| 32 | protected $regionMap = [ |
||
| 33 | 'cn-east' => 'ap-shanghai', |
||
| 34 | 'cn-sorth' => 'ap-guangzhou', |
||
| 35 | 'cn-north' => 'ap-beijing-1', |
||
| 36 | 'cn-south-2' => 'ap-guangzhou-2', |
||
| 37 | 'cn-southwest' => 'ap-chengdu', |
||
| 38 | 'sg' => 'ap-singapore', |
||
| 39 | 'tj' => 'ap-beijing-1', |
||
| 40 | 'bj' => 'ap-beijing', |
||
| 41 | 'sh' => 'ap-shanghai', |
||
| 42 | 'gz' => 'ap-guangzhou', |
||
| 43 | 'cd' => 'ap-chengdu', |
||
| 44 | 'sgp' => 'ap-singapore', |
||
| 45 | ]; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Adapter constructor. |
||
| 49 | * |
||
| 50 | * @param Client $client |
||
| 51 | * @param array $config |
||
| 52 | */ |
||
| 53 | public function __construct(Client $client, array $config) |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @return string |
||
| 63 | */ |
||
| 64 | 19 | public function getBucket() |
|
| 65 | 1 | { |
|
| 66 | 19 | return $this->config['bucket']; |
|
| 67 | } |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @return string |
||
| 71 | */ |
||
| 72 | 2 | public function getAppId() |
|
| 76 | |||
| 77 | /** |
||
| 78 | * @return string |
||
| 79 | */ |
||
| 80 | 2 | public function getRegion() |
|
| 85 | |||
| 86 | /** |
||
| 87 | * @param $path |
||
| 88 | * |
||
| 89 | * @return string |
||
| 90 | */ |
||
| 91 | 2 | public function getSourcePath($path) |
|
| 97 | |||
| 98 | /** |
||
| 99 | * @param string $path |
||
| 100 | * |
||
| 101 | * @return string |
||
| 102 | */ |
||
| 103 | 4 | public function getUrl($path) |
|
| 118 | |||
| 119 | /** |
||
| 120 | * @param string $path |
||
| 121 | * @param \DateTimeInterface $expiration |
||
| 122 | * @param array $options |
||
| 123 | * |
||
| 124 | * @return string |
||
| 125 | */ |
||
| 126 | 1 | public function getTemporaryUrl($path, DateTimeInterface $expiration, array $options = []) |
|
| 127 | { |
||
| 128 | 1 | $options = array_merge($options, |
|
| 129 | 1 | ['Scheme' => isset($this->config['scheme']) ? $this->config['scheme'] : 'http']); |
|
| 130 | |||
| 131 | 1 | $objectUrl = $this->client->getObjectUrl( |
|
| 132 | 1 | $this->getBucket(), $path, $expiration->format('c'), $options |
|
| 133 | 1 | ); |
|
| 134 | |||
| 135 | 1 | $url = parse_url($objectUrl); |
|
| 136 | |||
| 137 | 1 | if ($this->config['cdn']) { |
|
| 138 | 1 | return $this->config['cdn'].urldecode($url['path']).'?'.$url['query']; |
|
| 139 | } |
||
| 140 | |||
| 141 | return sprintf('%s://%s%s?%s', $url['scheme'], $url['host'], urldecode($url['path']), $url['query']); |
||
| 142 | } |
||
| 143 | |||
| 144 | /** |
||
| 145 | * @param string $path |
||
| 146 | * @param string $contents |
||
| 147 | * @param Config $config |
||
| 148 | * |
||
| 149 | * @return array|bool |
||
| 150 | */ |
||
| 151 | 2 | public function write($path, $contents, Config $config) |
|
| 155 | |||
| 156 | /** |
||
| 157 | * @param string $path |
||
| 158 | * @param resource $resource |
||
| 159 | * @param Config $config |
||
| 160 | * |
||
| 161 | * @return array|bool |
||
| 162 | */ |
||
| 163 | 2 | public function writeStream($path, $resource, Config $config) |
|
| 167 | |||
| 168 | /** |
||
| 169 | * @param string $path |
||
| 170 | * @param string $contents |
||
| 171 | * @param Config $config |
||
| 172 | * |
||
| 173 | * @return array|bool |
||
| 174 | */ |
||
| 175 | 2 | public function update($path, $contents, Config $config) |
|
| 179 | |||
| 180 | /** |
||
| 181 | * @param string $path |
||
| 182 | * @param resource $resource |
||
| 183 | * @param Config $config |
||
| 184 | * |
||
| 185 | * @return array|bool |
||
| 186 | */ |
||
| 187 | 1 | public function updateStream($path, $resource, Config $config) |
|
| 191 | |||
| 192 | /** |
||
| 193 | * @param string $path |
||
| 194 | * @param string $newpath |
||
| 195 | * |
||
| 196 | * @return bool |
||
| 197 | */ |
||
| 198 | 1 | public function rename($path, $newpath) |
|
| 206 | |||
| 207 | /** |
||
| 208 | * @param string $path |
||
| 209 | * @param string $newpath |
||
| 210 | * |
||
| 211 | * @return bool |
||
| 212 | */ |
||
| 213 | 2 | public function copy($path, $newpath) |
|
| 219 | |||
| 220 | /** |
||
| 221 | * @param string $path |
||
| 222 | * |
||
| 223 | * @return bool |
||
| 224 | */ |
||
| 225 | 2 | public function delete($path) |
|
| 232 | |||
| 233 | /** |
||
| 234 | * @param string $dirname |
||
| 235 | * |
||
| 236 | * @return bool |
||
| 237 | */ |
||
| 238 | 1 | public function deleteDir($dirname) |
|
| 255 | |||
| 256 | /** |
||
| 257 | * @param string $dirname |
||
| 258 | * @param Config $config |
||
| 259 | * |
||
| 260 | * @return array|bool |
||
| 261 | */ |
||
| 262 | 1 | public function createDir($dirname, Config $config) |
|
| 270 | |||
| 271 | /** |
||
| 272 | * @param string $path |
||
| 273 | * @param string $visibility |
||
| 274 | * |
||
| 275 | * @return bool |
||
| 276 | */ |
||
| 277 | 1 | public function setVisibility($path, $visibility) |
|
| 288 | |||
| 289 | /** |
||
| 290 | * @param string $path |
||
| 291 | * |
||
| 292 | * @return bool |
||
| 293 | */ |
||
| 294 | 1 | public function has($path) |
|
| 302 | |||
| 303 | /** |
||
| 304 | * @param string $path |
||
| 305 | * |
||
| 306 | * @return array|bool |
||
| 307 | */ |
||
| 308 | 1 | public function read($path) |
|
| 330 | |||
| 331 | /** |
||
| 332 | * @return \GuzzleHttp\Client |
||
| 333 | */ |
||
| 334 | 1 | protected function getHttpClient() |
|
| 342 | |||
| 343 | /** |
||
| 344 | * @param string $path |
||
| 345 | * |
||
| 346 | * @return array|bool |
||
| 347 | */ |
||
| 348 | 1 | public function readStream($path) |
|
| 365 | |||
| 366 | /** |
||
| 367 | * @param string $directory |
||
| 368 | * @param bool $recursive |
||
| 369 | * |
||
| 370 | * @return array|bool |
||
| 371 | */ |
||
| 372 | 1 | public function listContents($directory = '', $recursive = false) |
|
| 384 | |||
| 385 | /** |
||
| 386 | * @param string $path |
||
| 387 | * |
||
| 388 | * @return array|bool |
||
| 389 | */ |
||
| 390 | 5 | public function getMetadata($path) |
|
| 397 | |||
| 398 | /** |
||
| 399 | * @param string $path |
||
| 400 | * |
||
| 401 | * @return array|bool |
||
| 402 | */ |
||
| 403 | 1 | public function getSize($path) |
|
| 410 | |||
| 411 | /** |
||
| 412 | * @param string $path |
||
| 413 | * |
||
| 414 | * @return array|bool |
||
| 415 | */ |
||
| 416 | 1 | public function getMimetype($path) |
|
| 423 | |||
| 424 | /** |
||
| 425 | * @param string $path |
||
| 426 | * |
||
| 427 | * @return array|bool |
||
| 428 | */ |
||
| 429 | 1 | public function getTimestamp($path) |
|
| 436 | |||
| 437 | /** |
||
| 438 | * @param string $path |
||
| 439 | * |
||
| 440 | * @return array|bool |
||
| 441 | */ |
||
| 442 | 1 | public function getVisibility($path) |
|
| 460 | |||
| 461 | /** |
||
| 462 | * @param array $content |
||
| 463 | * |
||
| 464 | * @return array |
||
| 465 | */ |
||
| 466 | 1 | private function normalizeFileInfo(array $content) |
|
| 481 | |||
| 482 | /** |
||
| 483 | * @param string $directory |
||
| 484 | * @param bool $recursive |
||
| 485 | * |
||
| 486 | * @return mixed |
||
| 487 | */ |
||
| 488 | 2 | private function listObjects($directory = '', $recursive = false) |
|
| 496 | } |
||
| 497 |