Complex classes like BucketManager 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 BucketManager, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 15 | final class BucketManager |
||
| 16 | { |
||
| 17 | private $auth; |
||
| 18 | private $config; |
||
| 19 | |||
| 20 | 42 | public function __construct(Auth $auth, Config $config = null) |
|
| 29 | |||
| 30 | /** |
||
| 31 | * 获取指定账号下所有的空间名。 |
||
| 32 | * |
||
| 33 | * @return string[] 包含所有空间名 |
||
| 34 | */ |
||
| 35 | 3 | public function buckets($shared = true) |
|
| 43 | |||
| 44 | /** |
||
| 45 | * 获取指定空间绑定的所有的域名 |
||
| 46 | * |
||
| 47 | * @return string[] 包含所有空间域名 |
||
| 48 | */ |
||
| 49 | public function domains($bucket) |
||
| 53 | |||
| 54 | /** |
||
| 55 | * 获取空间绑定的域名列表 |
||
| 56 | * @return string[] 包含空间绑定的所有域名 |
||
| 57 | */ |
||
| 58 | |||
| 59 | /** |
||
| 60 | * 列取空间的文件列表 |
||
| 61 | * |
||
| 62 | * @param $bucket 空间名 |
||
| 63 | * @param $prefix 列举前缀 |
||
| 64 | * @param $marker 列举标识符 |
||
| 65 | * @param $limit 单次列举个数限制 |
||
| 66 | * @param $delimiter 指定目录分隔符 |
||
| 67 | * |
||
| 68 | * @return array 包含文件信息的数组,类似:[ |
||
| 69 | * { |
||
| 70 | * "hash" => "<Hash string>", |
||
| 71 | * "key" => "<Key string>", |
||
| 72 | * "fsize" => "<file size>", |
||
| 73 | * "putTime" => "<file modify time>" |
||
| 74 | * }, |
||
| 75 | * ... |
||
| 76 | * ] |
||
| 77 | * @link http://developer.qiniu.com/docs/v6/api/reference/rs/list.html |
||
| 78 | */ |
||
| 79 | 3 | public function listFiles($bucket, $prefix = null, $marker = null, $limit = 1000, $delimiter = null) |
|
| 89 | |||
| 90 | /** |
||
| 91 | * 获取资源的元信息,但不返回文件内容 |
||
| 92 | * |
||
| 93 | * @param $bucket 待获取信息资源所在的空间 |
||
| 94 | * @param $key 待获取资源的文件名 |
||
| 95 | * |
||
| 96 | * @return array 包含文件信息的数组,类似: |
||
| 97 | * [ |
||
| 98 | * "hash" => "<Hash string>", |
||
| 99 | * "key" => "<Key string>", |
||
| 100 | * "fsize" => <file size>, |
||
| 101 | * "putTime" => "<file modify time>" |
||
| 102 | * "fileType" => <file type> |
||
| 103 | * ] |
||
| 104 | * |
||
| 105 | * @link http://developer.qiniu.com/docs/v6/api/reference/rs/stat.html |
||
| 106 | */ |
||
| 107 | 3 | public function stat($bucket, $key) |
|
| 112 | |||
| 113 | /** |
||
| 114 | * 删除指定资源 |
||
| 115 | * |
||
| 116 | * @param $bucket 待删除资源所在的空间 |
||
| 117 | * @param $key 待删除资源的文件名 |
||
| 118 | * |
||
| 119 | * @return mixed 成功返回NULL,失败返回对象Qiniu\Http\Error |
||
| 120 | * @link http://developer.qiniu.com/docs/v6/api/reference/rs/delete.html |
||
| 121 | */ |
||
| 122 | 6 | public function delete($bucket, $key) |
|
| 128 | |||
| 129 | |||
| 130 | /** |
||
| 131 | * 给资源进行重命名,本质为move操作。 |
||
| 132 | * |
||
| 133 | * @param $bucket 待操作资源所在空间 |
||
| 134 | * @param $oldname 待操作资源文件名 |
||
| 135 | * @param $newname 目标资源文件名 |
||
| 136 | * |
||
| 137 | * @return mixed 成功返回NULL,失败返回对象Qiniu\Http\Error |
||
| 138 | */ |
||
| 139 | 3 | public function rename($bucket, $oldname, $newname) |
|
| 143 | |||
| 144 | /** |
||
| 145 | * 给资源进行重命名,本质为move操作。 |
||
| 146 | * |
||
| 147 | * @param $from_bucket 待操作资源所在空间 |
||
| 148 | * @param $from_key 待操作资源文件名 |
||
| 149 | * @param $to_bucket 目标资源空间名 |
||
| 150 | * @param $to_key 目标资源文件名 |
||
| 151 | * |
||
| 152 | * @return mixed 成功返回NULL,失败返回对象Qiniu\Http\Error |
||
| 153 | * @link http://developer.qiniu.com/docs/v6/api/reference/rs/copy.html |
||
| 154 | */ |
||
| 155 | 12 | public function copy($from_bucket, $from_key, $to_bucket, $to_key, $force = false) |
|
| 166 | |||
| 167 | /** |
||
| 168 | * 将资源从一个空间到另一个空间 |
||
| 169 | * |
||
| 170 | * @param $from_bucket 待操作资源所在空间 |
||
| 171 | * @param $from_key 待操作资源文件名 |
||
| 172 | * @param $to_bucket 目标资源空间名 |
||
| 173 | * @param $to_key 目标资源文件名 |
||
| 174 | * |
||
| 175 | * @return mixed 成功返回NULL,失败返回对象Qiniu\Http\Error |
||
| 176 | * @link http://developer.qiniu.com/docs/v6/api/reference/rs/move.html |
||
| 177 | */ |
||
| 178 | 3 | public function move($from_bucket, $from_key, $to_bucket, $to_key, $force = false) |
|
| 189 | |||
| 190 | /** |
||
| 191 | * 主动修改指定资源的文件类型 |
||
| 192 | * |
||
| 193 | * @param $bucket 待操作资源所在空间 |
||
| 194 | * @param $key 待操作资源文件名 |
||
| 195 | * @param $mime 待操作文件目标mimeType |
||
| 196 | * |
||
| 197 | * @return mixed 成功返回NULL,失败返回对象Qiniu\Http\Error |
||
| 198 | * @link http://developer.qiniu.com/docs/v6/api/reference/rs/chgm.html |
||
| 199 | */ |
||
| 200 | 3 | public function changeMime($bucket, $key, $mime) |
|
| 208 | |||
| 209 | |||
| 210 | /** |
||
| 211 | * 修改指定资源的存储类型 |
||
| 212 | * |
||
| 213 | * @param $bucket 待操作资源所在空间 |
||
| 214 | * @param $key 待操作资源文件名 |
||
| 215 | * @param $fileType 待操作文件目标文件类型 |
||
| 216 | * |
||
| 217 | * @return mixed 成功返回NULL,失败返回对象Qiniu\Http\Error |
||
| 218 | * @link https://developer.qiniu.com/kodo/api/3710/modify-the-file-type |
||
| 219 | */ |
||
| 220 | public function changeType($bucket, $key, $fileType) |
||
| 227 | |||
| 228 | /** |
||
| 229 | * 修改文件的存储状态,即禁用状态和启用状态间的的互相转换 |
||
| 230 | * |
||
| 231 | * @param $bucket 待操作资源所在空间 |
||
| 232 | * @param $key 待操作资源文件名 |
||
| 233 | * @param $status 待操作文件目标文件类型 |
||
| 234 | * |
||
| 235 | * @return mixed 成功返回NULL,失败返回对象Qiniu\Http\Error |
||
| 236 | * @link https://developer.qiniu.com/kodo/api/4173/modify-the-file-status |
||
| 237 | */ |
||
| 238 | public function changeStatus($bucket, $key, $status) |
||
| 245 | |||
| 246 | /** |
||
| 247 | * 从指定URL抓取资源,并将该资源存储到指定空间中 |
||
| 248 | * |
||
| 249 | * @param $url 指定的URL |
||
| 250 | * @param $bucket 目标资源空间 |
||
| 251 | * @param $key 目标资源文件名 |
||
| 252 | * |
||
| 253 | * @return array 包含已拉取的文件信息。 |
||
| 254 | * 成功时: [ |
||
| 255 | * [ |
||
| 256 | * "hash" => "<Hash string>", |
||
| 257 | * "key" => "<Key string>" |
||
| 258 | * ], |
||
| 259 | * null |
||
| 260 | * ] |
||
| 261 | * |
||
| 262 | * 失败时: [ |
||
| 263 | * null, |
||
| 264 | * Qiniu/Http/Error |
||
| 265 | * ] |
||
| 266 | * @link http://developer.qiniu.com/docs/v6/api/reference/rs/fetch.html |
||
| 267 | */ |
||
| 268 | 3 | public function fetch($url, $bucket, $key = null) |
|
| 281 | |||
| 282 | /** |
||
| 283 | * 从镜像源站抓取资源到空间中,如果空间中已经存在,则覆盖该资源 |
||
| 284 | * |
||
| 285 | * @param $bucket 待获取资源所在的空间 |
||
| 286 | * @param $key 代获取资源文件名 |
||
| 287 | * |
||
| 288 | * @return mixed 成功返回NULL,失败返回对象Qiniu\Http\Error |
||
| 289 | * @link http://developer.qiniu.com/docs/v6/api/reference/rs/prefetch.html |
||
| 290 | */ |
||
| 291 | 3 | public function prefetch($bucket, $key) |
|
| 303 | |||
| 304 | /** |
||
| 305 | * 在单次请求中进行多个资源管理操作 |
||
| 306 | * |
||
| 307 | * @param $operations 资源管理操作数组 |
||
| 308 | * |
||
| 309 | * @return array 每个资源的处理情况,结果类似: |
||
| 310 | * [ |
||
| 311 | * { "code" => <HttpCode int>, "data" => <Data> }, |
||
| 312 | * { "code" => <HttpCode int> }, |
||
| 313 | * { "code" => <HttpCode int> }, |
||
| 314 | * { "code" => <HttpCode int> }, |
||
| 315 | * { "code" => <HttpCode int>, "data" => { "error": "<ErrorMessage string>" } }, |
||
| 316 | * ... |
||
| 317 | * ] |
||
| 318 | * @link http://developer.qiniu.com/docs/v6/api/reference/rs/batch.html |
||
| 319 | */ |
||
| 320 | 12 | public function batch($operations) |
|
| 325 | |||
| 326 | /** |
||
| 327 | * 设置文件的生命周期 |
||
| 328 | * |
||
| 329 | * @param $bucket 设置文件生命周期文件所在的空间 |
||
| 330 | * @param $key 设置文件生命周期文件的文件名 |
||
| 331 | * @param $days 设置该文件多少天后删除,当$days设置为0时表示取消该文件的生命周期 |
||
| 332 | * |
||
| 333 | * @return Mixed |
||
| 334 | * @link https://developer.qiniu.com/kodo/api/update-file-lifecycle |
||
| 335 | */ |
||
| 336 | 3 | public function deleteAfterDays($bucket, $key, $days) |
|
| 343 | |||
| 344 | 3 | private function getRsfHost() |
|
| 352 | |||
| 353 | 33 | private function getRsHost() |
|
| 361 | |||
| 362 | private function getApiHost() |
||
| 370 | |||
| 371 | 27 | private function rsPost($path, $body = null) |
|
| 376 | |||
| 377 | private function apiGet($path) |
||
| 382 | |||
| 383 | 6 | private function rsGet($path) |
|
| 388 | |||
| 389 | 9 | private function get($url) |
|
| 398 | |||
| 399 | 27 | private function post($url, $body) |
|
| 409 | |||
| 410 | 3 | public static function buildBatchCopy($source_bucket, $key_pairs, $target_bucket, $force) |
|
| 414 | |||
| 415 | |||
| 416 | 3 | public static function buildBatchRename($bucket, $key_pairs, $force) |
|
| 420 | |||
| 421 | |||
| 422 | 6 | public static function buildBatchMove($source_bucket, $key_pairs, $target_bucket, $force) |
|
| 426 | |||
| 427 | |||
| 428 | public static function buildBatchDelete($bucket, $keys) |
||
| 432 | |||
| 433 | |||
| 434 | 3 | public static function buildBatchStat($bucket, $keys) |
|
| 438 | |||
| 439 | public static function buildBatchDeleteAfterDays($bucket, $key_day_pairs) |
||
| 447 | |||
| 448 | public static function buildBatchChangeMime($bucket, $key_mime_pairs) |
||
| 456 | |||
| 457 | public static function buildBatchChangeType($bucket, $key_type_pairs) |
||
| 465 | |||
| 466 | 3 | private static function oneKeyBatch($operation, $bucket, $keys) |
|
| 474 | |||
| 475 | 9 | private static function twoKeyBatch($operation, $source_bucket, $key_pairs, $target_bucket, $force) |
|
| 492 | } |
||
| 493 |
When comparing two booleans, it is generally considered safer to use the strict comparison operator.