CdnManager::getCdnRefreshList()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 23
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 12
nc 1
nop 8
dl 0
loc 23
ccs 0
cts 9
cp 0
crap 2
rs 9.8666
c 1
b 0
f 1

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
3
namespace Qiniu\Cdn;
4
5
use Qiniu\Auth;
6
use Qiniu\Http\Error;
7
use Qiniu\Http\Client;
8
use Qiniu\Http\Proxy;
9
10
final class CdnManager
11
{
12
13
    private $auth;
14
    private $server;
15 3
    private $proxy;
16
17 3
    public function __construct(Auth $auth, $proxy = null, $proxy_auth = null, $proxy_user_password = null)
18 3
    {
19 3
        $this->auth = $auth;
20
        $this->server = 'http://fusion.qiniuapi.com';
21
        $this->proxy = new Proxy($proxy, $proxy_auth, $proxy_user_password);
22
    }
23
24
    /**
25
     * @param array $urls 待刷新的文件链接数组
26
     * @return array
27
     */
28
    public function refreshUrls(array $urls)
29
    {
30
        return $this->refreshUrlsAndDirs($urls, array());
31
    }
32
33
    /**
34
     * @param array $dirs 待刷新的文件链接数组
35
     * @return array
36
     * 目前客户默认没有目录刷新权限,刷新会有400038报错,参考:https://developer.qiniu.com/fusion/api/1229/cache-refresh
37
     * 需要刷新目录请工单联系技术支持 https://support.qiniu.com/tickets/category
38
     */
39
    public function refreshDirs(array $dirs)
40
    {
41
        return $this->refreshUrlsAndDirs(array(), $dirs);
42
    }
43
44
    /**
45
     * @param array $urls 待刷新的文件链接数组
46
     * @param array $dirs 待刷新的目录链接数组
47
     *
48
     * @return array 刷新的请求回复和错误,参考 examples/cdn_manager.php 代码
49
     * @link http://developer.qiniu.com/article/fusion/api/refresh.html
50
     *
51
     * 目前客户默认没有目录刷新权限,刷新会有400038报错,参考:https://developer.qiniu.com/fusion/api/1229/cache-refresh
52
     * 需要刷新目录请工单联系技术支持 https://support.qiniu.com/tickets/category
53
     */
54
    public function refreshUrlsAndDirs(array $urls, array  $dirs)
55
    {
56
        $req = array();
57
        if (!empty($urls)) {
58
            $req['urls'] = $urls;
59
        }
60
        if (!empty($dirs)) {
61
            $req['dirs'] = $dirs;
62
        }
63
64
        $url = $this->server . '/v2/tune/refresh';
65
        $body = json_encode($req);
66
        return $this->post($url, $body);
67
    }
68
69
    /**
70
     * 查询 CDN 刷新记录
71
     *
72
     * @param string $requestId 指定要查询记录所在的刷新请求id
73
     * @param string $isDir 指定是否查询目录,取值为 yes/no,默认不填则为两种类型记录都查询
74
     * @param array $urls 要查询的url列表,每个url可以是文件url,也可以是目录url
75
     * @param string $state 指定要查询记录的状态,取值processing/success/failure
76
     * @param int $pageNo 要求返回的页号,默认为0
77
     * @param int $pageSize 要求返回的页长度,默认为100
78
     * @param string $startTime 指定查询的开始日期,格式2006-01-01
79
     * @param string $endTime 指定查询的结束日期,格式2006-01-01
80
     * @return array
81
     * @link https://developer.qiniu.com/fusion/api/1229/cache-refresh#4
82
     */
83
    public function getCdnRefreshList(
84
        $requestId = null,
85
        $isDir = null,
86
        $urls = array(),
87
        $state = null,
88
        $pageNo = 0,
89
        $pageSize = 100,
90
        $startTime = null,
91
        $endTime = null
92
    ) {
93
        $req = array();
94
        \Qiniu\setWithoutEmpty($req, 'requestId', $requestId);
95
        \Qiniu\setWithoutEmpty($req, 'isDir', $isDir);
96
        \Qiniu\setWithoutEmpty($req, 'urls', $urls);
0 ignored issues
show
Bug introduced by
$urls of type array is incompatible with the type string expected by parameter $value of Qiniu\setWithoutEmpty(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

96
        \Qiniu\setWithoutEmpty($req, 'urls', /** @scrutinizer ignore-type */ $urls);
Loading history...
97
        \Qiniu\setWithoutEmpty($req, 'state', $state);
98
        \Qiniu\setWithoutEmpty($req, 'pageNo', $pageNo);
99
        \Qiniu\setWithoutEmpty($req, 'pageSize', $pageSize);
100
        \Qiniu\setWithoutEmpty($req, 'startTime', $startTime);
101
        \Qiniu\setWithoutEmpty($req, 'endTime', $endTime);
102
103
        $body = json_encode($req);
104
        $url = $this->server . '/v2/tune/refresh/list';
105
        return $this->post($url, $body);
106
    }
107
108
    /**
109
     * @param array $urls 待预取的文件链接数组
110
     *
111
     * @return array 预取的请求回复和错误,参考 examples/cdn_manager.php 代码
112
     *
113
     * @link http://developer.qiniu.com/article/fusion/api/refresh.html
114
     */
115
    public function prefetchUrls(array $urls)
116
    {
117
        $req = array(
118
            'urls' => $urls,
119
        );
120
121
        $url = $this->server . '/v2/tune/prefetch';
122
        $body = json_encode($req);
123
        return $this->post($url, $body);
124
    }
125
126
    /**
127
     * 查询 CDN 预取记录
128
     *
129
     * @param string $requestId 指定要查询记录所在的刷新请求id
130
     * @param array $urls 要查询的url列表,每个url可以是文件url,也可以是目录url
131
     * @param string $state 指定要查询记录的状态,取值processing/success/failure
132
     * @param int $pageNo 要求返回的页号,默认为0
133
     * @param int $pageSize 要求返回的页长度,默认为100
134
     * @param string $startTime 指定查询的开始日期,格式2006-01-01
135
     * @param string $endTime 指定查询的结束日期,格式2006-01-01
136
     * @return array
137
     * @link https://developer.qiniu.com/fusion/api/1227/file-prefetching#4
138
     */
139
    public function getCdnPrefetchList(
140
        $requestId = null,
141
        $urls = array(),
142
        $state = null,
143
        $pageNo = 0,
144
        $pageSize = 100,
145
        $startTime = null,
146
        $endTime = null
147
    ) {
148
        $req = array();
149
        \Qiniu\setWithoutEmpty($req, 'requestId', $requestId);
150
        \Qiniu\setWithoutEmpty($req, 'urls', $urls);
0 ignored issues
show
Bug introduced by
$urls of type array is incompatible with the type string expected by parameter $value of Qiniu\setWithoutEmpty(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

150
        \Qiniu\setWithoutEmpty($req, 'urls', /** @scrutinizer ignore-type */ $urls);
Loading history...
151
        \Qiniu\setWithoutEmpty($req, 'state', $state);
152
        \Qiniu\setWithoutEmpty($req, 'pageNo', $pageNo);
153
        \Qiniu\setWithoutEmpty($req, 'pageSize', $pageSize);
154
        \Qiniu\setWithoutEmpty($req, 'startTime', $startTime);
155
        \Qiniu\setWithoutEmpty($req, 'endTime', $endTime);
156
157
        $body = json_encode($req);
158
        $url = $this->server . '/v2/tune/prefetch/list';
159
        return $this->post($url, $body);
160
    }
161
162
    /**
163
     * @param array $domains 待获取带宽数据的域名数组
164
     * @param string $startDate 开始的日期,格式类似 2017-01-01
165
     * @param string $endDate 结束的日期,格式类似 2017-01-01
166
     * @param string $granularity 获取数据的时间间隔,可以是 5min, hour 或者 day
167
     *
168
     * @return array 带宽数据和错误信息,参考 examples/cdn_manager.php 代码
169
     *
170 3
     * @link http://developer.qiniu.com/article/fusion/api/traffic-bandwidth.html
171
     */
172 3
    public function getBandwidthData(array $domains, $startDate, $endDate, $granularity)
173 3
    {
174 3
        $req = array();
175 3
        $req['domains'] = implode(';', $domains);
176 3
        $req['startDate'] = $startDate;
177 3
        $req['endDate'] = $endDate;
178 3
        $req['granularity'] = $granularity;
179
180
        $url = $this->server . '/v2/tune/bandwidth';
181 3
        $body = json_encode($req);
182
        return $this->post($url, $body);
183 3
    }
184
185
    /**
186
     * @param array $domains 待获取流量数据的域名数组
187
     * @param string $startDate 开始的日期,格式类似 2017-01-01
188
     * @param string $endDate 结束的日期,格式类似 2017-01-01
189
     * @param string $granularity 获取数据的时间间隔,可以是 5min, hour 或者 day
190
     *
191
     * @return array 流量数据和错误信息,参考 examples/cdn_manager.php 代码
192
     *
193
     * @link http://developer.qiniu.com/article/fusion/api/traffic-bandwidth.html
194
     */
195
    public function getFluxData(array $domains, $startDate, $endDate, $granularity)
196
    {
197
        $req = array();
198
        $req['domains'] = implode(';', $domains);
199
        $req['startDate'] = $startDate;
200
        $req['endDate'] = $endDate;
201
        $req['granularity'] = $granularity;
202
203
        $url = $this->server . '/v2/tune/flux';
204
        $body = json_encode($req);
205
        return $this->post($url, $body);
206
    }
207
208
    /**
209
     * @param array $domains 待获取日志下载链接的域名数组
210
     * @param string $logDate 获取指定日期的日志下载链接,格式类似 2017-01-01
211
     *
212
     * @return array 日志下载链接数据和错误信息,参考 examples/cdn_manager.php 代码
213
     *
214
     * @link http://developer.qiniu.com/article/fusion/api/log.html
215
     */
216
    public function getCdnLogList(array $domains, $logDate)
217
    {
218
        $req = array();
219
        $req['domains'] = implode(';', $domains);
220
        $req['day'] = $logDate;
221
222
        $url = $this->server . '/v2/tune/log/list';
223
        $body = json_encode($req);
224
        return $this->post($url, $body);
225
    }
226
227
    private function post($url, $body)
228
    {
229
        $headers = $this->auth->authorization($url, $body, 'application/json');
230
        $headers['Content-Type'] = 'application/json';
231
        $ret = Client::post($url, $body, $headers, $this->proxy->makeReqOpt());
232
        if (!$ret->ok()) {
233
            return array(null, new Error($url, $ret));
234
        }
235
        $r = ($ret->body === null) ? array() : $ret->json();
236
        return array($r, null);
237
    }
238
239
    /**
240
     * 构建时间戳防盗链鉴权的访问外链
241
     *
242
     * @param string $rawUrl 需要签名的资源url
243
     * @param string $encryptKey 时间戳防盗链密钥
244
     * @param string $durationInSeconds 链接的有效期(以秒为单位)
245
     *
246
     * @return string 带鉴权信息的资源外链,参考 examples/cdn_timestamp_antileech.php 代码
247
     */
248
    public static function createTimestampAntiLeechUrl($rawUrl, $encryptKey, $durationInSeconds)
249
    {
250
        $parsedUrl = parse_url($rawUrl);
251
        $deadline = time() + $durationInSeconds;
252
        $expireHex = dechex($deadline);
253
        $path = isset($parsedUrl['path']) ? $parsedUrl['path'] : '';
254
        $strToSign = $encryptKey . $path . $expireHex;
255
        $signStr = md5($strToSign);
256
        if (isset($parsedUrl['query'])) {
257
            $signedUrl = $rawUrl . '&sign=' . $signStr . '&t=' . $expireHex;
258
        } else {
259
            $signedUrl = $rawUrl . '?sign=' . $signStr . '&t=' . $expireHex;
260
        }
261
        return $signedUrl;
262
    }
263
}
264