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 | * @param $name 创建的空间名 |
||
| 48 | * @param $region 创建的区域,默认华东 |
||
| 49 | * |
||
| 50 | * @return mixed 成功返回NULL,失败返回对象Qiniu\Http\Error |
||
| 51 | */ |
||
| 52 | public function creatBucket($name, $region='z0') |
||
| 57 | |||
| 58 | /** |
||
| 59 | * 删除空间 |
||
| 60 | * |
||
| 61 | * @param $name 删除的空间名 |
||
| 62 | * |
||
| 63 | * @return mixed 成功返回NULL,失败返回对象Qiniu\Http\Error |
||
| 64 | */ |
||
| 65 | public function deleteBucket($name) |
||
| 70 | |||
| 71 | /** |
||
| 72 | * 获取指定空间绑定的所有的域名 |
||
| 73 | * |
||
| 74 | * @return string[] 包含所有空间域名 |
||
| 75 | */ |
||
| 76 | public function domains($bucket) |
||
| 80 | |||
| 81 | 3 | /** |
|
| 82 | 3 | * 获取空间绑定的域名列表 |
|
| 83 | 3 | * @return string[] 包含空间绑定的所有域名 |
|
| 84 | 3 | */ |
|
| 85 | 3 | ||
| 86 | 3 | /** |
|
| 87 | 3 | * 列取空间的文件列表 |
|
| 88 | * |
||
| 89 | * @param $bucket 空间名 |
||
| 90 | * @param $prefix 列举前缀 |
||
| 91 | * @param $marker 列举标识符 |
||
| 92 | * @param $limit 单次列举个数限制 |
||
| 93 | * @param $delimiter 指定目录分隔符 |
||
| 94 | * |
||
| 95 | * @return array 包含文件信息的数组,类似:[ |
||
| 96 | * { |
||
| 97 | * "hash" => "<Hash string>", |
||
| 98 | * "key" => "<Key string>", |
||
| 99 | * "fsize" => "<file size>", |
||
| 100 | * "putTime" => "<file modify time>" |
||
| 101 | * }, |
||
| 102 | * ... |
||
| 103 | * ] |
||
| 104 | * @link http://developer.qiniu.com/docs/v6/api/reference/rs/list.html |
||
| 105 | */ |
||
| 106 | public function listFiles($bucket, $prefix = null, $marker = null, $limit = 1000, $delimiter = null) |
||
| 116 | |||
| 117 | /** |
||
| 118 | * 获取资源的元信息,但不返回文件内容 |
||
| 119 | * |
||
| 120 | * @param $bucket 待获取信息资源所在的空间 |
||
| 121 | * @param $key 待获取资源的文件名 |
||
| 122 | 15 | * |
|
| 123 | * @return array 包含文件信息的数组,类似: |
||
| 124 | 15 | * [ |
|
| 125 | 15 | * "hash" => "<Hash string>", |
|
| 126 | 15 | * "key" => "<Key string>", |
|
| 127 | * "fsize" => <file size>, |
||
| 128 | * "putTime" => "<file modify time>" |
||
| 129 | * "fileType" => <file type> |
||
| 130 | * ] |
||
| 131 | * |
||
| 132 | * @link http://developer.qiniu.com/docs/v6/api/reference/rs/stat.html |
||
| 133 | */ |
||
| 134 | public function stat($bucket, $key) |
||
| 139 | 3 | ||
| 140 | /** |
||
| 141 | 3 | * 删除指定资源 |
|
| 142 | * |
||
| 143 | * @param $bucket 待删除资源所在的空间 |
||
| 144 | * @param $key 待删除资源的文件名 |
||
| 145 | * |
||
| 146 | * @return mixed 成功返回NULL,失败返回对象Qiniu\Http\Error |
||
| 147 | * @link http://developer.qiniu.com/docs/v6/api/reference/rs/delete.html |
||
| 148 | */ |
||
| 149 | public function delete($bucket, $key) |
||
| 155 | 15 | ||
| 156 | |||
| 157 | 15 | /** |
|
| 158 | 15 | * 给资源进行重命名,本质为move操作。 |
|
| 159 | 15 | * |
|
| 160 | 15 | * @param $bucket 待操作资源所在空间 |
|
| 161 | 3 | * @param $oldname 待操作资源文件名 |
|
| 162 | 3 | * @param $newname 目标资源文件名 |
|
| 163 | 15 | * |
|
| 164 | 15 | * @return mixed 成功返回NULL,失败返回对象Qiniu\Http\Error |
|
| 165 | */ |
||
| 166 | public function rename($bucket, $oldname, $newname) |
||
| 170 | |||
| 171 | /** |
||
| 172 | * 给资源进行重命名,本质为move操作。 |
||
| 173 | * |
||
| 174 | * @param $from_bucket 待操作资源所在空间 |
||
| 175 | * @param $from_key 待操作资源文件名 |
||
| 176 | * @param $to_bucket 目标资源空间名 |
||
| 177 | * @param $to_key 目标资源文件名 |
||
| 178 | 3 | * |
|
| 179 | * @return mixed 成功返回NULL,失败返回对象Qiniu\Http\Error |
||
| 180 | 3 | * @link http://developer.qiniu.com/docs/v6/api/reference/rs/copy.html |
|
| 181 | 3 | */ |
|
| 182 | 3 | public function copy($from_bucket, $from_key, $to_bucket, $to_key, $force = false) |
|
| 193 | |||
| 194 | /** |
||
| 195 | * 将资源从一个空间到另一个空间 |
||
| 196 | * |
||
| 197 | * @param $from_bucket 待操作资源所在空间 |
||
| 198 | * @param $from_key 待操作资源文件名 |
||
| 199 | * @param $to_bucket 目标资源空间名 |
||
| 200 | 3 | * @param $to_key 目标资源文件名 |
|
| 201 | * |
||
| 202 | 3 | * @return mixed 成功返回NULL,失败返回对象Qiniu\Http\Error |
|
| 203 | 3 | * @link http://developer.qiniu.com/docs/v6/api/reference/rs/move.html |
|
| 204 | 3 | */ |
|
| 205 | 3 | public function move($from_bucket, $from_key, $to_bucket, $to_key, $force = false) |
|
| 216 | |||
| 217 | /** |
||
| 218 | * 主动修改指定资源的文件元信息 |
||
| 219 | * |
||
| 220 | * @param $bucket 待操作资源所在空间 |
||
| 221 | * @param $key 待操作资源文件名 |
||
| 222 | * @param $mime 待操作文件目标mimeType |
||
| 223 | * |
||
| 224 | * @return mixed 成功返回NULL,失败返回对象Qiniu\Http\Error |
||
| 225 | * @link http://developer.qiniu.com/docs/v6/api/reference/rs/chgm.html |
||
| 226 | */ |
||
| 227 | public function changeMime($bucket, $key, $mime) |
||
| 235 | |||
| 236 | |||
| 237 | /** |
||
| 238 | * 修改指定资源的存储类型 |
||
| 239 | * |
||
| 240 | * @param $bucket 待操作资源所在空间 |
||
| 241 | * @param $key 待操作资源文件名 |
||
| 242 | * @param $fileType 待操作文件目标文件类型 |
||
| 243 | * |
||
| 244 | * @return mixed 成功返回NULL,失败返回对象Qiniu\Http\Error |
||
| 245 | * @link https://developer.qiniu.com/kodo/api/3710/modify-the-file-type |
||
| 246 | */ |
||
| 247 | public function changeType($bucket, $key, $fileType) |
||
| 254 | |||
| 255 | /** |
||
| 256 | * 修改文件的存储状态,即禁用状态和启用状态间的的互相转换 |
||
| 257 | * |
||
| 258 | * @param $bucket 待操作资源所在空间 |
||
| 259 | * @param $key 待操作资源文件名 |
||
| 260 | * @param $status 待操作文件目标文件类型 |
||
| 261 | * |
||
| 262 | * @return mixed 成功返回NULL,失败返回对象Qiniu\Http\Error |
||
| 263 | * @link https://developer.qiniu.com/kodo/api/4173/modify-the-file-status |
||
| 264 | */ |
||
| 265 | public function changeStatus($bucket, $key, $status) |
||
| 272 | 3 | ||
| 273 | 3 | /** |
|
| 274 | * 从指定URL抓取资源,并将该资源存储到指定空间中 |
||
| 275 | 3 | * |
|
| 276 | 3 | * @param $url 指定的URL |
|
| 277 | * @param $bucket 目标资源空间 |
||
| 278 | 3 | * @param $key 目标资源文件名 |
|
| 279 | 3 | * |
|
| 280 | * @return array 包含已拉取的文件信息。 |
||
| 281 | * 成功时: [ |
||
| 282 | * [ |
||
| 283 | * "hash" => "<Hash string>", |
||
| 284 | * "key" => "<Key string>" |
||
| 285 | * ], |
||
| 286 | * null |
||
| 287 | * ] |
||
| 288 | * |
||
| 289 | * 失败时: [ |
||
| 290 | * null, |
||
| 291 | 3 | * Qiniu/Http/Error |
|
| 292 | * ] |
||
| 293 | 3 | * @link http://developer.qiniu.com/docs/v6/api/reference/rs/fetch.html |
|
| 294 | 3 | */ |
|
| 295 | public function fetch($url, $bucket, $key = null) |
||
| 308 | |||
| 309 | /** |
||
| 310 | * 从镜像源站抓取资源到空间中,如果空间中已经存在,则覆盖该资源 |
||
| 311 | * |
||
| 312 | * @param $bucket 待获取资源所在的空间 |
||
| 313 | * @param $key 代获取资源文件名 |
||
| 314 | * |
||
| 315 | * @return mixed 成功返回NULL,失败返回对象Qiniu\Http\Error |
||
| 316 | * @link http://developer.qiniu.com/docs/v6/api/reference/rs/prefetch.html |
||
| 317 | */ |
||
| 318 | public function prefetch($bucket, $key) |
||
| 330 | |||
| 331 | /** |
||
| 332 | * 在单次请求中进行多个资源管理操作 |
||
| 333 | * |
||
| 334 | * @param $operations 资源管理操作数组 |
||
| 335 | * |
||
| 336 | 3 | * @return array 每个资源的处理情况,结果类似: |
|
| 337 | * [ |
||
| 338 | 3 | * { "code" => <HttpCode int>, "data" => <Data> }, |
|
| 339 | 3 | * { "code" => <HttpCode int> }, |
|
| 340 | 3 | * { "code" => <HttpCode int> }, |
|
| 341 | 3 | * { "code" => <HttpCode int> }, |
|
| 342 | * { "code" => <HttpCode int>, "data" => { "error": "<ErrorMessage string>" } }, |
||
| 343 | * ... |
||
| 344 | 3 | * ] |
|
| 345 | * @link http://developer.qiniu.com/docs/v6/api/reference/rs/batch.html |
||
| 346 | 3 | */ |
|
| 347 | 3 | public function batch($operations) |
|
| 352 | |||
| 353 | 33 | /** |
|
| 354 | * 设置文件的生命周期 |
||
| 355 | 33 | * |
|
| 356 | 33 | * @param $bucket 设置文件生命周期文件所在的空间 |
|
| 357 | * @param $key 设置文件生命周期文件的文件名 |
||
| 358 | * @param $days 设置该文件多少天后删除,当$days设置为0时表示取消该文件的生命周期 |
||
| 359 | 33 | * |
|
| 360 | * @return Mixed |
||
| 361 | * @link https://developer.qiniu.com/kodo/api/update-file-lifecycle |
||
| 362 | */ |
||
| 363 | public function deleteAfterDays($bucket, $key, $days) |
||
| 370 | |||
| 371 | 27 | private function getRsfHost() |
|
| 379 | |||
| 380 | private function getRsHost() |
||
| 388 | |||
| 389 | 12 | private function getApiHost() |
|
| 397 | |||
| 398 | private function rsPost($path, $body = null) |
||
| 403 | 33 | ||
| 404 | 9 | private function apiPost($path, $body = null) |
|
| 409 | |||
| 410 | 3 | private function apiGet($path) |
|
| 415 | |||
| 416 | 3 | private function rsGet($path) |
|
| 421 | |||
| 422 | 6 | private function get($url) |
|
| 431 | |||
| 432 | private function post($url, $body) |
||
| 442 | |||
| 443 | public static function buildBatchCopy($source_bucket, $key_pairs, $target_bucket, $force) |
||
| 447 | |||
| 448 | |||
| 449 | public static function buildBatchRename($bucket, $key_pairs, $force) |
||
| 453 | |||
| 454 | |||
| 455 | public static function buildBatchMove($source_bucket, $key_pairs, $target_bucket, $force) |
||
| 459 | |||
| 460 | |||
| 461 | public static function buildBatchDelete($bucket, $keys) |
||
| 465 | |||
| 466 | 6 | ||
| 467 | public static function buildBatchStat($bucket, $keys) |
||
| 471 | 6 | ||
| 472 | 6 | public static function buildBatchDeleteAfterDays($bucket, $key_day_pairs) |
|
| 480 | 9 | ||
| 481 | 9 | public static function buildBatchChangeMime($bucket, $key_mime_pairs) |
|
| 489 | 9 | ||
| 490 | 9 | public static function buildBatchChangeType($bucket, $key_type_pairs) |
|
| 498 | |||
| 499 | private static function oneKeyBatch($operation, $bucket, $keys) |
||
| 507 | |||
| 508 | private static function twoKeyBatch($operation, $source_bucket, $key_pairs, $target_bucket, $force) |
||
| 525 | } |
||
| 526 |
When comparing two booleans, it is generally considered safer to use the strict comparison operator.