Completed
Pull Request — master (#293)
by
unknown
45:36 queued 20:34
created

BucketManager   F

Complexity

Total Complexity 111

Size/Duplication

Total Lines 984
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 5

Test Coverage

Coverage 73.18%

Importance

Changes 0
Metric Value
dl 0
loc 984
ccs 131
cts 179
cp 0.7318
rs 1.616
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 27 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 42
        } 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 3
        }
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 3
     * 删除空间
80
     *
81 3
     * @param $name     删除的空间名
82 3
     *
83 3
     * @return mixed      成功返回NULL,失败返回对象Qiniu\Http\Error
84 3
     */
85 3
    public function deleteBucket($name)
86 3
    {
87 3
        $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
    public function bucketInfo($bucket)
107 6
    {
108
        $path = '/v2/bucketInfo?bucket=' . $bucket;
109 6
        $info = $this->ucPost($path);
110 6
        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 15
     */
123
    public function bucketInfos($region = null, $global = 'false', $shared = 'false', $fs = 'false')
124 15
    {
125 15
        $path = '/v2/bucketInfos?region=' . $region . '&global=' . $global . '&shared=' . $shared . '&fs=' . $fs;
126 15
        $info = $this->ucPost($path);
127
        return $info;
128
    }
129
130
    /**
131
     * 获取空间绑定的域名列表
132
     * @return string[] 包含空间绑定的所有域名
133
     */
134
135
    /**
136
     * 列取空间的文件列表
137
     *
138
     * @param $bucket     空间名
139 3
     * @param $prefix     列举前缀
140
     * @param $marker     列举标识符
141 3
     * @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 15
    public function listFiles($bucket, $prefix = null, $marker = null, $limit = 1000, $delimiter = null)
156
    {
157 15
        $query = array('bucket' => $bucket);
158 15
        \Qiniu\setWithoutEmpty($query, 'prefix', $prefix);
159 15
        \Qiniu\setWithoutEmpty($query, 'marker', $marker);
160 15
        \Qiniu\setWithoutEmpty($query, 'limit', $limit);
161 3
        \Qiniu\setWithoutEmpty($query, 'delimiter', $delimiter);
162 3
        $url = $this->getRsfHost() . '/list?' . http_build_query($query);
163 15
        return $this->get($url);
164 15
    }
165
166
    /**
167
     * 设置Referer防盗链
168
     *
169
     * @param $bucket     空间名
170
     * @param $mode     0: 表示关闭Referer(使用此选项将会忽略以下参数并将恢复默认值); 1: 表示设置Referer白名单; 2:
171
     * 表示设置Referer黑名单
172
     * @param $norefer     0: 表示不允许空 Refer 访问; 1: 表示允许空 Refer 访问
173
     * @param $pattern      规则字符串, 当前允许格式分为三种: 一种为空主机头域名, 比如 foo.com; 一种是泛域名,
174
     * 比如 *.bar.com; 一种是完全通配符,
175
     *          即一个 *; 多个规则之间用;隔开, 比如: foo.com;*.bar.com;sub.foo.com;*.sub.bar.com
176
     * @param $source_enabled  源站是否支持,默认为0只给CDN配置, 设置为1表示开启源站防盗链
177
     *
178 3
     * @return mixed      成功返回NULL,失败返回对象Qiniu\Http\Error
179
     */
180 3
    // public function referAntiLeech(){
181 3
182 3
    // }
183 3
184
    /**
185
     * 增加bucket生命规则
186 3
     *
187 3
     * @param $bucket     空间名
188
     * @param $name     规则名称 bucket 内唯一,长度小于50,不能为空,只能为字母、数字、下划线
189
     * @param $prefix     同一个 bucket 里面前缀不能重复
190
     * @param $delete_after_days      指定上传文件多少天后删除,指定为0表示不删除,大于0表示多少天后删除,
191
     * 需大于 to_line_after_days
192
     * @param $to_line_after_days  指定文件上传多少天后转低频存储。指定为0表示不转低频存储,
193
     * 小于0表示上传的文件立即变低频存储
194
     * @return mixed      成功返回NULL,失败返回对象Qiniu\Http\Error
195
     */
196
    public function bucketLifecycleRule($bucket, $name, $prefix, $delete_after_days, $to_line_after_days)
197
    {
198
        $path = '/rules/add';
199
        if ($bucket) {
200 3
            $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...
201
        }
202 3
        if ($name) {
203 3
            $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...
204 3
        }
205 3
        if ($prefix) {
206 3
            $params['prefix'] = $prefix;
207
        }
208
        if ($delete_after_days) {
209
            $params['delete_after_days'] = $delete_after_days;
210
        }
211
        if ($to_line_after_days) {
212
            $params['to_line_after_days'] = $to_line_after_days;
213
        }
214
        $data = http_build_query($params);
215
        $info = $this->ucPost($path, $data);
216
        return $info;
217
    }
218
219
    /**
220
     * 更新bucket生命规则
221
     *
222
     * @param $bucket     空间名
223
     * @param $name     规则名称 bucket 内唯一,长度小于50,不能为空,只能为字母、数字、下划线
224
     * @param $prefix     同一个 bucket 里面前缀不能重复
225
     * @param $delete_after_days      指定上传文件多少天后删除,指定为0表示不删除,大于0表示多少天后删除,
226
     * 需大于 to_line_after_days
227
     * @param $to_line_after_days  指定文件上传多少天后转低频存储。指定为0表示不转低频存储,
228
     * 小于0表示上传的文件立即变低频存储
229
     * @return mixed      成功返回NULL,失败返回对象Qiniu\Http\Error
230
     */
231
    public function updateBucketLifecycleRule(
232
        $bucket,
233
        $name,
234
        $prefix,
235
        $delete_after_days,
236
        $to_line_after_days
237
    ) {
238
        $path = '/rules/update';
239
        if ($bucket) {
240
            $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...
241
        }
242
        if ($name) {
243
            $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...
244
        }
245
        if ($prefix) {
246
            $params['prefix'] = $prefix;
247
        }
248
        if ($delete_after_days) {
249
            $params['delete_after_days'] = $delete_after_days;
250
        }
251
        if ($to_line_after_days) {
252
            $params['to_line_after_days'] = $to_line_after_days;
253
        }
254
        $data = http_build_query($params);
255
        $info = $this->ucPost($path, $data);
256
        return $info;
257
    }
258
259
    /**
260
     * 获取bucket生命规则
261
     *
262
     * @param $bucket     空间名
263
     * @return mixed      成功返回NULL,失败返回对象Qiniu\Http\Error
264
     */
265
    public function getBucketLifecycleRules($bucket)
266
    {
267
        $path = '/rules/get?bucket=' . $bucket;
268 3
        $info = $this->ucGet($path);
269
        return $info;
270
    }
271 3
272 3
    /**
273 3
     * 删除bucket生命规则
274
     *
275 3
     * @param $bucket     空间名
276 3
     * @param $name     规则名称 bucket 内唯一,长度小于50,不能为空,只能为字母、数字、下划线
277
     * @return mixed      成功返回NULL,失败返回对象Qiniu\Http\Error
278 3
     */
279 3
    public function deleteBucketLifecycleRule($bucket, $name)
280
    {
281
        $path = '/rules/delete';
282
        if ($bucket) {
283
            $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...
284
        }
285
        if ($name) {
286
            $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...
287
        }
288
        $data = http_build_query($params);
289
        $info = $this->ucPost($path, $data);
290
        return $info;
291 3
    }
292
293 3
    /**
294 3
     * 增加bucket事件通知规则
295
     *
296 3
     * @param $bucket     空间名
297 3
     * @param $name     规则名称 bucket 内唯一,长度小于50,不能为空,只能为字母、数字、下划线
298
     * @param $prefix     同一个 bucket 里面前缀不能重复
299 3
     * @param $suffix      可选,文件配置的后缀
300 3
     * @param $event  事件类型,可以指定多个,包括 put,mkfile,delete,copy,move,append,disable,
301 3
     * enable,deleteMarkerCreate
302
     * @param $callbackURL 通知URL,可以指定多个,失败依次重试
303
     * @param $access_key 可选,设置的话会对通知请求用对应的ak、sk进行签名
304
     * @param $host 可选,通知请求的host
305
     *
306
     * @return mixed      成功返回NULL,失败返回对象Qiniu\Http\Error
307
     */
308
    public function putBucketEvent(
309
        $bucket,
310
        $name,
311
        $prefix,
312
        $suffix,
313
        $event,
314
        $callbackURL,
315
        $access_key = null,
316
        $host = null
317
    ) {
318
        $path = '/events/add';
319
        if ($bucket) {
320 12
            $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...
321
        }
322 12
        if ($name) {
323 12
            $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...
324
        }
325
        if ($prefix) {
326
            $params['prefix'] = $prefix;
327
        }
328
        if ($suffix) {
329
            $params['suffix'] = $suffix;
330
        }
331
        if ($event) {
332
            $params['event'] = $event;
333
        }
334
        if ($callbackURL) {
335
            $params['callbackURL'] = $callbackURL;
336 3
        }
337
        if ($access_key) {
338 3
            $params['access_key'] = $access_key;
339 3
        }
340 3
        if ($host) {
341 3
            $params['host'] = $host;
342
        }
343
        $data = http_build_query($params);
344 3
        $info = $this->ucPost($path, $data);
345
        return $info;
346 3
    }
347 3
348
    /**
349
     * 更新bucket事件通知规则
350 3
     *
351
     * @param $bucket     空间名
352
     * @param $name     规则名称 bucket 内唯一,长度小于50,不能为空,只能为字母、数字、下划线
353 33
     * @param $prefix     同一个 bucket 里面前缀不能重复
354
     * @param $suffix      可选,文件配置的后缀
355 33
     * @param $event  事件类型,可以指定多个,包括 put,mkfile,delete,copy,move,append,disable,
356 33
     * enable,deleteMarkerCreate
357
     * @param $callbackURL 通知URL,可以指定多个,失败依次重试
358
     * @param $access_key 可选,设置的话会对通知请求用对应的ak、sk进行签名
359 33
     * @param $host 可选,通知请求的host
360
     *
361
     * @return mixed      成功返回NULL,失败返回对象Qiniu\Http\Error
362
     */
363
    public function updateBucketEvent(
364
        $bucket,
365
        $name,
366
        $prefix,
367
        $suffix,
368
        $event,
369
        $callbackURL,
370
        $access_key = null,
371 27
        $host = null
372
    ) {
373 27
        $path = '/events/update';
374 27
        if ($bucket) {
375
            $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...
376
        }
377
        if ($name) {
378
            $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...
379
        }
380
        if ($prefix) {
381
            $params['prefix'] = $prefix;
382
        }
383 9
        if ($suffix) {
384
            $params['suffix'] = $suffix;
385 9
        }
386 9
        if ($event) {
387
            $params['event'] = $event;
388
        }
389 12
        if ($callbackURL) {
390
            $params['callbackURL'] = $callbackURL;
391 12
        }
392 12
        if ($access_key) {
393 12
            $params['access_key'] = $access_key;
394 8
        }
395
        if ($host) {
396 8
            $params['host'] = $host;
397
        }
398
        $data = http_build_query($params);
399 33
        $info = $this->ucPost($path, $data);
400
        return $info;
401 33
    }
402 33
403 33
    /**
404 9
     * 获取bucket事件通知规则
405
     *
406 30
     * @param $bucket     空间名
407 30
     * @return mixed      成功返回NULL,失败返回对象Qiniu\Http\Error
408
     */
409
    public function getBucketEvents($bucket)
410 3
    {
411
        $path = '/events/get?bucket=' . $bucket;
412 3
        $info = $this->ucGet($path);
413
        return $info;
414
    }
415
416 3
    /**
417
     * 删除bucket事件通知规则
418 3
     *
419
     * @param $bucket     空间名
420
     * @param $name     规则名称 bucket 内唯一,长度小于50,不能为空,只能为字母、数字、下划线
421
     * @return mixed      成功返回NULL,失败返回对象Qiniu\Http\Error
422 6
     */
423
    public function deleteBucketEvent($bucket, $name)
424 6
    {
425
        $path = '/events/delete';
426
        if ($bucket) {
427
            $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...
428 3
        }
429
        if ($name) {
430 3
            $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...
431
        }
432
        $data = http_build_query($params);
433
        $info = $this->ucPost($path, $data);
434 3
        return $info;
435
    }
436 3
437
    /**
438
     * 设置bucket的跨域信息,最多允许设置10条跨域规则。
439
     * 对于同一个域名如果设置了多条规则,那么按顺序使用第一条匹配的规则去生成返回值。
440
     * 对于简单跨域请求,只匹配 Origin;
441
     * 对于预检请求, 需要匹配 Origin、AllowedMethod、AllowedHeader;
442
     * allowed_orgin: 允许的域名。必填;支持通配符*;*表示全部匹配;只有第一个*生效;需要设置"Scheme";大小写敏感。例如
443
     * 规则:http://*.abc.*.com 请求:"http://test.abc.test.com" 结果:不通过
444
     * 规则:"http://abc.com" 请求:"https://abc.com"/"abc.com" 结果:不通过
445
     * 规则:"abc.com" 请求:"http://abc.com" 结果:不通过
446
     * allowed_method: 允许的方法。必填;不支持通配符;大小写不敏感;
447
     * allowed_header: 允许的header。选填;支持通配符*,但只能是单独的*,表示允许全部header,其他*不生效;
448
     * 空则不允许任何header;大小写不敏感;
449
     * exposed_header: 暴露的header。选填;不支持通配符;X-Log, X-Reqid是默认会暴露的两个header;
450
     * 其他的header如果没有设置,则不会暴露;大小写不敏感;
451
     * max_age: 结果可以缓存的时间。选填;空则不缓存;
452
     * allowed_credentials:该配置不支持设置,默认为true。
453
     * 备注:如果没有设置任何corsRules,那么默认允许所有的跨域请求
454
     */
455
    public function putCorsRules($bucket, $params)
456
    {
457
        $path = '/corsRules/set/' . $bucket;
458
        $data = json_encode($params);
459
        $info = $this->ucPost($path, $data);
460
        return $info;
461
    }
462
463
    /**
464
     * 获取bucket的跨域信息
465
     * $bucket 空间名
466 6
     */
467
    public function getCorsRules($bucket)
468 6
    {
469 6
        $path = '/corsRules/get/' . $bucket;
470 6
        $info = $this->ucGet($path);
471 6
        return $info;
472 6
    }
473
474
    /**
475 9
     * 设置回源规则
476
     * 使用该API设置源站优先级高于/image设置的源站,即IO优先读取source接口设置的源站配置,如果存在会忽略/image设置的源站
477 9
     * Bucket 空间名
478
     * Host(可选)回源Host
479
     * RetryCodes(可选),镜像回源时源站返回Code可以重试,最多指定3个,当前只支持4xx错误码重试
480 9
     * SourceQiniuAK,SourceQiniuSK(可选)如果存在将在回源时对URL进行签名,客户源站可以验证以保证请求来自Qiniu服务器
481 9
     * Expires(可选) 签名过期时间,如果不设置默认为1小时
482 9
     * Addr 回源地址,不可重复。
483 9
     * Weight 权重,范围限制1-100,不填默认为1,回源时会根据所有源的权重值进行源站选择,主备源会分开计算.
484 9
     * Backup 是否备用回源,回源优先尝试主源
485 9
     */
486 9
    public function putBucktSourceConfig($params)
487 9
    {
488 9
        $path = '/mirrorConfig/set';
489 9
        $data = json_encode($params);
490 9
        $info = $this->ucPostV2($path, $data);
491
        return $info;
492
    }
493
494
    /**
495
     * 获取空间回源配置
496
     */
497
    public function getBucktSourceConfig($params)
498
    {
499
        $path = '/mirrorConfig/get';
500
        $data = json_encode($params);
501
        $info = $this->ucPostV2($path, $data);
502
        return $info;
503
    }
504
505
    /**
506
     * 开关原图保护
507
     * mode 为1表示开启原图保护,0表示关闭
508
     */
509
    public function putBucketAccessStyleMode($bucket, $mode)
510
    {
511
        $path = '/accessMode/' . $bucket . '/mode/' . $mode;
512
        $info = $this->ucPost($path, null);
513
        return $info;
514
    }
515
516
    /**
517
     * 设置Bucket的maxAge
518
     * maxAge为0或者负数表示为默认值(31536000)
519
     */
520
    public function putBucketMaxAge($bucket, $maxAge)
521
    {
522
        $path = '/maxAge?bucket=' . $bucket . '&maxAge=' . $maxAge;
523
        $info = $this->ucPost($path, null);
524
        return $info;
525
    }
526
527
    /**
528
     * 设置配额
529
     * <bucket>: 空间名称,不支持授权空间
530
     * <size>: 空间存储量配额,参数传入0或不传表示不更改当前配置,传入-1表示取消限额,新创建的空间默认没有限额。
531
     * <count>: 空间文件数配额,参数含义同<size>
532
     */
533
    public function putBucketQuota($bucket, $size, $count)
534
    {
535
        $path = '/setbucketquota/' . $bucket . '/size/' . $size . '/count/' . $count;
536
        $info = $this->apiPost($path, null);
537
        return $info;
538
    }
539
540
    /**
541
     * 获取配额
542
     * bucket 空间名称
543
     */
544
    public function getBucketQuota($bucket)
545
    {
546
        $path = '/getbucketquota/' . $bucket;
547
        $info = $this->apiPost($path, null);
548
        return $info;
549
    }
550
551
    /**
552
     * 获取资源的元信息,但不返回文件内容
553
     *
554
     * @param $bucket     待获取信息资源所在的空间
555
     * @param $key        待获取资源的文件名
556
     *
557
     * @return array    包含文件信息的数组,类似:
558
     *                                              [
559
     *                                                  "hash" => "<Hash string>",
560
     *                                                  "key" => "<Key string>",
561
     *                                                  "fsize" => <file size>,
562
     *                                                  "putTime" => "<file modify time>"
563
     *                                                  "fileType" => <file type>
564
     *                                              ]
565
     *
566
     * @link  http://developer.qiniu.com/docs/v6/api/reference/rs/stat.html
567
     */
568
    public function stat($bucket, $key)
569
    {
570
        $path = '/stat/' . \Qiniu\entry($bucket, $key);
571
        return $this->rsGet($path);
572
    }
573
574
    /**
575
     * 删除指定资源
576
     *
577
     * @param $bucket     待删除资源所在的空间
578
     * @param $key        待删除资源的文件名
579
     *
580
     * @return mixed      成功返回NULL,失败返回对象Qiniu\Http\Error
581
     * @link  http://developer.qiniu.com/docs/v6/api/reference/rs/delete.html
582
     */
583
    public function delete($bucket, $key)
584
    {
585
        $path = '/delete/' . \Qiniu\entry($bucket, $key);
586
        list(, $error) = $this->rsPost($path);
587
        return $error;
588
    }
589
590
591
    /**
592
     * 给资源进行重命名,本质为move操作。
593
     *
594
     * @param $bucket     待操作资源所在空间
595
     * @param $oldname    待操作资源文件名
596
     * @param $newname    目标资源文件名
597
     *
598
     * @return mixed      成功返回NULL,失败返回对象Qiniu\Http\Error
599
     */
600
    public function rename($bucket, $oldname, $newname)
601
    {
602
        return $this->move($bucket, $oldname, $bucket, $newname);
603
    }
604
605
    /**
606
     * 对资源进行复制。
607
     *
608
     * @param $from_bucket     待操作资源所在空间
609
     * @param $from_key        待操作资源文件名
610
     * @param $to_bucket       目标资源空间名
611
     * @param $to_key          目标资源文件名
612
     *
613
     * @return mixed      成功返回NULL,失败返回对象Qiniu\Http\Error
614
     * @link  http://developer.qiniu.com/docs/v6/api/reference/rs/copy.html
615
     */
616
    public function copy($from_bucket, $from_key, $to_bucket, $to_key, $force = false)
617
    {
618
        $from = \Qiniu\entry($from_bucket, $from_key);
619
        $to = \Qiniu\entry($to_bucket, $to_key);
620
        $path = '/copy/' . $from . '/' . $to;
621
        if ($force === true) {
622
            $path .= '/force/true';
623
        }
624
        list(, $error) = $this->rsPost($path);
625
        return $error;
626
    }
627
628
    /**
629
     * 将资源从一个空间到另一个空间
630
     *
631
     * @param $from_bucket     待操作资源所在空间
632
     * @param $from_key        待操作资源文件名
633
     * @param $to_bucket       目标资源空间名
634
     * @param $to_key          目标资源文件名
635
     *
636
     * @return mixed      成功返回NULL,失败返回对象Qiniu\Http\Error
637
     * @link  http://developer.qiniu.com/docs/v6/api/reference/rs/move.html
638
     */
639
    public function move($from_bucket, $from_key, $to_bucket, $to_key, $force = false)
640
    {
641
        $from = \Qiniu\entry($from_bucket, $from_key);
642
        $to = \Qiniu\entry($to_bucket, $to_key);
643
        $path = '/move/' . $from . '/' . $to;
644
        if ($force) {
645
            $path .= '/force/true';
646
        }
647
        list(, $error) = $this->rsPost($path);
648
        return $error;
649
    }
650
651
    /**
652
     * 主动修改指定资源的文件元信息
653
     *
654
     * @param $bucket     待操作资源所在空间
655
     * @param $key        待操作资源文件名
656
     * @param $mime       待操作文件目标mimeType
657
     *
658
     * @return mixed      成功返回NULL,失败返回对象Qiniu\Http\Error
659
     * @link  http://developer.qiniu.com/docs/v6/api/reference/rs/chgm.html
660
     */
661
    public function changeMime($bucket, $key, $mime)
662
    {
663
        $resource = \Qiniu\entry($bucket, $key);
664
        $encode_mime = \Qiniu\base64_urlSafeEncode($mime);
665
        $path = '/chgm/' . $resource . '/mime/' . $encode_mime;
666
        list(, $error) = $this->rsPost($path);
667
        return $error;
668
    }
669
670
671
    /**
672
     * 修改指定资源的存储类型
673
     *
674
     * @param $bucket     待操作资源所在空间
675
     * @param $key        待操作资源文件名
676
     * @param $fileType       待操作文件目标文件类型
677
     *
678
     * @return mixed      成功返回NULL,失败返回对象Qiniu\Http\Error
679
     * @link  https://developer.qiniu.com/kodo/api/3710/modify-the-file-type
680
     */
681
    public function changeType($bucket, $key, $fileType)
682
    {
683
        $resource = \Qiniu\entry($bucket, $key);
684
        $path = '/chtype/' . $resource . '/type/' . $fileType;
685
        list(, $error) = $this->rsPost($path);
686
        return $error;
687
    }
688
689
    /**
690
     * 修改文件的存储状态,即禁用状态和启用状态间的的互相转换
691
     *
692
     * @param $bucket     待操作资源所在空间
693
     * @param $key        待操作资源文件名
694
     * @param $status       待操作文件目标文件类型
695
     *
696
     * @return mixed      成功返回NULL,失败返回对象Qiniu\Http\Error
697
     * @link  https://developer.qiniu.com/kodo/api/4173/modify-the-file-status
698
     */
699
    public function changeStatus($bucket, $key, $status)
700
    {
701
        $resource = \Qiniu\entry($bucket, $key);
702
        $path = '/chstatus/' . $resource . '/status/' . $status;
703
        list(, $error) = $this->rsPost($path);
704
        return $error;
705
    }
706
707
    /**
708
     * 从指定URL抓取资源,并将该资源存储到指定空间中
709
     *
710
     * @param $url        指定的URL
711
     * @param $bucket     目标资源空间
712
     * @param $key        目标资源文件名
713
     *
714
     * @return array    包含已拉取的文件信息。
715
     *                         成功时:  [
716
     *                                          [
717
     *                                              "hash" => "<Hash string>",
718
     *                                              "key" => "<Key string>"
719
     *                                          ],
720
     *                                          null
721
     *                                  ]
722
     *
723
     *                         失败时:  [
724
     *                                          null,
725
     *                                         Qiniu/Http/Error
726
     *                                  ]
727
     * @link  http://developer.qiniu.com/docs/v6/api/reference/rs/fetch.html
728
     */
729
    public function fetch($url, $bucket, $key = null)
730
    {
731
732
        $resource = \Qiniu\base64_urlSafeEncode($url);
733
        $to = \Qiniu\entry($bucket, $key);
734
        $path = '/fetch/' . $resource . '/to/' . $to;
735
736
        $ak = $this->auth->getAccessKey();
737
        $ioHost = $this->config->getIovipHost($ak, $bucket);
738
739
        $url = $ioHost . $path;
740
        return $this->post($url, null);
741
    }
742
743
    /**
744
     * 从镜像源站抓取资源到空间中,如果空间中已经存在,则覆盖该资源
745
     *
746
     * @param $bucket     待获取资源所在的空间
747
     * @param $key        代获取资源文件名
748
     *
749
     * @return mixed      成功返回NULL,失败返回对象Qiniu\Http\Error
750
     * @link  http://developer.qiniu.com/docs/v6/api/reference/rs/prefetch.html
751
     */
752
    public function prefetch($bucket, $key)
753
    {
754
        $resource = \Qiniu\entry($bucket, $key);
755
        $path = '/prefetch/' . $resource;
756
757
        $ak = $this->auth->getAccessKey();
758
        $ioHost = $this->config->getIovipHost($ak, $bucket);
759
760
        $url = $ioHost . $path;
761
        list(, $error) = $this->post($url, null);
762
        return $error;
763
    }
764
765
    /**
766
     * 在单次请求中进行多个资源管理操作
767
     *
768
     * @param $operations     资源管理操作数组
769
     *
770
     * @return array 每个资源的处理情况,结果类似:
771
     *              [
772
     *                   { "code" => <HttpCode int>, "data" => <Data> },
773
     *                   { "code" => <HttpCode int> },
774
     *                   { "code" => <HttpCode int> },
775
     *                   { "code" => <HttpCode int> },
776
     *                   { "code" => <HttpCode int>, "data" => { "error": "<ErrorMessage string>" } },
777
     *                   ...
778
     *               ]
779
     * @link http://developer.qiniu.com/docs/v6/api/reference/rs/batch.html
780
     */
781
    public function batch($operations)
782
    {
783
        $params = 'op=' . implode('&op=', $operations);
784
        return $this->rsPost('/batch', $params);
785
    }
786
787
    /**
788
     * 设置文件的生命周期
789
     *
790
     * @param $bucket 设置文件生命周期文件所在的空间
791
     * @param $key    设置文件生命周期文件的文件名
792
     * @param $days   设置该文件多少天后删除,当$days设置为0时表示取消该文件的生命周期
793
     *
794
     * @return Mixed
795
     * @link https://developer.qiniu.com/kodo/api/update-file-lifecycle
796
     */
797
    public function deleteAfterDays($bucket, $key, $days)
798
    {
799
        $entry = \Qiniu\entry($bucket, $key);
800
        $path = "/deleteAfterDays/$entry/$days";
801
        list(, $error) = $this->rsPost($path);
802
        return $error;
803
    }
804
805
    private function getRsfHost()
806
    {
807
        $scheme = "http://";
808
        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
        return $scheme . Config::RSF_HOST;
812
    }
813
814
    private function getRsHost()
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::RS_HOST;
821
    }
822
823
    private function getApiHost()
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::API_HOST;
830
    }
831
832
    private function getUcHost()
833
    {
834
        $scheme = "http://";
835
        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...
836
            $scheme = "https://";
837
        }
838
        return $scheme . Config::UC_HOST;
839
    }
840
841
    private function rsPost($path, $body = null)
842
    {
843
        $url = $this->getRsHost() . $path;
844
        return $this->post($url, $body);
845
    }
846
847
    private function apiPost($path, $body = null)
848
    {
849
        $url = $this->getApiHost() . $path;
850
        return $this->post($url, $body);
851
    }
852
853
    private function ucPost($path, $body = null)
854
    {
855
        $url = $this->getUcHost() . $path;
856
        return $this->post($url, $body);
857
    }
858
859
    private function ucGet($path)
860
    {
861
        $url = $this->getUcHost() . $path;
862
        return $this->get($url);
863
    }
864
865
    private function apiGet($path)
866
    {
867
        $url = $this->getApiHost() . $path;
868
        return $this->get($url);
869
    }
870
871
    private function rsGet($path)
872
    {
873
        $url = $this->getRsHost() . $path;
874
        return $this->get($url);
875
    }
876
877
    private function get($url)
878
    {
879
        $headers = $this->auth->authorization($url);
880
        $ret = Client::get($url, $headers);
881
        if (!$ret->ok()) {
882
            return array(null, new Error($url, $ret));
883
        }
884
        return array($ret->json(), null);
885
    }
886
887
    private function post($url, $body)
888
    {
889
        $headers = $this->auth->authorization($url, $body, 'application/x-www-form-urlencoded');
890
        $ret = Client::post($url, $body, $headers);
891
        if (!$ret->ok()) {
892
            return array(null, new Error($url, $ret));
893
        }
894
        $r = ($ret->body === null) ? array() : $ret->json();
895
        return array($r, null);
896
    }
897
898
    private function ucPostV2($path, $body)
899
    {
900
        $url = $this->getUcHost() . $path;
901
        return $this->postV2($url, $body);
902
    }
903
904
    private function postV2($url, $body)
905
    {
906
        $headers = $this->auth->authorizationV2($url, 'POST', $body, 'application/json');
907
        $headers["Content-Type"] = 'application/json';
908
        $ret = Client::post($url, $body, $headers);
909
        if (!$ret->ok()) {
910
            return array(null, new Error($url, $ret));
911
        }
912
        $r = ($ret->body === null) ? array() : $ret->json();
913
        return array($r, null);
914
    }
915
916
    public static function buildBatchCopy($source_bucket, $key_pairs, $target_bucket, $force)
917
    {
918
        return self::twoKeyBatch('/copy', $source_bucket, $key_pairs, $target_bucket, $force);
919
    }
920
921
922
    public static function buildBatchRename($bucket, $key_pairs, $force)
923
    {
924
        return self::buildBatchMove($bucket, $key_pairs, $bucket, $force);
925
    }
926
927
928
    public static function buildBatchMove($source_bucket, $key_pairs, $target_bucket, $force)
929
    {
930
        return self::twoKeyBatch('/move', $source_bucket, $key_pairs, $target_bucket, $force);
931
    }
932
933
934
    public static function buildBatchDelete($bucket, $keys)
935
    {
936
        return self::oneKeyBatch('/delete', $bucket, $keys);
937
    }
938
939
940
    public static function buildBatchStat($bucket, $keys)
941
    {
942
        return self::oneKeyBatch('/stat', $bucket, $keys);
943
    }
944
945
    public static function buildBatchDeleteAfterDays($bucket, $key_day_pairs)
946
    {
947
        $data = array();
948
        foreach ($key_day_pairs as $key => $day) {
949
            array_push($data, '/deleteAfterDays/' . \Qiniu\entry($bucket, $key) . '/' . $day);
950
        }
951
        return $data;
952
    }
953
954
    public static function buildBatchChangeMime($bucket, $key_mime_pairs)
955
    {
956
        $data = array();
957
        foreach ($key_mime_pairs as $key => $mime) {
958
            array_push($data, '/chgm/' . \Qiniu\entry($bucket, $key) . '/mime/' . base64_encode($mime));
959
        }
960
        return $data;
961
    }
962
963
    public static function buildBatchChangeType($bucket, $key_type_pairs)
964
    {
965
        $data = array();
966
        foreach ($key_type_pairs as $key => $type) {
967
            array_push($data, '/chtype/' . \Qiniu\entry($bucket, $key) . '/type/' . $type);
968
        }
969
        return $data;
970
    }
971
972
    private static function oneKeyBatch($operation, $bucket, $keys)
973
    {
974
        $data = array();
975
        foreach ($keys as $key) {
976
            array_push($data, $operation . '/' . \Qiniu\entry($bucket, $key));
977
        }
978
        return $data;
979
    }
980
981
    private static function twoKeyBatch($operation, $source_bucket, $key_pairs, $target_bucket, $force)
982
    {
983
        if ($target_bucket === null) {
984
            $target_bucket = $source_bucket;
985
        }
986
        $data = array();
987
        $forceOp = "false";
988
        if ($force) {
989
            $forceOp = "true";
990
        }
991
        foreach ($key_pairs as $from_key => $to_key) {
992
            $from = \Qiniu\entry($source_bucket, $from_key);
993
            $to = \Qiniu\entry($target_bucket, $to_key);
994
            array_push($data, $operation . '/' . $from . '/' . $to . "/force/" . $forceOp);
995
        }
996
        return $data;
997
    }
998
}
999