Completed
Pull Request — master (#194)
by r
02:24
created

CacheManager::refresh()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 8
nc 4
nop 2
dl 0
loc 15
rs 9.4285
c 0
b 0
f 0
1
<?php
2
namespace Qiniu\Cdn;
3
4
use Qiniu\Auth;
5
use Qiniu\Config;
6
use Qiniu\Http\Client;
7
use Qiniu\Http\Error;
8
9
/**
10
 * 主要涉及了CDN 缓存的刷新和预取
11
 *
12
 * @link http://developer.qiniu.com/article/fusion/api/refresh.html
13
 * @link http://developer.qiniu.com/article/fusion/api/prefetch.html
14
 */
15
final class CacheManager
16
{
17
    private $auth;
18
19
    public function __construct(Auth $auth)
20
    {
21
        $this->auth = $auth;
22
    }
23
24
    public function refresh($urls, array $dirs = null)
25
    {
26
27
    	$body = array();
28
    	if (!empty($urls)) {
29
    		$body['urls'] = $urls;
30
    	}
31
    	if (!empty($dirs)) {
32
    		$body['dirs'] = $dirs;
33
    	}
34
35
        $body = json_encode($body);
36
37
    	return $this->post(Config::FUSION_HOST . '/v2/tune/refresh', $body);
38
    }
39
40
    private function post($url, $body)
41
    {
42
        $headers = $this->auth->authorization($url, $body, 'application/json');
43
        $headers['Content-Type'] = 'application/json';
44
        $ret = Client::post($url, $body, $headers);
45
        if (!$ret->ok()) {
46
            return array(null, new Error($url, $ret));
47
        }
48
        $r = ($ret->body === null) ? array() : $ret->json();
49
        return array($r, null);
50
    }
51
}
52