Completed
Push — master ( 0af6cb...e278f8 )
by Bai
12s queued 10s
created

CdnManager::getCdnPrefetchList()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 7
dl 0
loc 22
ccs 0
cts 14
cp 0
crap 2
rs 9.568
c 0
b 0
f 0
1
<?php
2
3
namespace Qiniu\Cdn;
4
5
use Qiniu\Auth;
6
use Qiniu\Http\Error;
7
use Qiniu\Http\Client;
8
9
final class CdnManager
10
{
11
12
    private $auth;
13
    private $server;
14
15 3
    public function __construct(Auth $auth)
16
    {
17 3
        $this->auth = $auth;
18 3
        $this->server = 'http://fusion.qiniuapi.com';
19 3
    }
20
21
    /**
22
     * @param array $urls 待刷新的文件链接数组
23
     * @return array
24
     */
25
    public function refreshUrls(array $urls)
26
    {
27
        return $this->refreshUrlsAndDirs($urls, array());
28
    }
29
30
    /**
31
     * @param array $dirs 待刷新的文件链接数组
32
     * @return array
33
     * 目前客户默认没有目录刷新权限,刷新会有400038报错,参考:https://developer.qiniu.com/fusion/api/1229/cache-refresh
34
     * 需要刷新目录请工单联系技术支持 https://support.qiniu.com/tickets/category
35
     */
36
    public function refreshDirs(array $dirs)
37
    {
38
        return $this->refreshUrlsAndDirs(array(), $dirs);
39
    }
40
41
    /**
42
     * @param array $urls 待刷新的文件链接数组
43
     * @param array $dirs 待刷新的目录链接数组
44
     *
45
     * @return array 刷新的请求回复和错误,参考 examples/cdn_manager.php 代码
46
     * @link http://developer.qiniu.com/article/fusion/api/refresh.html
47
     *
48
     * 目前客户默认没有目录刷新权限,刷新会有400038报错,参考:https://developer.qiniu.com/fusion/api/1229/cache-refresh
49
     * 需要刷新目录请工单联系技术支持 https://support.qiniu.com/tickets/category
50
     */
51
    public function refreshUrlsAndDirs(array $urls, array  $dirs)
52
    {
53
        $req = array();
54
        if (!empty($urls)) {
55
            $req['urls'] = $urls;
56
        }
57
        if (!empty($dirs)) {
58
            $req['dirs'] = $dirs;
59
        }
60
61
        $url = $this->server . '/v2/tune/refresh';
62
        $body = json_encode($req);
63
        return $this->post($url, $body);
64
    }
65
66
    /**
67
     * 查询 CDN 刷新记录
68
     *
69
     * @param string $requestId 指定要查询记录所在的刷新请求id
70
     * @param string $isDir 指定是否查询目录,取值为 yes/no,默认不填则为两种类型记录都查询
71
     * @param string $urls 要查询的url列表,每个url可以是文件url,也可以是目录url
72
     * @param string $state 指定要查询记录的状态,取值processing/success/failure
73
     * @param int $pageNo 要求返回的页号,默认为0
74
     * @param int $pageSize 要求返回的页长度,默认为100
75
     * @param string $startTime 指定查询的开始日期,格式2006-01-01
76
     * @param string $endTime 指定查询的结束日期,格式2006-01-01
77
     * @return array
78
     * @link https://developer.qiniu.com/fusion/api/1229/cache-refresh#4
79
     */
80
    public function getCdnRefreshList(
81
        $requestId = null,
82
        $isDir = null,
83
        $urls = null,
84
        $state = null,
85
        $pageNo = 0,
86
        $pageSize = 100,
87
        $startTime = null,
88
        $endTime = null
89
    ) {
90
        $req = array();
91
        \Qiniu\setWithoutEmpty($req, 'requestId', $requestId);
92
        \Qiniu\setWithoutEmpty($req, 'isDir', $isDir);
93
        \Qiniu\setWithoutEmpty($req, 'urls', $urls);
94
        \Qiniu\setWithoutEmpty($req, 'state', $state);
95
        \Qiniu\setWithoutEmpty($req, 'pageNo', $pageNo);
96
        \Qiniu\setWithoutEmpty($req, 'pageSize', $pageSize);
97
        \Qiniu\setWithoutEmpty($req, 'startTime', $startTime);
98
        \Qiniu\setWithoutEmpty($req, 'endTime', $endTime);
99
100
        $body = json_encode($req);
101
        $url = $this->server . '/v2/tune/refresh/list';
102
        return $this->post($url, $body);
103
    }
104
105
    /**
106
     * @param array $urls 待预取的文件链接数组
107
     *
108
     * @return array 预取的请求回复和错误,参考 examples/cdn_manager.php 代码
109
     *
110
     * @link http://developer.qiniu.com/article/fusion/api/refresh.html
111
     */
112
    public function prefetchUrls(array $urls)
113
    {
114
        $req = array(
115
            'urls' => $urls,
116
        );
117
118
        $url = $this->server . '/v2/tune/prefetch';
119
        $body = json_encode($req);
120
        return $this->post($url, $body);
121
    }
122
123
    /**
124
     * 查询 CDN 预取记录
125
     *
126
     * @param string $requestId 指定要查询记录所在的刷新请求id
127
     * @param string $urls 要查询的url列表,每个url可以是文件url,也可以是目录url
128
     * @param string $state 指定要查询记录的状态,取值processing/success/failure
129
     * @param int $pageNo 要求返回的页号,默认为0
130
     * @param int $pageSize 要求返回的页长度,默认为100
131
     * @param string $startTime 指定查询的开始日期,格式2006-01-01
132
     * @param string $endTime 指定查询的结束日期,格式2006-01-01
133
     * @return array
134
     * @link https://developer.qiniu.com/fusion/api/1227/file-prefetching#4
135
     */
136
    public function getCdnPrefetchList(
137
        $requestId = null,
138
        $urls = null,
139
        $state = null,
140
        $pageNo = 0,
141
        $pageSize = 100,
142
        $startTime = null,
143
        $endTime = null
144
    ) {
145
        $req = array();
146
        \Qiniu\setWithoutEmpty($req, 'requestId', $requestId);
147
        \Qiniu\setWithoutEmpty($req, 'urls', $urls);
148
        \Qiniu\setWithoutEmpty($req, 'state', $state);
149
        \Qiniu\setWithoutEmpty($req, 'pageNo', $pageNo);
150
        \Qiniu\setWithoutEmpty($req, 'pageSize', $pageSize);
151
        \Qiniu\setWithoutEmpty($req, 'startTime', $startTime);
152
        \Qiniu\setWithoutEmpty($req, 'endTime', $endTime);
153
154
        $body = json_encode($req);
155
        $url = $this->server . '/v2/tune/prefetch/list';
156
        return $this->post($url, $body);
157
    }
158
159
    /**
160
     * @param array $domains 待获取带宽数据的域名数组
161
     * @param string $startDate 开始的日期,格式类似 2017-01-01
162
     * @param string $endDate 结束的日期,格式类似 2017-01-01
163
     * @param string $granularity 获取数据的时间间隔,可以是 5min, hour 或者 day
164
     *
165
     * @return array 带宽数据和错误信息,参考 examples/cdn_manager.php 代码
166
     *
167
     * @link http://developer.qiniu.com/article/fusion/api/traffic-bandwidth.html
168
     */
169
    public function getBandwidthData(array $domains, $startDate, $endDate, $granularity)
170 3
    {
171
        $req = array();
172 3
        $req['domains'] = implode(';', $domains);
173 3
        $req['startDate'] = $startDate;
174 3
        $req['endDate'] = $endDate;
175 3
        $req['granularity'] = $granularity;
176 3
177 3
        $url = $this->server . '/v2/tune/bandwidth';
178 3
        $body = json_encode($req);
179
        return $this->post($url, $body);
180
    }
181 3
182
    /**
183 3
     * @param array $domains 待获取流量数据的域名数组
184
     * @param string $startDate 开始的日期,格式类似 2017-01-01
185
     * @param string $endDate 结束的日期,格式类似 2017-01-01
186
     * @param string $granularity 获取数据的时间间隔,可以是 5min, hour 或者 day
187
     *
188
     * @return array 流量数据和错误信息,参考 examples/cdn_manager.php 代码
189
     *
190
     * @link http://developer.qiniu.com/article/fusion/api/traffic-bandwidth.html
191
     */
192
    public function getFluxData(array $domains, $startDate, $endDate, $granularity)
193
    {
194
        $req = array();
195
        $req['domains'] = implode(';', $domains);
196
        $req['startDate'] = $startDate;
197
        $req['endDate'] = $endDate;
198
        $req['granularity'] = $granularity;
199
200
        $url = $this->server . '/v2/tune/flux';
201
        $body = json_encode($req);
202
        return $this->post($url, $body);
203
    }
204
205
    /**
206
     * @param array $domains 待获取日志下载链接的域名数组
207
     * @param string $logDate 获取指定日期的日志下载链接,格式类似 2017-01-01
208
     *
209
     * @return array 日志下载链接数据和错误信息,参考 examples/cdn_manager.php 代码
210
     *
211
     * @link http://developer.qiniu.com/article/fusion/api/log.html
212
     */
213
    public function getCdnLogList(array $domains, $logDate)
214
    {
215
        $req = array();
216
        $req['domains'] = implode(';', $domains);
217
        $req['day'] = $logDate;
218
219
        $url = $this->server . '/v2/tune/log/list';
220
        $body = json_encode($req);
221
        return $this->post($url, $body);
222
    }
223
224
    private function post($url, $body)
225
    {
226
        $headers = $this->auth->authorization($url, $body, 'application/json');
227
        $headers['Content-Type'] = 'application/json';
228
        $ret = Client::post($url, $body, $headers);
229
        if (!$ret->ok()) {
230
            return array(null, new Error($url, $ret));
231
        }
232
        $r = ($ret->body === null) ? array() : $ret->json();
233
        return array($r, null);
234
    }
235
236
    /**
237
     * 构建时间戳防盗链鉴权的访问外链
238
     *
239
     * @param string $rawUrl 需要签名的资源url
240
     * @param string $encryptKey 时间戳防盗链密钥
241
     * @param string $durationInSeconds 链接的有效期(以秒为单位)
242
     *
243
     * @return string 带鉴权信息的资源外链,参考 examples/cdn_timestamp_antileech.php 代码
244
     */
245
    public static function createTimestampAntiLeechUrl($rawUrl, $encryptKey, $durationInSeconds)
246
    {
247
        $parsedUrl = parse_url($rawUrl);
248
        $deadline = time() + $durationInSeconds;
249
        $expireHex = dechex($deadline);
250
        $path = isset($parsedUrl['path']) ? $parsedUrl['path'] : '';
251
        $strToSign = $encryptKey . $path . $expireHex;
252
        $signStr = md5($strToSign);
253
        if (isset($parsedUrl['query'])) {
254
            $signedUrl = $rawUrl . '&sign=' . $signStr . '&t=' . $expireHex;
255
        } else {
256
            $signedUrl = $rawUrl . '?sign=' . $signStr . '&t=' . $expireHex;
257
        }
258
        return $signedUrl;
259
    }
260
}
261