Completed
Pull Request — master (#293)
by
unknown
10:51 queued 09:27
created

BucketManager   F

Complexity

Total Complexity 111

Size/Duplication

Total Lines 975
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 5

Test Coverage

Coverage 31.27%

Importance

Changes 0
Metric Value
dl 0
loc 975
ccs 121
cts 387
cp 0.3127
rs 1.625
c 0
b 0
f 0
wmc 111
lcom 2
cbo 5

61 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 2
A buckets() 0 8 2
A listbuckets() 0 6 1
A creatBucket() 0 5 1
A deleteBucket() 0 5 1
A domains() 0 4 1
A bucketInfo() 0 6 1
A bucketInfos() 0 6 1
A listFiles() 0 10 1
B bucketLifecycleRule() 0 22 6
B updateBucketLifecycleRule() 0 22 6
A getBucketLifecycleRules() 0 6 1
A deleteBucketLifecycleRule() 0 13 3
C putBucketEvent() 0 39 9
C updateBucketEvent() 0 39 9
A getBucketEvents() 0 6 1
A deleteBucketEvent() 0 13 3
A putCorsRules() 0 7 1
A getCorsRules() 0 6 1
A putBucktSourceConfig() 0 7 1
A getBucktSourceConfig() 0 7 1
A putBucketAccessStyleMode() 0 6 1
A putBucketMaxAge() 0 6 1
A putBucketQuota() 0 6 1
A getBucketQuota() 0 6 1
A stat() 0 5 1
A delete() 0 6 1
A rename() 0 4 1
A copy() 0 11 2
A move() 0 11 2
A changeMime() 0 8 1
A changeType() 0 7 1
A changeStatus() 0 7 1
A fetch() 0 13 1
A prefetch() 0 12 1
A batch() 0 5 1
A deleteAfterDays() 0 7 1
A getRsfHost() 0 8 2
A getRsHost() 0 8 2
A getApiHost() 0 8 2
A getUcHost() 0 8 2
A rsPost() 0 5 1
A apiPost() 0 5 1
A ucPost() 0 5 1
A ucGet() 0 5 1
A apiGet() 0 5 1
A rsGet() 0 5 1
A get() 0 9 2
A post() 0 10 3
A ucPostV2() 0 5 1
A postV2() 0 11 3
A buildBatchCopy() 0 4 1
A buildBatchRename() 0 4 1
A buildBatchMove() 0 4 1
A buildBatchDelete() 0 4 1
A buildBatchStat() 0 4 1
A buildBatchDeleteAfterDays() 0 8 2
A buildBatchChangeMime() 0 8 2
A buildBatchChangeType() 0 8 2
A oneKeyBatch() 0 8 2
A twoKeyBatch() 0 17 4

How to fix   Complexity   

Complex Class

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
2
namespace Qiniu\Storage;
3
4
use Qiniu\Auth;
5
use Qiniu\Config;
6
use Qiniu\Zone;
7
use Qiniu\Http\Client;
8
use Qiniu\Http\Error;
9
10
/**
11
 * 主要涉及了空间资源管理及批量操作接口的实现,具体的接口规格可以参考
12
 *
13
 * @link https://developer.qiniu.com/kodo/api/1274/rs
14
 */
15
final class BucketManager
16
{
17
    private $auth;
18
    private $config;
19
20 42
    public function __construct(Auth $auth, Config $config = null)
21
    {
22 42
        $this->auth = $auth;
23 42
        if ($config == null) {
24 42
            $this->config = new Config();
25 28
        } else {
26
            $this->config = $config;
27
        }
28 42
    }
29
30
    /**
31
     * 获取指定账号下所有的空间名。
32
     *
33
     * @return string[] 包含所有空间名
34
     */
35 3
    public function buckets($shared = true)
36
    {
37 3
        $includeShared = "false";
38 3
        if ($shared === true) {
39 3
            $includeShared = "true";
40 2
        }
41 3
        return $this->rsGet('/buckets?shared=' . $includeShared);
42
    }
43
44
    /**
45
     * 列举空间,返回bucket列表
46
     * region 指定区域,global 指定全局空间。
47
     * 在指定了 region 参数时,
48
     * 如果指定 global 为 true,那么忽略 region 参数指定的区域,返回所有区域的全局空间。
49
     * 如果没有指定 global 为 true,那么返回指定区域中非全局空间。
50
     * 在没有指定 region 参数时(包括指定为空""),
51
     * 如果指定 global 为 true,那么返回所有区域的全局空间。
52
     * 如果没有指定 global 为 true,那么返回指定区域中所有的空间,包括全局空间。
53
     * 在指定了line为 true 时,只返回 Line 空间;否则,只返回非 Line 空间。
54
     * share 参数用于指定共享空间。
55
     */
56
57
    public function listbuckets($region = null, $global = 'false', $line = 'false', $shared = 'false')
0 ignored issues
show
Unused Code introduced by
The parameter $shared is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
58
    {
59
        $path = '/v3/buckets?region=' . $region . '&global=' . $global . '&line=' . $line . '&shared=' . $share;
0 ignored issues
show
Bug introduced by
The variable $share does not exist. Did you mean $shared?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
60
        $info = $this->ucPost($path);
61
        return $info;
62
    }
63
64
    /**
65
     * 创建空间
66
     *
67
     * @param $name     创建的空间名
68
     * @param $region    创建的区域,默认华东
69
     *
70
     * @return mixed      成功返回NULL,失败返回对象Qiniu\Http\Error
71
     */
72
    public function creatBucket($name, $region = 'z0')
73
    {
74
        $path = '/mkbucketv2/'.$name.'/region/' . $region;
75
        return $this->rsPost($path, null);
76
    }
77
78
    /**
79
     * 删除空间
80
     *
81
     * @param $name     删除的空间名
82
     *
83
     * @return mixed      成功返回NULL,失败返回对象Qiniu\Http\Error
84
     */
85
    public function deleteBucket($name)
86
    {
87
        $path = '/drop/'.$name;
88
        return $this->rsPost($path, null);
89
    }
90
91
    /**
92
     * 获取指定空间绑定的所有的域名
93
     *
94
     * @return string[] 包含所有空间域名
95
     */
96
    public function domains($bucket)
97
    {
98
        return $this->apiGet('/v6/domain/list?tbl=' . $bucket);
99
    }
100
101
    /**
102
     * 获取指定空间的相关信息
103
     *
104
     * @return string[] 包含空间信息
105
     */
106 4
    public function bucketInfo($bucket)
107 4
    {
108
        $path = '/v2/bucketInfo?bucket=' . $bucket;
109
        $info = $this->ucPost($path);
110
        return $info;
111
    }
112
113
    /**
114
     * 获取指定zone的空间信息列表
115
     * 在Region 未指定且Global 不为 true 时(包含未指定的情况,下同),返回用户的所有空间。
116
     * 在指定了 region 参数且 global 不为 true 时,只列举非全局空间。
117
     * 在指定了global为 true 时,返回所有全局空间,忽略region 参数
118
     * shared 不指定shared参数或指定shared为rw或false时,返回包含具有读写权限空间,指定shared为rd或true时,返回包含具有读权限空间。
119
     * fs:如果为 true,会返回每个空间当前的文件数和存储量(实时数据)。
120
     *
121
     * @return string[] 包含空间信息
122
     */
123
    public function bucketInfos($region = null, $global = 'false', $shared = 'false', $fs = 'false')
124
    {
125
        $path = '/v2/bucketInfos?region=' . $region . '&global=' . $global . '&shared=' . $shared . '&fs=' . $fs;
126
        $info = $this->ucPost($path);
127
        return $info;
128
    }
129
130
    /**
131
     * 获取空间绑定的域名列表
132
     * @return string[] 包含空间绑定的所有域名
133
     */
134
135
    /**
136
     * 列取空间的文件列表
137
     *
138
     * @param $bucket     空间名
139
     * @param $prefix     列举前缀
140
     * @param $marker     列举标识符
141
     * @param $limit      单次列举个数限制
142
     * @param $delimiter  指定目录分隔符
143
     *
144
     * @return array    包含文件信息的数组,类似:[
145
     *                                              {
146
     *                                                 "hash" => "<Hash string>",
147
     *                                                  "key" => "<Key string>",
148
     *                                                  "fsize" => "<file size>",
149
     *                                                  "putTime" => "<file modify time>"
150
     *                                              },
151
     *                                              ...
152
     *                                            ]
153
     * @link  http://developer.qiniu.com/docs/v6/api/reference/rs/list.html
154
     */
155 3
    public function listFiles($bucket, $prefix = null, $marker = null, $limit = 1000, $delimiter = null)
156
    {
157 3
        $query = array('bucket' => $bucket);
158 3
        \Qiniu\setWithoutEmpty($query, 'prefix', $prefix);
159 3
        \Qiniu\setWithoutEmpty($query, 'marker', $marker);
160 3
        \Qiniu\setWithoutEmpty($query, 'limit', $limit);
161 3
        \Qiniu\setWithoutEmpty($query, 'delimiter', $delimiter);
162 3
        $url = $this->getRsfHost() . '/list?' . http_build_query($query);
163 3
        return $this->get($url);
164
    }
165
166
    /**
167
     * 设置Referer防盗链
168
     *
169
     * @param $bucket     空间名
170
     * @param $mode     0: 表示关闭Referer(使用此选项将会忽略以下参数并将恢复默认值); 1: 表示设置Referer白名单; 2: 表示设置Referer
171
     * 黑名单
172
     * @param $norefer     0: 表示不允许空 Refer 访问; 1: 表示允许空 Refer 访问
173
     * @param $pattern      规则字符串, 当前允许格式分为三种: 一种为空主机头域名, 比如 foo.com; 一种是泛域名, 比如 *.bar.com; 一种
174
     * 是完全通配符,
175
     *          即一个 *; 多个规则之间用;隔开, 比如: foo.com;*.bar.com;sub.foo.com;*.sub.bar.com
176
     * @param $source_enabled  源站是否支持,默认为0只给CDN配置, 设置为1表示开启源站防盗链
177
     *
178
     * @return mixed      成功返回NULL,失败返回对象Qiniu\Http\Error
179
     */
180
    // public function referAntiLeech(){
181
182
    // }
183
184
    /**
185
     * 增加bucket生命规则
186
     *
187
     * @param $bucket     空间名
188
     * @param $name     规则名称 bucket 内唯一,长度小于50,不能为空,只能为字母、数字、下划线
189
     * @param $prefix     同一个 bucket 里面前缀不能重复
190
     * @param $delete_after_days      指定上传文件多少天后删除,指定为0表示不删除,大于0表示多少天后删除,需大于 to_line_after_days
191
     * @param $to_line_after_days  指定文件上传多少天后转低频存储。指定为0表示不转低频存储,小于0表示上传的文件立即变低频存储
192
     *
193
     * @return mixed      成功返回NULL,失败返回对象Qiniu\Http\Error
194
     */
195
    public function bucketLifecycleRule($bucket, $name, $prefix, $delete_after_days, $to_line_after_days)
196
    {
197
        $path = '/rules/add';
198
        if ($bucket) {
199
            $params['bucket'] = $bucket;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$params was never initialized. Although not strictly required by PHP, it is generally a good practice to add $params = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
200
        }
201
        if ($name) {
202
            $params['name'] = $name;
0 ignored issues
show
Bug introduced by
The variable $params does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
203
        }
204
        if ($prefix) {
205
            $params['prefix'] = $prefix;
206
        }
207
        if ($delete_after_days) {
208
            $params['delete_after_days'] = $delete_after_days;
209
        }
210
        if ($to_line_after_days) {
211
            $params['to_line_after_days'] = $to_line_after_days;
212
        }
213
        $data = http_build_query($params);
214
        $info = $this->ucPost($path, $data);
215
        return $info;
216
    }
217
218
    /**
219
     * 更新bucket生命规则
220
     *
221
     * @param $bucket     空间名
222
     * @param $name     规则名称 bucket 内唯一,长度小于50,不能为空,只能为字母、数字、下划线
223
     * @param $prefix     同一个 bucket 里面前缀不能重复
224
     * @param $delete_after_days      指定上传文件多少天后删除,指定为0表示不删除,大于0表示多少天后删除,需大于 to_line_after_days
225
     * @param $to_line_after_days  指定文件上传多少天后转低频存储。指定为0表示不转低频存储,小于0表示上传的文件立即变低频存储
226
     *
227
     * @return mixed      成功返回NULL,失败返回对象Qiniu\Http\Error
228
     */
229
    public function updateBucketLifecycleRule($bucket, $name, $prefix, $delete_after_days, $to_line_after_days)
230
    {
231
        $path = '/rules/update';
232
        if ($bucket) {
233
            $params['bucket'] = $bucket;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$params was never initialized. Although not strictly required by PHP, it is generally a good practice to add $params = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
234
        }
235
        if ($name) {
236
            $params['name'] = $name;
0 ignored issues
show
Bug introduced by
The variable $params does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
237
        }
238
        if ($prefix) {
239
            $params['prefix'] = $prefix;
240
        }
241
        if ($delete_after_days) {
242
            $params['delete_after_days'] = $delete_after_days;
243
        }
244
        if ($to_line_after_days) {
245
            $params['to_line_after_days'] = $to_line_after_days;
246
        }
247
        $data = http_build_query($params);
248
        $info = $this->ucPost($path, $data);
249
        return $info;
250
    }
251
252
    /**
253
     * 获取bucket生命规则
254
     *
255
     * @param $bucket     空间名
256
     * @return mixed      成功返回NULL,失败返回对象Qiniu\Http\Error
257
     */
258
    public function getBucketLifecycleRules($bucket)
259
    {
260
        $path = '/rules/get?bucket=' . $bucket;
261
        $info = $this->ucGet($path);
262
        return $info;
263
    }
264
265
    /**
266
     * 删除bucket生命规则
267
     *
268
     * @param $bucket     空间名
269
     * @param $name     规则名称 bucket 内唯一,长度小于50,不能为空,只能为字母、数字、下划线
270
     * @return mixed      成功返回NULL,失败返回对象Qiniu\Http\Error
271
     */
272
    public function deleteBucketLifecycleRule($bucket, $name)
273
    {
274
        $path = '/rules/delete';
275
        if ($bucket) {
276
            $params['bucket'] = $bucket;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$params was never initialized. Although not strictly required by PHP, it is generally a good practice to add $params = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
277
        }
278
        if ($name) {
279
            $params['name'] = $name;
0 ignored issues
show
Bug introduced by
The variable $params does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
280
        }
281
        $data = http_build_query($params);
282
        $info = $this->ucPost($path, $data);
283
        return $info;
284
    }
285
286
    /**
287
     * 增加bucket事件通知规则
288
     *
289
     * @param $bucket     空间名
290
     * @param $name     规则名称 bucket 内唯一,长度小于50,不能为空,只能为字母、数字、下划线
291
     * @param $prefix     同一个 bucket 里面前缀不能重复
292
     * @param $suffix      可选,文件配置的后缀
293
     * @param $event  事件类型,可以指定多个,包括 put,mkfile,delete,copy,move,append,disable,enable,deleteMarkerCreate
294
     * @param $callbackURL 通知URL,可以指定多个,失败依次重试
295
     * @param $access_key 可选,设置的话会对通知请求用对应的ak、sk进行签名
296
     * @param $host 可选,通知请求的host
297
     *
298
     * @return mixed      成功返回NULL,失败返回对象Qiniu\Http\Error
299
     */
300
    public function putBucketEvent(
301
        $bucket,
302
        $name,
303
        $prefix,
304
        $suffix,
305
        $event,
306
        $callbackURL,
307
        $access_key = null,
308
        $host = null
309
    ) {
310
        $path = '/events/add';
311
        if ($bucket) {
312
            $params['bucket'] = $bucket;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$params was never initialized. Although not strictly required by PHP, it is generally a good practice to add $params = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
313
        }
314
        if ($name) {
315
            $params['name'] = $name;
0 ignored issues
show
Bug introduced by
The variable $params does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
316
        }
317
        if ($prefix) {
318
            $params['prefix'] = $prefix;
319
        }
320
        if ($suffix) {
321
            $params['suffix'] = $suffix;
322
        }
323
        if ($event) {
324
            $params['event'] = $event;
325
        }
326
        if ($callbackURL) {
327
            $params['callbackURL'] = $callbackURL;
328
        }
329
        if ($access_key) {
330
            $params['access_key'] = $access_key;
331
        }
332
        if ($host) {
333
            $params['host'] = $host;
334
        }
335
        $data = http_build_query($params);
336
        $info = $this->ucPost($path, $data);
337
        return $info;
338
    }
339
340
    /**
341
     * 更新bucket事件通知规则
342
     *
343
     * @param $bucket     空间名
344
     * @param $name     规则名称 bucket 内唯一,长度小于50,不能为空,只能为字母、数字、下划线
345
     * @param $prefix     同一个 bucket 里面前缀不能重复
346
     * @param $suffix      可选,文件配置的后缀
347
     * @param $event  事件类型,可以指定多个,包括 put,mkfile,delete,copy,move,append,disable,enable,deleteMarkerCreate
348
     * @param $callbackURL 通知URL,可以指定多个,失败依次重试
349
     * @param $access_key 可选,设置的话会对通知请求用对应的ak、sk进行签名
350
     * @param $host 可选,通知请求的host
351
     *
352
     * @return mixed      成功返回NULL,失败返回对象Qiniu\Http\Error
353
     */
354
    public function updateBucketEvent(
355
        $bucket,
356
        $name,
357
        $prefix,
358
        $suffix,
359
        $event,
360
        $callbackURL,
361
        $access_key = null,
362
        $host = null
363
    ) {
364
        $path = '/events/update';
365
        if ($bucket) {
366
            $params['bucket'] = $bucket;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$params was never initialized. Although not strictly required by PHP, it is generally a good practice to add $params = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
367
        }
368
        if ($name) {
369
            $params['name'] = $name;
0 ignored issues
show
Bug introduced by
The variable $params does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
370
        }
371
        if ($prefix) {
372
            $params['prefix'] = $prefix;
373
        }
374
        if ($suffix) {
375
            $params['suffix'] = $suffix;
376
        }
377
        if ($event) {
378
            $params['event'] = $event;
379
        }
380
        if ($callbackURL) {
381
            $params['callbackURL'] = $callbackURL;
382
        }
383
        if ($access_key) {
384
            $params['access_key'] = $access_key;
385
        }
386
        if ($host) {
387
            $params['host'] = $host;
388
        }
389
        $data = http_build_query($params);
390
        $info = $this->ucPost($path, $data);
391
        return $info;
392
    }
393
394
    /**
395
     * 获取bucket事件通知规则
396
     *
397
     * @param $bucket     空间名
398
     * @return mixed      成功返回NULL,失败返回对象Qiniu\Http\Error
399
     */
400
    public function getBucketEvents($bucket)
401
    {
402
        $path = '/events/get?bucket=' . $bucket;
403
        $info = $this->ucGet($path);
404
        return $info;
405
    }
406
407
    /**
408
     * 删除bucket事件通知规则
409
     *
410
     * @param $bucket     空间名
411
     * @param $name     规则名称 bucket 内唯一,长度小于50,不能为空,只能为字母、数字、下划线
412
     * @return mixed      成功返回NULL,失败返回对象Qiniu\Http\Error
413
     */
414
    public function deleteBucketEvent($bucket, $name)
415
    {
416
        $path = '/events/delete';
417
        if ($bucket) {
418
            $params['bucket'] = $bucket;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$params was never initialized. Although not strictly required by PHP, it is generally a good practice to add $params = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
419
        }
420
        if ($name) {
421
            $params['name'] = $name;
0 ignored issues
show
Bug introduced by
The variable $params does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
422
        }
423
        $data = http_build_query($params);
424
        $info = $this->ucPost($path, $data);
425
        return $info;
426
    }
427
428
    /**
429
     * 设置bucket的跨域信息,最多允许设置10条跨域规则。
430
     * 对于同一个域名如果设置了多条规则,那么按顺序使用第一条匹配的规则去生成返回值。
431
     * 对于简单跨域请求,只匹配 Origin;
432
     * 对于预检请求, 需要匹配 Origin、AllowedMethod、AllowedHeader;
433
     * allowed_orgin: 允许的域名。必填;支持通配符*;*表示全部匹配;只有第一个*生效;需要设置"Scheme";大小写敏感。例如
434
     * 规则:http://*.abc.*.com 请求:"http://test.abc.test.com" 结果:不通过
435
     * 规则:"http://abc.com" 请求:"https://abc.com"/"abc.com" 结果:不通过
436
     * 规则:"abc.com" 请求:"http://abc.com" 结果:不通过
437
     * allowed_method: 允许的方法。必填;不支持通配符;大小写不敏感;
438
     * allowed_header: 允许的header。选填;支持通配符*,但只能是单独的*,表示允许全部header,其他*不生效;空则不允许任何header;大小
439
     * 写不敏感;
440
     * exposed_header: 暴露的header。选填;不支持通配符;X-Log, X-Reqid是默认会暴露的两个header;其他的header如果没有设置,则不会
441
     * 暴露;大小写不敏感;
442
     * max_age: 结果可以缓存的时间。选填;空则不缓存;
443
     * allowed_credentials:该配置不支持设置,默认为true。
444
     * 备注:如果没有设置任何corsRules,那么默认允许所有的跨域请求
445
     */
446
    public function putCorsRules($bucket, $params)
447
    {
448
        $path = '/corsRules/set/' . $bucket;
449
        $data = json_encode($params);
450
        $info = $this->ucPost($path, $data);
451
        return $info;
452
    }
453
454
    /**
455
     * 获取bucket的跨域信息
456
     * $bucket 空间名
457
     */
458
    public function getCorsRules($bucket)
459
    {
460
        $path = '/corsRules/get/' . $bucket;
461
        $info = $this->ucGet($path);
462
        return $info;
463
    }
464
465
    /**
466
     * 设置回源规则
467
     * 使用该API设置源站优先级高于/image设置的源站,即IO优先读取source接口设置的源站配置,如果存在会忽略/image设置的源站
468
     * Bucket 空间名
469
     * Host(可选)回源Host
470
     * RetryCodes(可选),镜像回源时源站返回Code可以重试,最多指定3个,当前只支持4xx错误码重试
471
     * SourceQiniuAK,SourceQiniuSK(可选)如果存在将在回源时对URL进行签名,客户源站可以验证以保证请求来自Qiniu服务器
472
     * Expires(可选) 签名过期时间,如果不设置默认为1小时
473
     * Addr 回源地址,不可重复。
474
     * Weight 权重,范围限制1-100,不填默认为1,回源时会根据所有源的权重值进行源站选择,主备源会分开计算.
475
     * Backup 是否备用回源,回源优先尝试主源
476
     */
477
    public function putBucktSourceConfig($params)
478
    {
479
        $path = '/mirrorConfig/set';
480
        $data = json_encode($params);
481
        $info = $this->ucPostV2($path, $data);
482
        return $info;
483
    }
484
485
    /**
486
     * 获取空间回源配置
487
     */
488
    public function getBucktSourceConfig($params)
489
    {
490
        $path = '/mirrorConfig/get';
491
        $data = json_encode($params);
492
        $info = $this->ucPostV2($path, $data);
493
        return $info;
494
    }
495
496
    /**
497
     * 开关原图保护
498
     * mode 为1表示开启原图保护,0表示关闭
499
     */
500
    public function putBucketAccessStyleMode($bucket, $mode)
501
    {
502
        $path = '/accessMode/' . $bucket . '/mode/' . $mode;
503
        $info = $this->ucPost($path, null);
504
        return $info;
505
    }
506
507
    /**
508
     * 设置Bucket的maxAge
509
     * maxAge为0或者负数表示为默认值(31536000)
510
     */
511
    public function putBucketMaxAge($bucket, $maxAge)
512
    {
513
        $path = '/maxAge?bucket=' . $bucket . '&maxAge=' . $maxAge;
514
        $info = $this->ucPost($path, null);
515
        return $info;
516
    }
517
518
    /**
519
     * 设置配额
520
     * <bucket>: 空间名称,不支持授权空间
521
     * <size>: 空间存储量配额,参数传入0或不传表示不更改当前配置,传入-1表示取消限额,新创建的空间默认没有限额。
522
     * <count>: 空间文件数配额,参数含义同<size>
523
     */
524
    public function putBucketQuota($bucket, $size, $count)
525
    {
526
        $path = '/setbucketquota/' . $bucket . '/size/' . $size . '/count/' . $count;
527
        $info = $this->apiPost($path, null);
528
        return $info;
529
    }
530
531
    /**
532
     * 获取配额
533
     * bucket 空间名称
534
     */
535
    public function getBucketQuota($bucket)
536
    {
537
        $path = '/getbucketquota/' . $bucket;
538
        $info = $this->apiPost($path, null);
539
        return $info;
540
    }
541
542
    /**
543
     * 获取资源的元信息,但不返回文件内容
544
     *
545
     * @param $bucket     待获取信息资源所在的空间
546
     * @param $key        待获取资源的文件名
547
     *
548
     * @return array    包含文件信息的数组,类似:
549
     *                                              [
550
     *                                                  "hash" => "<Hash string>",
551
     *                                                  "key" => "<Key string>",
552
     *                                                  "fsize" => <file size>,
553
     *                                                  "putTime" => "<file modify time>"
554
     *                                                  "fileType" => <file type>
555
     *                                              ]
556
     *
557
     * @link  http://developer.qiniu.com/docs/v6/api/reference/rs/stat.html
558
     */
559 3
    public function stat($bucket, $key)
560
    {
561 3
        $path = '/stat/' . \Qiniu\entry($bucket, $key);
562 3
        return $this->rsGet($path);
563
    }
564
565
    /**
566
     * 删除指定资源
567
     *
568
     * @param $bucket     待删除资源所在的空间
569
     * @param $key        待删除资源的文件名
570
     *
571
     * @return mixed      成功返回NULL,失败返回对象Qiniu\Http\Error
572
     * @link  http://developer.qiniu.com/docs/v6/api/reference/rs/delete.html
573
     */
574 6
    public function delete($bucket, $key)
575
    {
576 6
        $path = '/delete/' . \Qiniu\entry($bucket, $key);
577 6
        list(, $error) = $this->rsPost($path);
578 6
        return $error;
579
    }
580
581
582
    /**
583
     * 给资源进行重命名,本质为move操作。
584
     *
585
     * @param $bucket     待操作资源所在空间
586
     * @param $oldname    待操作资源文件名
587
     * @param $newname    目标资源文件名
588
     *
589
     * @return mixed      成功返回NULL,失败返回对象Qiniu\Http\Error
590
     */
591 3
    public function rename($bucket, $oldname, $newname)
592
    {
593 3
        return $this->move($bucket, $oldname, $bucket, $newname);
594
    }
595
596
    /**
597
     * 对资源进行复制。
598
     *
599
     * @param $from_bucket     待操作资源所在空间
600
     * @param $from_key        待操作资源文件名
601
     * @param $to_bucket       目标资源空间名
602
     * @param $to_key          目标资源文件名
603
     *
604
     * @return mixed      成功返回NULL,失败返回对象Qiniu\Http\Error
605
     * @link  http://developer.qiniu.com/docs/v6/api/reference/rs/copy.html
606
     */
607 12
    public function copy($from_bucket, $from_key, $to_bucket, $to_key, $force = false)
608
    {
609 12
        $from = \Qiniu\entry($from_bucket, $from_key);
610 12
        $to = \Qiniu\entry($to_bucket, $to_key);
611 12
        $path = '/copy/' . $from . '/' . $to;
612 12
        if ($force === true) {
613
            $path .= '/force/true';
614
        }
615 12
        list(, $error) = $this->rsPost($path);
616 12
        return $error;
617
    }
618
619
    /**
620
     * 将资源从一个空间到另一个空间
621
     *
622
     * @param $from_bucket     待操作资源所在空间
623
     * @param $from_key        待操作资源文件名
624
     * @param $to_bucket       目标资源空间名
625
     * @param $to_key          目标资源文件名
626
     *
627
     * @return mixed      成功返回NULL,失败返回对象Qiniu\Http\Error
628
     * @link  http://developer.qiniu.com/docs/v6/api/reference/rs/move.html
629
     */
630 3
    public function move($from_bucket, $from_key, $to_bucket, $to_key, $force = false)
631
    {
632 3
        $from = \Qiniu\entry($from_bucket, $from_key);
633 3
        $to = \Qiniu\entry($to_bucket, $to_key);
634 3
        $path = '/move/' . $from . '/' . $to;
635 3
        if ($force) {
636
            $path .= '/force/true';
637
        }
638 3
        list(, $error) = $this->rsPost($path);
639 3
        return $error;
640
    }
641
642
    /**
643
     * 主动修改指定资源的文件元信息
644
     *
645
     * @param $bucket     待操作资源所在空间
646
     * @param $key        待操作资源文件名
647
     * @param $mime       待操作文件目标mimeType
648
     *
649
     * @return mixed      成功返回NULL,失败返回对象Qiniu\Http\Error
650
     * @link  http://developer.qiniu.com/docs/v6/api/reference/rs/chgm.html
651
     */
652 3
    public function changeMime($bucket, $key, $mime)
653
    {
654 3
        $resource = \Qiniu\entry($bucket, $key);
655 3
        $encode_mime = \Qiniu\base64_urlSafeEncode($mime);
656 3
        $path = '/chgm/' . $resource . '/mime/' . $encode_mime;
657 3
        list(, $error) = $this->rsPost($path);
658 3
        return $error;
659
    }
660
661
662
    /**
663
     * 修改指定资源的存储类型
664
     *
665
     * @param $bucket     待操作资源所在空间
666
     * @param $key        待操作资源文件名
667
     * @param $fileType       待操作文件目标文件类型
668
     *
669
     * @return mixed      成功返回NULL,失败返回对象Qiniu\Http\Error
670
     * @link  https://developer.qiniu.com/kodo/api/3710/modify-the-file-type
671
     */
672
    public function changeType($bucket, $key, $fileType)
673
    {
674
        $resource = \Qiniu\entry($bucket, $key);
675
        $path = '/chtype/' . $resource . '/type/' . $fileType;
676
        list(, $error) = $this->rsPost($path);
677
        return $error;
678
    }
679
680
    /**
681
     * 修改文件的存储状态,即禁用状态和启用状态间的的互相转换
682
     *
683
     * @param $bucket     待操作资源所在空间
684
     * @param $key        待操作资源文件名
685
     * @param $status       待操作文件目标文件类型
686
     *
687
     * @return mixed      成功返回NULL,失败返回对象Qiniu\Http\Error
688
     * @link  https://developer.qiniu.com/kodo/api/4173/modify-the-file-status
689
     */
690
    public function changeStatus($bucket, $key, $status)
691
    {
692
        $resource = \Qiniu\entry($bucket, $key);
693
        $path = '/chstatus/' . $resource . '/status/' . $status;
694
        list(, $error) = $this->rsPost($path);
695
        return $error;
696
    }
697
698
    /**
699
     * 从指定URL抓取资源,并将该资源存储到指定空间中
700
     *
701
     * @param $url        指定的URL
702
     * @param $bucket     目标资源空间
703
     * @param $key        目标资源文件名
704
     *
705
     * @return array    包含已拉取的文件信息。
706
     *                         成功时:  [
707
     *                                          [
708
     *                                              "hash" => "<Hash string>",
709
     *                                              "key" => "<Key string>"
710
     *                                          ],
711
     *                                          null
712
     *                                  ]
713
     *
714
     *                         失败时:  [
715
     *                                          null,
716
     *                                         Qiniu/Http/Error
717
     *                                  ]
718
     * @link  http://developer.qiniu.com/docs/v6/api/reference/rs/fetch.html
719
     */
720 3
    public function fetch($url, $bucket, $key = null)
721
    {
722
723 3
        $resource = \Qiniu\base64_urlSafeEncode($url);
724 3
        $to = \Qiniu\entry($bucket, $key);
725 3
        $path = '/fetch/' . $resource . '/to/' . $to;
726
727 3
        $ak = $this->auth->getAccessKey();
728 3
        $ioHost = $this->config->getIovipHost($ak, $bucket);
729
730
        $url = $ioHost . $path;
731
        return $this->post($url, null);
732
    }
733
734
    /**
735
     * 从镜像源站抓取资源到空间中,如果空间中已经存在,则覆盖该资源
736
     *
737
     * @param $bucket     待获取资源所在的空间
738
     * @param $key        代获取资源文件名
739
     *
740
     * @return mixed      成功返回NULL,失败返回对象Qiniu\Http\Error
741
     * @link  http://developer.qiniu.com/docs/v6/api/reference/rs/prefetch.html
742
     */
743 3
    public function prefetch($bucket, $key)
744
    {
745 3
        $resource = \Qiniu\entry($bucket, $key);
746 3
        $path = '/prefetch/' . $resource;
747
748 3
        $ak = $this->auth->getAccessKey();
749 3
        $ioHost = $this->config->getIovipHost($ak, $bucket);
750
751
        $url = $ioHost . $path;
752
        list(, $error) = $this->post($url, null);
753
        return $error;
754
    }
755
756
    /**
757
     * 在单次请求中进行多个资源管理操作
758
     *
759
     * @param $operations     资源管理操作数组
760
     *
761
     * @return array 每个资源的处理情况,结果类似:
762
     *              [
763
     *                   { "code" => <HttpCode int>, "data" => <Data> },
764
     *                   { "code" => <HttpCode int> },
765
     *                   { "code" => <HttpCode int> },
766
     *                   { "code" => <HttpCode int> },
767
     *                   { "code" => <HttpCode int>, "data" => { "error": "<ErrorMessage string>" } },
768
     *                   ...
769
     *               ]
770
     * @link http://developer.qiniu.com/docs/v6/api/reference/rs/batch.html
771
     */
772 12
    public function batch($operations)
773
    {
774 12
        $params = 'op=' . implode('&op=', $operations);
775 12
        return $this->rsPost('/batch', $params);
776
    }
777
778
    /**
779
     * 设置文件的生命周期
780
     *
781
     * @param $bucket 设置文件生命周期文件所在的空间
782
     * @param $key    设置文件生命周期文件的文件名
783
     * @param $days   设置该文件多少天后删除,当$days设置为0时表示取消该文件的生命周期
784
     *
785
     * @return Mixed
786
     * @link https://developer.qiniu.com/kodo/api/update-file-lifecycle
787
     */
788 3
    public function deleteAfterDays($bucket, $key, $days)
789
    {
790 3
        $entry = \Qiniu\entry($bucket, $key);
791 3
        $path = "/deleteAfterDays/$entry/$days";
792 3
        list(, $error) = $this->rsPost($path);
793 3
        return $error;
794
    }
795
796 3
    private function getRsfHost()
797
    {
798 3
        $scheme = "http://";
799 3
        if ($this->config->useHTTPS == true) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
800
            $scheme = "https://";
801
        }
802 3
        return $scheme . Config::RSF_HOST;
803
    }
804
805 33
    private function getRsHost()
806
    {
807 33
        $scheme = "http://";
808 33
        if ($this->config->useHTTPS == true) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
809
            $scheme = "https://";
810
        }
811 33
        return $scheme . Config::RS_HOST;
812
    }
813
814
    private function getApiHost()
815
    {
816
        $scheme = "http://";
817
        if ($this->config->useHTTPS == true) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
818
            $scheme = "https://";
819
        }
820
        return $scheme . Config::API_HOST;
821
    }
822
823
    private function getUcHost()
824
    {
825
        $scheme = "http://";
826
        if ($this->config->useHTTPS == true) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
827
            $scheme = "https://";
828
        }
829
        return $scheme . Config::UC_HOST;
830
    }
831
832 27
    private function rsPost($path, $body = null)
833
    {
834 27
        $url = $this->getRsHost() . $path;
835 27
        return $this->post($url, $body);
836
    }
837
838
    private function apiPost($path, $body = null)
839
    {
840
        $url = $this->getApiHost() . $path;
841
        return $this->post($url, $body);
842
    }
843
844
    private function ucPost($path, $body = null)
845
    {
846
        $url = $this->getUcHost() . $path;
847
        return $this->post($url, $body);
848
    }
849
850
    private function ucGet($path)
851
    {
852
        $url = $this->getUcHost() . $path;
853
        return $this->get($url);
854
    }
855
856
    private function apiGet($path)
857
    {
858
        $url = $this->getApiHost() . $path;
859
        return $this->get($url);
860
    }
861
862 6
    private function rsGet($path)
863
    {
864 6
        $url = $this->getRsHost() . $path;
865 6
        return $this->get($url);
866
    }
867
868 9
    private function get($url)
869
    {
870 9
        $headers = $this->auth->authorization($url);
871 9
        $ret = Client::get($url, $headers);
872 9
        if (!$ret->ok()) {
873 9
            return array(null, new Error($url, $ret));
874
        }
875
        return array($ret->json(), null);
876
    }
877
878 27
    private function post($url, $body)
879
    {
880 27
        $headers = $this->auth->authorization($url, $body, 'application/x-www-form-urlencoded');
881 27
        $ret = Client::post($url, $body, $headers);
882 27
        if (!$ret->ok()) {
883 27
            return array(null, new Error($url, $ret));
884
        }
885
        $r = ($ret->body === null) ? array() : $ret->json();
886
        return array($r, null);
887
    }
888
889
    private function ucPostV2($path, $body)
890
    {
891
        $url = $this->getUcHost() . $path;
892
        return $this->postV2($url, $body);
893
    }
894
895
    private function postV2($url, $body)
896
    {
897
        $headers = $this->auth->authorizationV2($url, 'POST', $body, 'application/json');
898
        $headers["Content-Type"] = 'application/json';
899
        $ret = Client::post($url, $body, $headers);
900
        if (!$ret->ok()) {
901
            return array(null, new Error($url, $ret));
902
        }
903
        $r = ($ret->body === null) ? array() : $ret->json();
904
        return array($r, null);
905
    }
906
907 3
    public static function buildBatchCopy($source_bucket, $key_pairs, $target_bucket, $force)
908
    {
909 3
        return self::twoKeyBatch('/copy', $source_bucket, $key_pairs, $target_bucket, $force);
910
    }
911
912
913 3
    public static function buildBatchRename($bucket, $key_pairs, $force)
914
    {
915 3
        return self::buildBatchMove($bucket, $key_pairs, $bucket, $force);
916
    }
917
918
919 6
    public static function buildBatchMove($source_bucket, $key_pairs, $target_bucket, $force)
920
    {
921 6
        return self::twoKeyBatch('/move', $source_bucket, $key_pairs, $target_bucket, $force);
922
    }
923
924
925
    public static function buildBatchDelete($bucket, $keys)
926
    {
927
        return self::oneKeyBatch('/delete', $bucket, $keys);
928
    }
929
930
931 3
    public static function buildBatchStat($bucket, $keys)
932
    {
933 3
        return self::oneKeyBatch('/stat', $bucket, $keys);
934
    }
935
936
    public static function buildBatchDeleteAfterDays($bucket, $key_day_pairs)
937
    {
938
        $data = array();
939
        foreach ($key_day_pairs as $key => $day) {
940
            array_push($data, '/deleteAfterDays/' . \Qiniu\entry($bucket, $key) . '/' . $day);
941
        }
942
        return $data;
943
    }
944
945
    public static function buildBatchChangeMime($bucket, $key_mime_pairs)
946
    {
947
        $data = array();
948
        foreach ($key_mime_pairs as $key => $mime) {
949
            array_push($data, '/chgm/' . \Qiniu\entry($bucket, $key) . '/mime/' . base64_encode($mime));
950
        }
951
        return $data;
952
    }
953
954
    public static function buildBatchChangeType($bucket, $key_type_pairs)
955
    {
956
        $data = array();
957
        foreach ($key_type_pairs as $key => $type) {
958
            array_push($data, '/chtype/' . \Qiniu\entry($bucket, $key) . '/type/' . $type);
959
        }
960
        return $data;
961
    }
962
963 3
    private static function oneKeyBatch($operation, $bucket, $keys)
964
    {
965 3
        $data = array();
966 3
        foreach ($keys as $key) {
967 3
            array_push($data, $operation . '/' . \Qiniu\entry($bucket, $key));
968 2
        }
969 3
        return $data;
970
    }
971
972 9
    private static function twoKeyBatch($operation, $source_bucket, $key_pairs, $target_bucket, $force)
973
    {
974 9
        if ($target_bucket === null) {
975
            $target_bucket = $source_bucket;
976
        }
977 9
        $data = array();
978 9
        $forceOp = "false";
979 9
        if ($force) {
980 9
            $forceOp = "true";
981 6
        }
982 9
        foreach ($key_pairs as $from_key => $to_key) {
983 9
            $from = \Qiniu\entry($source_bucket, $from_key);
984 9
            $to = \Qiniu\entry($target_bucket, $to_key);
985 9
            array_push($data, $operation . '/' . $from . '/' . $to . "/force/" . $forceOp);
986 6
        }
987 9
        return $data;
988
    }
989
}
990