Completed
Pull Request — master (#224)
by
unknown
24:43
created

CdnManager   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 187
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 6
Bugs 1 Features 2
Metric Value
c 6
b 1
f 2
dl 0
loc 187
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
    public function refreshUrls($urls)
22
    {
23
        return $this->refreshUrlsAndDirs($urls, null);
24
    }
25
26
    public function refreshDirs($dirs)
27
    {
28
        return $this->refreshUrlsAndDirs(null, $dirs);
0 ignored issues
show
Documentation introduced by
null is of type null, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

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