Completed
Pull Request — master (#208)
by
unknown
26:29 queued 24:37
created

CdnManager::refreshUrls()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 2
c 1
b 0
f 1
nc 1
nop 1
dl 0
loc 3
rs 10
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
    private $auth;
12
    private $server;
13
14
    public function __construct(Auth $auth) {
15
        $this->auth = $auth;
16
        $this->server = 'http://fusion.qiniuapi.com';
17
    }
18
19
    public function refreshUrls($urls) {
20
        return $this->refreshUrlsAndDirs($urls, null);
21
    }
22
23
    public function refreshDirs($dirs) {
24
        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...
25
    }
26
27
    /**
28
     * @param array $urls 待刷新的文件链接数组
29
     * 
30
     * @return array 刷新的请求回复和错误,参考 examples/cdn_manager.php 代码
31
     * @link http://developer.qiniu.com/article/fusion/api/refresh.html
32
     */
33
    public function refreshUrlsAndDirs($urls, $dirs) {
34
        $req = array();
35
        if (!empty($urls)) {
36
            $req['urls'] = $urls;
37
        }
38
        if (!empty($dirs)) {
39
            $req['dirs'] = $dirs;
40
        }
41
42
        $url = $this->server . '/v2/tune/refresh';
43
        $body = json_encode($req);
44
        return $this->post($url, $body);
45
    }
46
47
    /**
48
     * @param array $urls 待预取的文件链接数组
49
     * 
50
     * @return array 预取的请求回复和错误,参考 examples/cdn_manager.php 代码
51
     *                                         
52
     * @link http://developer.qiniu.com/article/fusion/api/refresh.html
53
     */
54
    public function prefetchUrls($urls) {
55
        $req = array(
56
            'urls' => $urls,
57
        );
58
59
        $url = $this->server . '/v2/tune/prefetch';
60
        $body = json_encode($req);
61
        return $this->post($url, $body);
62
    }
63
64
    /**
65
     * @param array $domains      待获取带宽数据的域名数组
66
     * @param string $startDate   开始的日期,格式类似 2017-01-01
67
     * @param string $endDate     结束的日期,格式类似 2017-01-01
68
     * @param string $granularity 获取数据的时间间隔,可以是 5min, hour 或者 day
69
     * 
70
     * @return array 带宽数据和错误信息,参考 examples/cdn_manager.php 代码
71
     * 
72
     * @link http://developer.qiniu.com/article/fusion/api/traffic-bandwidth.html
73
     */
74
    public function getBandwidthData($domains, $startDate, $endDate, $granularity) {
75
        $req = array();
76
        $req['domains'] = implode(';', $domains);
77
        $req['startDate'] = $startDate;
78
        $req['endDate'] = $endDate;
79
        $req['granularity'] = $granularity;
80
81
        $url = $this->server . '/v2/tune/bandwidth';
82
        $body = json_encode($req);
83
        return $this->post($url, $body);
84
    }
85
86
    /**
87
     * @param array  $domains     待获取流量数据的域名数组
88
     * @param string $startDate   开始的日期,格式类似 2017-01-01
89
     * @param string $endDate     结束的日期,格式类似 2017-01-01
90
     * @param string $granularity 获取数据的时间间隔,可以是 5min, hour 或者 day
91
     * 
92
     * @return array 流量数据和错误信息,参考 examples/cdn_manager.php 代码
93
     * 
94
     * @link http://developer.qiniu.com/article/fusion/api/traffic-bandwidth.html
95
     */
96
    public function getFluxData($domains, $startDate, $endDate, $granularity) {
97
        $req = array();
98
        $req['domains'] = implode(';', $domains);
99
        $req['startDate'] = $startDate;
100
        $req['endDate'] = $endDate;
101
        $req['granularity'] = $granularity;
102
103
        $url = $this->server . '/v2/tune/flux';
104
        $body = json_encode($req);
105
        return $this->post($url, $body);
106
    }
107
108
    /**
109
     * @param array  $domains 待获取日志下载链接的域名数组
110
     * @param string $logDate 获取指定日期的日志下载链接,格式类似 2017-01-01
111
     * 
112
     * @return array 日志下载链接数据和错误信息,参考 examples/cdn_manager.php 代码
113
     * 
114
     * @link http://developer.qiniu.com/article/fusion/api/log.html
115
     */
116
    public function getCdnLogList($domains, $logDate) {
117
        $req = array();
118
        $req['domains'] = implode(';', $domains);
119
        $req['day'] = $logDate;
120
121
        $url = $this->server . '/v2/tune/log/list';
122
        $body = json_encode($req);
123
        return $this->post($url, $body);
124
    }
125
126
    private function post($url, $body) {
127
        $headers = $this->auth->authorization($url, $body, 'application/json');
128
        $headers['Content-Type'] = 'application/json';
129
        $ret = Client::post($url, $body, $headers);
130
        if (!$ret->ok()) {
131
            return array(null, new Error($url, $ret));
132
        }
133
        $r = ($ret->body === null) ? array() : $ret->json();
134
        return array($r, null);
135
    }
136
137
    /**
138
     * 构建时间戳防盗链鉴权的访问外链
139
     *      
140
     * @param string $host        带访问协议的域名
141
     * @param string $fileName    原始文件名,不需要urlencode
142
     * @param string $queryString 查询参数,不需要urlencode
143
     * @param string $encryptKey  时间戳防盗链密钥
144
     * @param string $deadline    链接有效期时间戳(以秒为单位) 
145
     * 
146
     * @return string 带鉴权信息的资源外链,参考 examples/cdn_manager.php 代码
147
     */
148
    public static function createStandardAntileechUrlBasedOnTimestamp($host,
149
            $fileName, $queryString, $encryptKey, $deadline) {
150
        if (!empty($queryString)) {
151
            $urlToSign = $host . '/' . urlencode($fileName) . '?' . urlencode($queryString);
152
        } else {
153
            $urlToSign = $host . '/' . urlencode($fileName);
154
        }
155
156
        $path = '/' . urlencode($fileName);
157
        $expireHex = dechex($deadline);
158
159
        $strToSign = $encryptKey . $path . $expireHex;
160
        $signStr = md5($strToSign);
161
162
        if (!empty($queryString)) {
163
            $signedUrl = $urlToSign . '&sign=' . $signStr . '&t=' . $expireHex;
164
        } else {
165
            $signedUrl = $urlToSign . '?sign=' . $signStr . '&t=' . $expireHex;
166
        }
167
168
        return $signedUrl;
169
    }
170
171
}
172