Completed
Pull Request — master (#293)
by
unknown
15:58 queued 14:39
created

BucketManager::putBucketEvent()   C

Complexity

Conditions 9
Paths 256

Size

Total Lines 30

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 90

Importance

Changes 0
Metric Value
cc 9
nc 256
nop 8
dl 0
loc 30
ccs 0
cts 29
cp 0
crap 90
rs 6.5222
c 0
b 0
f 0

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

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