Completed
Pull Request — master (#190)
by r
02:19
created

Zone::getBucketHosts()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 24
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 4
Bugs 0 Features 0
Metric Value
cc 3
eloc 15
c 4
b 0
f 0
nc 3
nop 2
dl 0
loc 24
ccs 0
cts 0
cp 0
crap 12
rs 8.9713
1
<?php
2
namespace Qiniu;
3
4
use Qiniu\Http\Client;
5
use Qiniu\Http\Error;
6
7
final class Zone
8
{
9 18
    public $ioHost;            // 七牛源站Host
10
    public $upHost;
11 18
    public $upHostBackup;
12 18
13 18
    //array(
14
    //    <scheme>:<ak>:<bucket> ==>
15 15
    //        array('deadline' => 'xxx', 'upHosts' => array(), 'ioHost' => 'xxx.com')
16
    //)
17 15
    public $hostCache;
18
    public $scheme = 'http';
19
20
    public function __construct($scheme = null)
21
    {
22
        $this->hostCache = array();
23
        if ($scheme != null) {
24
            $this->scheme = $scheme;
25
        }
26
    }
27
28
    public function getUpHostByToken($uptoken)
29
    {
30
        list($ak, $bucket) = $this->unmarshalUpToken($uptoken);
31
        list($upHosts,) = $this->getUpHosts($ak, $bucket);
32
        return $upHosts[0];
33
    }
34
35
    public function getBackupUpHostByToken($uptoken)
36
    {
37
        list($ak, $bucket) = $this->unmarshalUpToken($uptoken);
38
        list($upHosts,) = $this->getUpHosts($ak, $bucket);
39
        return $upHosts[1];
40
    }
41
42
    public function getIoHost($ak, $bucket)
43
    {
44
        list($bucketHosts,) = $this->getBucketHosts($ak, $bucket);
45
        $ioHosts = $bucketHosts['ioHost'];
46
        if (count($ioHosts) === 0) {
47
            return "";
48
        }
49
50
        return $ioHosts[0];
51
    }
52
53
    public function getUpHosts($ak, $bucket)
54
    {
55
        list($bucketHosts, $err) = $this->getBucketHosts($ak, $bucket);
56
        if ($err !== null) {
57
            return array(null, $err);
58
        }
59
60
        $upHosts = $bucketHosts['upHosts'];
61
        return array($upHosts, null);
62
    }
63
64
    private function unmarshalUpToken($uptoken)
65
    {
66
        $token = split(':', $uptoken);
0 ignored issues
show
Deprecated Code introduced by
The function split() has been deprecated with message: Deprecated as of PHP 5.3.0. Relying on this feature is highly discouraged (use preg_split() instead).

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
67
        if (count($token) !== 3) {
68
            throw new \Exception("Invalid Uptoken", 1);
69
        }
70
71
        $ak = $token[0];
72
        $policy = base64_urlSafeDecode($token[2]);
73
        $policy = json_decode($policy, true);
74
75
        $bucket = $policy['scope'];
76
        if (strpos($bucket, ':')) {
77
            $bucket = split(':', $bucket)[0];
0 ignored issues
show
Deprecated Code introduced by
The function split() has been deprecated with message: Deprecated as of PHP 5.3.0. Relying on this feature is highly discouraged (use preg_split() instead).

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
78
        }
79
         
80
        return array($ak, $bucket);
81
    }
82
83
    public function getBucketHosts($ak, $bucket)
84
    {
85
        $key = $this->scheme . $ak . $bucket;
86
87
        $bucketHosts = $this->getBucketHostsFromCache($key);
88
        if (count($bucketHosts) > 0) {
89
            return array($bucketHosts, null);
90
        }
91
92
        list($hosts, $err) = $this->bucketHosts($ak, $bucket);
93
        if ($err !== null) {
94
            return array(null , $err);
95
        }
96
97
        $schemeHosts = $hosts[$this->scheme];
98
        $bucketHosts = array(
99
            'upHosts' => $schemeHosts['up'],
100
            'ioHost' => $schemeHosts['io'],
101
            'deadline' => time() + $hosts['ttl']
102
        );
103
104
        $this->setBucketHostsToCache($key, $bucketHosts);
105
        return array($bucketHosts, null);
106
    }
107
108
    private function getBucketHostsFromCache($key)
109
    {
110
        $ret = array();
111
        if (count($this->hostCache) === 0) {
112
            return $ret;
113
        }
114
115
        if (!array_key_exists($key, $this->hostCache)) {
116
            return $ret;
117
        }
118
119
        if ($this->hostCache[$key]['deadline'] > time()) {
120
            $ret = $this->hostCache[$key];
121
        }
122
123
        return $ret;
124
    }
125
126
    private function setBucketHostsToCache($key, $val)
127
    {
128
        $this->hostCache[$key] = $val;
129
        return;
130
    }
131
132
    /*  请求包:
133
     *   GET /v1/query?ak=<ak>&&bucket=<bucket>
134
     *  返回包:
135
     *  
136
     *  200 OK {
137
     *    "ttl": <ttl>,              // 有效时间
138
     *    "http": {
139
     *      "up": [],
140
     *      "io": [],                // 当bucket为global时,我们不需要iohost, io缺省
141
     *    },
142
     *    "https": {
143
     *      "up": [],
144
     *      "io": [],                // 当bucket为global时,我们不需要iohost, io缺省
145
     *    }
146
     *  }
147
     **/
148
    private function bucketHosts($ak, $bucket)
149
    {
150
        $url = Config::UC_HOST . '/v1/query' . "?ak=$ak&bucket=$bucket";
151
        $ret = Client::Get($url);
152
        if (!$ret->ok()) {
153
            return array(null, new Error($url, $ret));
154
        }
155
        $r = ($ret->body === null) ? array() : $ret->json();
156
        return array($r, null);
157
    }
158
}
159