Completed
Pull Request — master (#343)
by
unknown
20:27
created

CdnManager::getCdnRefreshList()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 6
ccs 0
cts 5
cp 0
crap 2
rs 10
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
     * @param array $urls 待预取的文件链接数组
68
     *
69
     * @return array 预取的请求回复和错误,参考 examples/cdn_manager.php 代码
70
     *
71
     * @link http://developer.qiniu.com/article/fusion/api/refresh.html
72
     */
73
    public function prefetchUrls(array $urls)
74
    {
75
        $req = array(
76
            'urls' => $urls,
77
        );
78
79
        $url = $this->server . '/v2/tune/prefetch';
80
        $body = json_encode($req);
81
        return $this->post($url, $body);
82
    }
83
84
    /**
85
     * @param array $domains 待获取带宽数据的域名数组
86
     * @param string $startDate 开始的日期,格式类似 2017-01-01
87
     * @param string $endDate 结束的日期,格式类似 2017-01-01
88
     * @param string $granularity 获取数据的时间间隔,可以是 5min, hour 或者 day
89
     *
90
     * @return array 带宽数据和错误信息,参考 examples/cdn_manager.php 代码
91
     *
92
     * @link http://developer.qiniu.com/article/fusion/api/traffic-bandwidth.html
93
     */
94
    public function getBandwidthData(array $domains, $startDate, $endDate, $granularity)
95
    {
96
        $req = array();
97
        $req['domains'] = implode(';', $domains);
98
        $req['startDate'] = $startDate;
99
        $req['endDate'] = $endDate;
100
        $req['granularity'] = $granularity;
101
102
        $url = $this->server . '/v2/tune/bandwidth';
103
        $body = json_encode($req);
104
        return $this->post($url, $body);
105
    }
106
107
    /**
108
     * @param array $domains 待获取流量数据的域名数组
109
     * @param string $startDate 开始的日期,格式类似 2017-01-01
110
     * @param string $endDate 结束的日期,格式类似 2017-01-01
111
     * @param string $granularity 获取数据的时间间隔,可以是 5min, hour 或者 day
112
     *
113
     * @return array 流量数据和错误信息,参考 examples/cdn_manager.php 代码
114
     *
115
     * @link http://developer.qiniu.com/article/fusion/api/traffic-bandwidth.html
116
     */
117
    public function getFluxData(array $domains, $startDate, $endDate, $granularity)
118
    {
119
        $req = array();
120
        $req['domains'] = implode(';', $domains);
121
        $req['startDate'] = $startDate;
122
        $req['endDate'] = $endDate;
123
        $req['granularity'] = $granularity;
124
125
        $url = $this->server . '/v2/tune/flux';
126
        $body = json_encode($req);
127
        return $this->post($url, $body);
128
    }
129
130
    /**
131
     * @param array $domains 待获取日志下载链接的域名数组
132
     * @param string $logDate 获取指定日期的日志下载链接,格式类似 2017-01-01
133
     *
134
     * @return array 日志下载链接数据和错误信息,参考 examples/cdn_manager.php 代码
135
     *
136
     * @link http://developer.qiniu.com/article/fusion/api/log.html
137
     */
138
    public function getCdnLogList(array $domains, $logDate)
139
    {
140
        $req = array();
141
        $req['domains'] = implode(';', $domains);
142
        $req['day'] = $logDate;
143
144
        $url = $this->server . '/v2/tune/log/list';
145
        $body = json_encode($req);
146
        return $this->post($url, $body);
147
    }
148
    /**
149
     * @param array $params 查询CDN刷新参数
150
     * @return array 参考 examples/cdn_get_refresh_list.php 代码
151
     */
152
    public function getCdnRefreshList($params = array())
153
    {
154
        $url = $this->server . '/v2/tune/refresh/list';
155
        $body = json_encode($params);
156
        return $this->post($url, $body);
157
    }
158
159
    private function post($url, $body)
160
    {
161
        $headers = $this->auth->authorization($url, $body, 'application/json');
162
        $headers['Content-Type'] = 'application/json';
163
        $ret = Client::post($url, $body, $headers);
164
        if (!$ret->ok()) {
165
            return array(null, new Error($url, $ret));
166
        }
167
        $r = ($ret->body === null) ? array() : $ret->json();
168
        return array($r, null);
169
    }
170 3
171
    /**
172 3
     * 构建时间戳防盗链鉴权的访问外链
173 3
     *
174 3
     * @param string $rawUrl 需要签名的资源url
175 3
     * @param string $encryptKey 时间戳防盗链密钥
176 3
     * @param string $durationInSeconds 链接的有效期(以秒为单位)
177 3
     *
178 3
     * @return string 带鉴权信息的资源外链,参考 examples/cdn_timestamp_antileech.php 代码
179
     */
180
    public static function createTimestampAntiLeechUrl($rawUrl, $encryptKey, $durationInSeconds)
181 3
    {
182
        $parsedUrl = parse_url($rawUrl);
183 3
        $deadline = time() + $durationInSeconds;
184
        $expireHex = dechex($deadline);
185
        $path = isset($parsedUrl['path']) ? $parsedUrl['path'] : '';
186
        $strToSign = $encryptKey . $path . $expireHex;
187
        $signStr = md5($strToSign);
188
        if (isset($parsedUrl['query'])) {
189
            $signedUrl = $rawUrl . '&sign=' . $signStr . '&t=' . $expireHex;
190
        } else {
191
            $signedUrl = $rawUrl . '?sign=' . $signStr . '&t=' . $expireHex;
192
        }
193
        return $signedUrl;
194
    }
195
}
196