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 | * |
|
| 84 | 3 | * @return string[] 包含空间信息 |
|
| 85 | 3 | */ |
|
| 86 | 3 | public function bucketInfo($bucket){ |
|
| 91 | |||
| 92 | /** |
||
| 93 | * 获取指定zone的空间信息列表 |
||
| 94 | * 在Region 未指定且Global 不为 true 时(包含未指定的情况,下同),返回用户的所有空间。 |
||
| 95 | * 在指定了 region 参数且 global 不为 true 时,只列举非全局空间。 |
||
| 96 | * 在指定了global为 true 时,返回所有全局空间,忽略region 参数 |
||
| 97 | * shared 不指定shared参数或指定shared为rw或false时,返回包含具有读写权限空间,指定shared为rd或true时,返回包含具有读权限空间。 |
||
| 98 | * fs:如果为 true,会返回每个空间当前的文件数和存储量(实时数据)。 |
||
| 99 | * |
||
| 100 | * @return string[] 包含空间信息 |
||
| 101 | */ |
||
| 102 | public function bucketInfos($region=null, $global='false', $shared='false', $fs='false'){ |
||
| 107 | 6 | ||
| 108 | /** |
||
| 109 | 6 | * 获取空间绑定的域名列表 |
|
| 110 | 6 | * @return string[] 包含空间绑定的所有域名 |
|
| 111 | */ |
||
| 112 | |||
| 113 | /** |
||
| 114 | * 列取空间的文件列表 |
||
| 115 | * |
||
| 116 | * @param $bucket 空间名 |
||
| 117 | * @param $prefix 列举前缀 |
||
| 118 | * @param $marker 列举标识符 |
||
| 119 | * @param $limit 单次列举个数限制 |
||
| 120 | * @param $delimiter 指定目录分隔符 |
||
| 121 | * |
||
| 122 | 15 | * @return array 包含文件信息的数组,类似:[ |
|
| 123 | * { |
||
| 124 | 15 | * "hash" => "<Hash string>", |
|
| 125 | 15 | * "key" => "<Key string>", |
|
| 126 | 15 | * "fsize" => "<file size>", |
|
| 127 | * "putTime" => "<file modify time>" |
||
| 128 | * }, |
||
| 129 | * ... |
||
| 130 | * ] |
||
| 131 | * @link http://developer.qiniu.com/docs/v6/api/reference/rs/list.html |
||
| 132 | */ |
||
| 133 | public function listFiles($bucket, $prefix = null, $marker = null, $limit = 1000, $delimiter = null) |
||
| 143 | |||
| 144 | /** |
||
| 145 | * 获取资源的元信息,但不返回文件内容 |
||
| 146 | * |
||
| 147 | * @param $bucket 待获取信息资源所在的空间 |
||
| 148 | * @param $key 待获取资源的文件名 |
||
| 149 | * |
||
| 150 | * @return array 包含文件信息的数组,类似: |
||
| 151 | * [ |
||
| 152 | * "hash" => "<Hash string>", |
||
| 153 | * "key" => "<Key string>", |
||
| 154 | * "fsize" => <file size>, |
||
| 155 | 15 | * "putTime" => "<file modify time>" |
|
| 156 | * "fileType" => <file type> |
||
| 157 | 15 | * ] |
|
| 158 | 15 | * |
|
| 159 | 15 | * @link http://developer.qiniu.com/docs/v6/api/reference/rs/stat.html |
|
| 160 | 15 | */ |
|
| 161 | 3 | public function stat($bucket, $key) |
|
| 166 | |||
| 167 | /** |
||
| 168 | * 删除指定资源 |
||
| 169 | * |
||
| 170 | * @param $bucket 待删除资源所在的空间 |
||
| 171 | * @param $key 待删除资源的文件名 |
||
| 172 | * |
||
| 173 | * @return mixed 成功返回NULL,失败返回对象Qiniu\Http\Error |
||
| 174 | * @link http://developer.qiniu.com/docs/v6/api/reference/rs/delete.html |
||
| 175 | */ |
||
| 176 | public function delete($bucket, $key) |
||
| 182 | 3 | ||
| 183 | 3 | ||
| 184 | /** |
||
| 185 | * 给资源进行重命名,本质为move操作。 |
||
| 186 | 3 | * |
|
| 187 | 3 | * @param $bucket 待操作资源所在空间 |
|
| 188 | * @param $oldname 待操作资源文件名 |
||
| 189 | * @param $newname 目标资源文件名 |
||
| 190 | * |
||
| 191 | * @return mixed 成功返回NULL,失败返回对象Qiniu\Http\Error |
||
| 192 | */ |
||
| 193 | public function rename($bucket, $oldname, $newname) |
||
| 197 | |||
| 198 | /** |
||
| 199 | * 给资源进行重命名,本质为move操作。 |
||
| 200 | 3 | * |
|
| 201 | * @param $from_bucket 待操作资源所在空间 |
||
| 202 | 3 | * @param $from_key 待操作资源文件名 |
|
| 203 | 3 | * @param $to_bucket 目标资源空间名 |
|
| 204 | 3 | * @param $to_key 目标资源文件名 |
|
| 205 | 3 | * |
|
| 206 | 3 | * @return mixed 成功返回NULL,失败返回对象Qiniu\Http\Error |
|
| 207 | * @link http://developer.qiniu.com/docs/v6/api/reference/rs/copy.html |
||
| 208 | */ |
||
| 209 | public function copy($from_bucket, $from_key, $to_bucket, $to_key, $force = false) |
||
| 220 | |||
| 221 | /** |
||
| 222 | * 将资源从一个空间到另一个空间 |
||
| 223 | * |
||
| 224 | * @param $from_bucket 待操作资源所在空间 |
||
| 225 | * @param $from_key 待操作资源文件名 |
||
| 226 | * @param $to_bucket 目标资源空间名 |
||
| 227 | * @param $to_key 目标资源文件名 |
||
| 228 | * |
||
| 229 | * @return mixed 成功返回NULL,失败返回对象Qiniu\Http\Error |
||
| 230 | * @link http://developer.qiniu.com/docs/v6/api/reference/rs/move.html |
||
| 231 | */ |
||
| 232 | public function move($from_bucket, $from_key, $to_bucket, $to_key, $force = false) |
||
| 243 | |||
| 244 | /** |
||
| 245 | * 主动修改指定资源的文件元信息 |
||
| 246 | * |
||
| 247 | * @param $bucket 待操作资源所在空间 |
||
| 248 | * @param $key 待操作资源文件名 |
||
| 249 | * @param $mime 待操作文件目标mimeType |
||
| 250 | * |
||
| 251 | * @return mixed 成功返回NULL,失败返回对象Qiniu\Http\Error |
||
| 252 | * @link http://developer.qiniu.com/docs/v6/api/reference/rs/chgm.html |
||
| 253 | */ |
||
| 254 | public function changeMime($bucket, $key, $mime) |
||
| 262 | |||
| 263 | |||
| 264 | /** |
||
| 265 | * 修改指定资源的存储类型 |
||
| 266 | * |
||
| 267 | * @param $bucket 待操作资源所在空间 |
||
| 268 | 3 | * @param $key 待操作资源文件名 |
|
| 269 | * @param $fileType 待操作文件目标文件类型 |
||
| 270 | * |
||
| 271 | 3 | * @return mixed 成功返回NULL,失败返回对象Qiniu\Http\Error |
|
| 272 | 3 | * @link https://developer.qiniu.com/kodo/api/3710/modify-the-file-type |
|
| 273 | 3 | */ |
|
| 274 | public function changeType($bucket, $key, $fileType) |
||
| 281 | |||
| 282 | /** |
||
| 283 | * 修改文件的存储状态,即禁用状态和启用状态间的的互相转换 |
||
| 284 | * |
||
| 285 | * @param $bucket 待操作资源所在空间 |
||
| 286 | * @param $key 待操作资源文件名 |
||
| 287 | * @param $status 待操作文件目标文件类型 |
||
| 288 | * |
||
| 289 | * @return mixed 成功返回NULL,失败返回对象Qiniu\Http\Error |
||
| 290 | * @link https://developer.qiniu.com/kodo/api/4173/modify-the-file-status |
||
| 291 | 3 | */ |
|
| 292 | public function changeStatus($bucket, $key, $status) |
||
| 299 | 3 | ||
| 300 | 3 | /** |
|
| 301 | 3 | * 从指定URL抓取资源,并将该资源存储到指定空间中 |
|
| 302 | * |
||
| 303 | * @param $url 指定的URL |
||
| 304 | * @param $bucket 目标资源空间 |
||
| 305 | * @param $key 目标资源文件名 |
||
| 306 | * |
||
| 307 | * @return array 包含已拉取的文件信息。 |
||
| 308 | * 成功时: [ |
||
| 309 | * [ |
||
| 310 | * "hash" => "<Hash string>", |
||
| 311 | * "key" => "<Key string>" |
||
| 312 | * ], |
||
| 313 | * null |
||
| 314 | * ] |
||
| 315 | * |
||
| 316 | * 失败时: [ |
||
| 317 | * null, |
||
| 318 | * Qiniu/Http/Error |
||
| 319 | * ] |
||
| 320 | 12 | * @link http://developer.qiniu.com/docs/v6/api/reference/rs/fetch.html |
|
| 321 | */ |
||
| 322 | 12 | public function fetch($url, $bucket, $key = null) |
|
| 335 | |||
| 336 | 3 | /** |
|
| 337 | * 从镜像源站抓取资源到空间中,如果空间中已经存在,则覆盖该资源 |
||
| 338 | 3 | * |
|
| 339 | 3 | * @param $bucket 待获取资源所在的空间 |
|
| 340 | 3 | * @param $key 代获取资源文件名 |
|
| 341 | 3 | * |
|
| 342 | * @return mixed 成功返回NULL,失败返回对象Qiniu\Http\Error |
||
| 343 | * @link http://developer.qiniu.com/docs/v6/api/reference/rs/prefetch.html |
||
| 344 | 3 | */ |
|
| 345 | public function prefetch($bucket, $key) |
||
| 357 | |||
| 358 | /** |
||
| 359 | 33 | * 在单次请求中进行多个资源管理操作 |
|
| 360 | * |
||
| 361 | * @param $operations 资源管理操作数组 |
||
| 362 | * |
||
| 363 | * @return array 每个资源的处理情况,结果类似: |
||
| 364 | * [ |
||
| 365 | * { "code" => <HttpCode int>, "data" => <Data> }, |
||
| 366 | * { "code" => <HttpCode int> }, |
||
| 367 | * { "code" => <HttpCode int> }, |
||
| 368 | * { "code" => <HttpCode int> }, |
||
| 369 | * { "code" => <HttpCode int>, "data" => { "error": "<ErrorMessage string>" } }, |
||
| 370 | * ... |
||
| 371 | 27 | * ] |
|
| 372 | * @link http://developer.qiniu.com/docs/v6/api/reference/rs/batch.html |
||
| 373 | 27 | */ |
|
| 374 | 27 | public function batch($operations) |
|
| 379 | |||
| 380 | /** |
||
| 381 | * 设置文件的生命周期 |
||
| 382 | * |
||
| 383 | 9 | * @param $bucket 设置文件生命周期文件所在的空间 |
|
| 384 | * @param $key 设置文件生命周期文件的文件名 |
||
| 385 | 9 | * @param $days 设置该文件多少天后删除,当$days设置为0时表示取消该文件的生命周期 |
|
| 386 | 9 | * |
|
| 387 | * @return Mixed |
||
| 388 | * @link https://developer.qiniu.com/kodo/api/update-file-lifecycle |
||
| 389 | 12 | */ |
|
| 390 | public function deleteAfterDays($bucket, $key, $days) |
||
| 397 | |||
| 398 | private function getRsfHost() |
||
| 406 | 30 | ||
| 407 | 30 | private function getRsHost() |
|
| 415 | |||
| 416 | 3 | private function getApiHost() |
|
| 424 | 6 | ||
| 425 | private function getUcHost() |
||
| 433 | |||
| 434 | 3 | private function rsPost($path, $body = null) |
|
| 439 | |||
| 440 | private function apiPost($path, $body = null) |
||
| 445 | |||
| 446 | private function ucPost($path, $body = null) |
||
| 451 | |||
| 452 | private function apiGet($path) |
||
| 457 | |||
| 458 | private function rsGet($path) |
||
| 463 | |||
| 464 | private function get($url) |
||
| 473 | |||
| 474 | private function post($url, $body) |
||
| 484 | 9 | ||
| 485 | 9 | public static function buildBatchCopy($source_bucket, $key_pairs, $target_bucket, $force) |
|
| 489 | 9 | ||
| 490 | 9 | ||
| 491 | public static function buildBatchRename($bucket, $key_pairs, $force) |
||
| 495 | |||
| 496 | |||
| 497 | public static function buildBatchMove($source_bucket, $key_pairs, $target_bucket, $force) |
||
| 501 | |||
| 502 | |||
| 503 | public static function buildBatchDelete($bucket, $keys) |
||
| 507 | |||
| 508 | |||
| 509 | public static function buildBatchStat($bucket, $keys) |
||
| 513 | |||
| 514 | public static function buildBatchDeleteAfterDays($bucket, $key_day_pairs) |
||
| 522 | |||
| 523 | public static function buildBatchChangeMime($bucket, $key_mime_pairs) |
||
| 531 | |||
| 532 | public static function buildBatchChangeType($bucket, $key_type_pairs) |
||
| 540 | |||
| 541 | private static function oneKeyBatch($operation, $bucket, $keys) |
||
| 549 | |||
| 550 | private static function twoKeyBatch($operation, $source_bucket, $key_pairs, $target_bucket, $force) |
||
| 567 | } |
||
| 568 |
When comparing two booleans, it is generally considered safer to use the strict comparison operator.