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

CacheManager::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
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
 * 主要涉及了空间资源管理及批量操作接口的实现,具体的接口规格可以参考
11
 *
12
 * @link http://developer.qiniu.com/docs/v6/api/reference/rs/
13
 */
14
final class CacheManager
15
{
16
    private $auth;
17
18
    public function __construct(Auth $auth)
19
    {
20
        $this->auth = $auth;
21
    }
22
23
    public function refresh($urls, array $dirs = null)
24
    {
25
26
    	$body = array();
27
    	if (!empty($urls)) {
28
    		$body['urls'] = $urls;
29
    	}
30
    	if (!empty($dirs)) {
31
    		$body['dirs'] = $dirs;
32
    	}
33
34
        $body = json_encode($body);
35
36
    	return $this->post(Config::FUSION_HOST . '/v2/tune/refresh', $body);
37
    }
38
39
    private function post($url, $body)
40
    {
41
        $headers = $this->auth->authorization($url, $body, 'application/json');
42
        $headers['Content-Type'] = 'application/json';
43
        $ret = Client::post($url, $body, $headers);
44
        if (!$ret->ok()) {
45
            return array(null, new Error($url, $ret));
46
        }
47
        $r = ($ret->body === null) ? array() : $ret->json();
48
        return array($r, null);
49
    }
50
}
51