Completed
Push — master ( 98fd9c...c23e7d )
by r
04:55
created

CdnManager::post()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

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