Completed
Pull Request — master (#224)
by
unknown
18:38 queued 16:40
created

CdnManager   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 200
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 7
Bugs 1 Features 2
Metric Value
c 7
b 1
f 2
dl 0
loc 200
rs 10
wmc 20
lcom 1
cbo 4

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A refreshUrls() 0 4 1
A refreshDirs() 0 4 1
B refreshUrlsAndDirs() 0 20 5
A prefetchUrls() 0 13 2
A getBandwidthData() 0 12 1
A getFluxData() 0 12 1
A getCdnLogList() 0 10 1
A post() 0 11 3
B createTimestampAntiLeechUrl() 0 28 4
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
    public function __construct(Auth $auth)
16
    {
17
        $this->auth = $auth;
18
        $this->server = 'http://fusion.qiniuapi.com';
19
    }
20
21
    /**
22
     * @param string|array $urls
23
     * @return array
24
     */
25
    public function refreshUrls($urls)
26
    {
27
        return $this->refreshUrlsAndDirs($urls, null);
28
    }
29
30
    /**
31
     * @param string|array $dirs
32
     * @return array
33
     */
34
    public function refreshDirs($dirs)
35
    {
36
        return $this->refreshUrlsAndDirs(null, $dirs);
37
    }
38
39
    /**
40
     * @param string|array $urls 待刷新的文件链接数组
41
     * @param string|array $dirs 待刷新的文件夹链接数组
42
     * @return array 刷新的请求回复和错误,参考 examples/cdn_manager.php 代码
43
     * @link http://developer.qiniu.com/article/fusion/api/refresh.html
44
     */
45
    public function refreshUrlsAndDirs($urls, $dirs)
46
    {
47
        $req = array();
48
        if (!empty($urls)) {
49
            if (!is_array($urls)){
50
                $urls = array($urls);
51
            }
52
            $req['urls'] = $urls;
53
        }
54
        if (!empty($dirs)) {
55
            if (!is_array($dirs)){
56
                $dirs = array($dirs);
57
            }
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 https://developer.qiniu.com/fusion/api/1227/file-prefetching
72
     */
73
    public function prefetchUrls($urls)
74
    {
75
        if (!is_array($urls)){
76
            $urls = array($urls);
77
        }
78
        $req = array(
79
            'urls' => $urls,
80
        );
81
82
        $url = $this->server . '/v2/tune/prefetch';
83
        $body = json_encode($req);
84
        return $this->post($url, $body);
85
    }
86
87
    /**
88
     * @param array $domains      待获取带宽数据的域名数组
89
     * @param string $startDate   开始的日期,格式类似 2017-01-01
90
     * @param string $endDate     结束的日期,格式类似 2017-01-01
91
     * @param string $granularity 获取数据的时间间隔,可以是 5min, hour 或者 day
92
     *
93
     * @return array 带宽数据和错误信息,参考 examples/cdn_manager.php 代码
94
     *
95
     * @link http://developer.qiniu.com/article/fusion/api/traffic-bandwidth.html
96
     */
97
    public function getBandwidthData($domains, $startDate, $endDate, $granularity)
98
    {
99
        $req = array();
100
        $req['domains'] = implode(';', $domains);
101
        $req['startDate'] = $startDate;
102
        $req['endDate'] = $endDate;
103
        $req['granularity'] = $granularity;
104
105
        $url = $this->server . '/v2/tune/bandwidth';
106
        $body = json_encode($req);
107
        return $this->post($url, $body);
108
    }
109
110
    /**
111
     * @param array  $domains     待获取流量数据的域名数组
112
     * @param string $startDate   开始的日期,格式类似 2017-01-01
113
     * @param string $endDate     结束的日期,格式类似 2017-01-01
114
     * @param string $granularity 获取数据的时间间隔,可以是 5min, hour 或者 day
115
     *
116
     * @return array 流量数据和错误信息,参考 examples/cdn_manager.php 代码
117
     *
118
     * @link http://developer.qiniu.com/article/fusion/api/traffic-bandwidth.html
119
     */
120
    public function getFluxData($domains, $startDate, $endDate, $granularity)
121
    {
122
        $req = array();
123
        $req['domains'] = implode(';', $domains);
124
        $req['startDate'] = $startDate;
125
        $req['endDate'] = $endDate;
126
        $req['granularity'] = $granularity;
127
128
        $url = $this->server . '/v2/tune/flux';
129
        $body = json_encode($req);
130
        return $this->post($url, $body);
131
    }
132
133
    /**
134
     * @param array  $domains 待获取日志下载链接的域名数组
135
     * @param string $logDate 获取指定日期的日志下载链接,格式类似 2017-01-01
136
     *
137
     * @return array 日志下载链接数据和错误信息,参考 examples/cdn_manager.php 代码
138
     *
139
     * @link http://developer.qiniu.com/article/fusion/api/log.html
140
     */
141
    public function getCdnLogList($domains, $logDate)
142
    {
143
        $req = array();
144
        $req['domains'] = implode(';', $domains);
145
        $req['day'] = $logDate;
146
147
        $url = $this->server . '/v2/tune/log/list';
148
        $body = json_encode($req);
149
        return $this->post($url, $body);
150
    }
151
152
    /**
153
     * @param $url
154
     * @param $body
155
     * @return array
156
     */
157
    private function post($url, $body)
158
    {
159
        $headers = $this->auth->authorization($url, $body, 'application/json');
160
        $headers['Content-Type'] = 'application/json';
161
        $ret = Client::post($url, $body, $headers);
162
        if (!$ret->ok()) {
163
            return array(null, new Error($url, $ret));
164
        }
165
        $r = ($ret->body === null) ? array() : $ret->json();
166
        return array($r, null);
167
    }
168
169
    /**
170
     * 构建时间戳防盗链鉴权的访问外链
171
     *
172
     * @param string $host             带访问协议的域名
173
     * @param string $fileName         原始文件名,不需要urlencode
174
     * @param string $queryStringArray 查询参数命名数组,不需要urlencode
175
     * @param string $encryptKey       时间戳防盗链密钥
176
     * @param string $deadline         链接有效期时间戳(以秒为单位)
177
     *
178
     * @return string 带鉴权信息的资源外链,参考 examples/cdn_manager.php 代码
179
     */
180
    public static function createTimestampAntiLeechUrl($host, $fileName, $queryStringArray, $encryptKey, $deadline)
181
    {
182
        $encodedFileName= str_replace("+", "%20", urlencode($fileName));
183
        if (!empty($queryStringArray)) {
184
            $queryStrings = array();
185
            foreach ($queryStringArray as $key => $value) {
0 ignored issues
show
Bug introduced by
The expression $queryStringArray of type string is not traversable.
Loading history...
186
                array_push($queryStrings, urlencode($key) . '=' . urlencode($value));
187
            }
188
            $queryString = implode('&', $queryStrings);
189
            $urlToSign = $host . '/' . $encodedFileName . '?' . $queryString;
190
        } else {
191
            $urlToSign = $host . '/' . $encodedFileName;
192
        }
193
194
        $path = '/' . $encodedFileName;
195
        $expireHex = dechex($deadline);
196
197
        $strToSign = $encryptKey . $path . $expireHex;
198
        $signStr = md5($strToSign);
199
200
        if (!empty($queryString)) {
201
            $signedUrl = $urlToSign . '&sign=' . $signStr . '&t=' . $expireHex;
202
        } else {
203
            $signedUrl = $urlToSign . '?sign=' . $signStr . '&t=' . $expireHex;
204
        }
205
206
        return $signedUrl;
207
    }
208
}
209