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

Zone::getBucketHosts()   B

Complexity

Conditions 5
Paths 9

Size

Total Lines 27
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 5
eloc 14
c 2
b 0
f 0
nc 9
nop 2
dl 0
loc 27
ccs 0
cts 0
cp 0
crap 30
rs 8.439
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
    public $hostCache; //<scheme>:<ak>:<bucket> ==> array('deadline' => 'xxx', 'upHosts' => array(), 'ioHost' => 'xxx.com')
14
    public $scheme = 'http';
15 15
16
    public function __construct($scheme = null)
17 15
    {
18
        $this->hostCache = array();
19
        if ($scheme != null) 
20
        {
21
            $this->scheme = $scheme;
22
        }
23
    }
24
25
    public function getUpHostByToken($uptoken)
26
    {
27
        list($ak, $bucket) = $this->unmarshalUpToken($uptoken);
28
        list($upHosts,) = $this->getUpHosts($ak, $bucket);
29
        return $upHosts[0];
30
    }
31
32
    public function getBackupUpHostByToken($uptoken)
33
    {
34
        list($ak, $bucket) = $this->unmarshalUpToken($uptoken);
35
        list($upHosts,) = $this->getUpHosts($ak, $bucket);
36
        return $upHosts[1];
37
    }
38
39
    public function getIoHost($ak, $bucket)
40
    {
41
        list($bucketHosts, ) = $this->getBucketHosts($ak, $bucket);
42
        $ioHost = $bucketHosts['ioHost'];
0 ignored issues
show
Unused Code introduced by
$ioHost is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
43
        return $upHost;
0 ignored issues
show
Bug introduced by
The variable $upHost does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
44
    }
45
46
    public function getUpHosts($ak, $bucket)
47
    {
48
        list($bucketHosts, $err) = $this->getBucketHosts($ak, $bucket);
49
        if ($err !== null)
50
        {
51
            return array(null, $err);
52
        }
53
54
        $upHosts = $bucketHosts['upHosts'];
55
        return array($upHosts, null);
56
    }
57
58
    private function unmarshalUpToken($uptoken)
59
    {
60
        $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...
61
        if (count($token) !== 3)
62
        {
63
            throw new \Exception("Invalid Uptoken", 1);
64
        }
65
66
        $ak = $token[0];
67
        $policy = base64_urlSafeDecode($token[2]);
68
        $policy = json_decode($policy, true);
69
70
        $bucket = $policy['scope'];
71
        if (strpos($bucket, ':'))
72
        {
73
            $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...
74
        }
75
         
76
        return array($ak, $bucket);
77
    }
78
79
    public function getBucketHosts($ak, $bucket)
80
    {
81
        $key = $ak . $bucket;
82
83
        $exist = false;
84
        if (count($this->hostCache) > 0) 
85
        {
86
            $exist = array_key_exists($key, $this->hostCache) && $this->hostCache[$key]['deadline'] > time();
87
        }
88
89
        if ($exist) 
90
        {
91
            return $this->hostCache[$key];
92
        }
93
94
        list($hosts, $err) = $this->bucketHosts($ak, $bucket);
95
        if ($err !== null) 
96
        {
97
            return array(null , $err);
98
        }
99
100
        $schemeHosts = $hosts[$this->scheme];
101
        $bucketHosts = array('upHosts' => $schemeHosts['up'], 'ioHost' => $schemeHosts['io'], 'deadline' => time() + $hosts['ttl']);
102
103
        $this->hostCache[$key] = $bucketHosts;
104
        return array($bucketHosts, null);
105
    }
106
107
108
    /*  请求包:
109
     *   GET /v1/query?ak=<ak>&&bucket=<bucket>
110
     *  返回包:
111
     *  
112
     *  200 OK {
113
     *    "ttl": <ttl>,              // 有效时间
114
     *    "http": {
115
     *      "up": [],
116
     *      "io": [],                // 当bucket为global时,我们不需要iohost, io缺省
117
     *    },
118
     *    "https": {
119
     *      "up": [],
120
     *      "io": [],                // 当bucket为global时,我们不需要iohost, io缺省
121
     *    }
122
     *  }
123
     **/
124
    private function bucketHosts($ak, $bucket) 
125
    {
126
        $path = '/v1/query' . "?ak=$ak&bucket=$bucket";
127
        $ret = Client::Get(Config::UC_HOST . $path);
128
        if (!$ret->ok()) {
129
            return array(null, new Error($url, $ret));
0 ignored issues
show
Bug introduced by
The variable $url does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
130
        }
131
        $r = ($ret->body === null) ? array() : $ret->json();
132
        return array($r, null);
133
    }
134
}
135