Completed
Pull Request — master (#293)
by
unknown
22:09
created

BucketManager::updateBucketEvent()   C

Complexity

Conditions 9
Paths 256

Size

Total Lines 39

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 9

Importance

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