Completed
Pull Request — master (#190)
by r
25:30
created

Zone::zone1()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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