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

Zone::getBucketHosts()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 24
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 13
nc 6
nop 2
dl 0
loc 24
ccs 0
cts 0
cp 0
crap 20
rs 8.6845
c 1
b 0
f 0
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
        $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
        $upHosts = $this->getUpHosts($ak, $bucket);
36
        return $upHosts[1];
37
    }
38
39
    public function getUpHosts($ak, $bucket)
40
    {
41
        $bucketHosts = $this->getBucketHosts($ak, $bucket);
42
        $upHosts = $bucketHosts['upHosts'];
43
        return $upHosts;
44
    }
45
46
    public function getBucketHostsByUpToken($uptoken) 
47
    {
48
        list($ak, $bucket) = $this->unmarshalUpToken($uptoken);
49
        return $this->getBucketHosts($ak, $bucket);
50
    }
51
52
    private function unmarshalUpToken($uptoken)
53
    {
54
        $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...
55
        if (count($token) !== 3)
56
        {
57
            throw new \Exception("Invalid Uptoken", 1);
58
        }
59
60
        $ak = $token[0];
61
        $policy = base64_urlSafeDecode($token[2]);
62
        $policy = json_decode($policy, true);
63
64
        list($bucket, $_) = split(':', $policy['scope']);
0 ignored issues
show
Unused Code introduced by
The assignment to $_ is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
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...
65
         
66
        return array($ak, $bucket);
67
    }
68
69
    public function getBucketHosts($ak, $bucket)
70
    {
71
        $key = $ak . $bucket;
72
73
        $exist = false;
74
        if (count($this->hostCache) > 0) 
75
        {
76
            $exist = array_key_exists($key, $this->hostCache) && $this->hostCache[$key]['deadline'] > time();
77
        }
78
79
        if ($exist) 
80
        {
81
            return $this->hostCache[$key];
82
        }
83
84
        list($hosts, $_) = $this->bucketHosts($ak, $bucket);
0 ignored issues
show
Unused Code introduced by
The assignment to $_ is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
85
86
        var_dump($hosts);
0 ignored issues
show
Security Debugging Code introduced by
var_dump($hosts); looks like debug code. Are you sure you do not want to remove it? This might expose sensitive data.
Loading history...
87
        $schemeHosts = $hosts[$this->scheme];
88
        $bucketHosts = array('upHosts' => $schemeHosts['up'], 'ioHost' => $schemeHosts['io'], 'deadline' => time() + $hosts['ttl']);
89
90
        $this->hostCache[$key] = $bucketHosts;
91
        return $bucketHosts;
92
    }
93
94
95
    /*  请求包:
96
     *   GET /v1/query?ak=<ak>&&bucket=<bucket>
97
     *  返回包:
98
     *  
99
     *  200 OK {
100
     *    "ttl": <ttl>,              // 有效时间
101
     *    "http": {
102
     *      "up": [],
103
     *      "io": [],                // 当bucket为global时,我们不需要iohost, io缺省
104
     *    },
105
     *    "https": {
106
     *      "up": [],
107
     *      "io": [],                // 当bucket为global时,我们不需要iohost, io缺省
108
     *    }
109
     *  }
110
     **/
111
    private function bucketHosts($ak, $bucket) 
112
    {
113
        $path = '/v1/query' . "?ak=$ak&bucket=$bucket";
114
        $ret = Client::Get(Config::UC_HOST . $path);
115
        if (!$ret->ok()) {
116
            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...
117
        }
118
        $r = ($ret->body === null) ? array() : $ret->json();
119
        return array($r, null);
120
    }
121
}
122