Completed
Pull Request — master (#260)
by
unknown
08:09
created

Zone::queryZone()   C

Complexity

Conditions 11
Paths 41

Size

Total Lines 53
Code Lines 40

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 88.9333

Importance

Changes 0
Metric Value
cc 11
eloc 40
nc 41
nop 2
dl 0
loc 53
ccs 6
cts 44
cp 0.1364
crap 88.9333
rs 6.2926
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
namespace Qiniu;
3
4
use Qiniu\Http\Client;
5
use Qiniu\Http\Error;
6
7
final class Zone
8
{
9
10
    //源站上传域名
11
    public $srcUpHosts;
12
    //CDN加速上传域名
13
    public $cdnUpHosts;
14
    //资源管理域名
15
    public $rsHost;
16
    //资源列举域名
17
    public $rsfHost;
18
    //资源处理域名
19
    public $apiHost;
20
    //IOVIP域名
21
    public $iovipHost;
22
23
    //构造一个Zone对象
24 30
    public function __construct(
25
        $srcUpHosts = array(),
26
        $cdnUpHosts = array(),
27
        $rsHost = "rs.qiniu.com",
28
        $rsfHost = "rsf.qiniu.com",
29
        $apiHost = "api.qiniu.com",
30
        $iovipHost = null
31
    ) {
32
33 30
        $this->srcUpHosts = $srcUpHosts;
34 30
        $this->cdnUpHosts = $cdnUpHosts;
35 30
        $this->rsHost = $rsHost;
36 30
        $this->rsfHost = $rsfHost;
37 30
        $this->apiHost = $apiHost;
38 30
        $this->iovipHost = $iovipHost;
39 30
    }
40
41
    //华东机房
42
    public static function zone0()
43
    {
44
        $Zone_z0 = new Zone(
45
            array("up.qiniup.com", 'up-nb.qiniup.com', 'up-xs.qiniup.com'),
46
            array('upload.qiniup.com', 'upload-nb.qiniup.com', 'upload-xs.qiniup.com'),
47
            'rs.qiniu.com',
48
            'rsf.qiniu.com',
49
            'api.qiniu.com',
50
            'iovip.qbox.me'
51
        );
52
        return $Zone_z0;
53
    }
54
55
    //华北机房
56
    public static function zone1()
57
    {
58
        $Zone_z1 = new Zone(
59
            array('up-z1.qiniup.com'),
60
            array('upload-z1.qiniup.com'),
61
            "rs-z1.qiniu.com",
62
            "rsf-z1.qiniu.com",
63
            "api-z1.qiniu.com",
64
            "iovip-z1.qbox.me"
65
        );
66
67
        return $Zone_z1;
68
    }
69
70
    //华南机房
71
    public static function zone2()
72
    {
73
        $Zone_z2 = new Zone(
74
            array('up-z2.qiniup.com', 'up-gz.qiniup.com', 'up-fs.qiniup.com'),
75
            array('upload-z2.qiniup.com', 'upload-gz.qiniup.com', 'upload-fs.qiniup.com'),
76
            "rs-z2.qiniu.com",
77
            "rsf-z2.qiniu.com",
78
            "api-z2.qiniu.com",
79
            "iovip-z2.qbox.me"
80
        );
81
        return $Zone_z2;
82
    }
83
84
    //北美机房
85
    public static function zoneNa0()
86
    {
87
        //北美机房
88
        $Zone_na0 = new Zone(
89
            array('up-na0.qiniup.com'),
90
            array('upload-na0.qiniup.com'),
91
            "rs-na0.qiniu.com",
92
            "rsf-na0.qiniu.com",
93
            "api-na0.qiniu.com",
94
            "iovip-na0.qbox.me"
95
        );
96
        return $Zone_na0;
97
    }
98
99
    //新加坡机房
100
    public static function zoneAs0()
101
    {
102
        //新加坡机房
103
        $Zone_na0 = new Zone(
0 ignored issues
show
Unused Code introduced by
$Zone_na0 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...
104
            array('up-as0.qiniup.com'),
105
            array('upload-as0.qiniup.com'),
106
            "rs-as0.qiniu.com",
107
            "rsf-as0.qiniu.com",
108
            "api-as0.qiniu.com",
109
            "iovip-as0.qbox.me"
110
        );
111
        return $Zone_as0;
0 ignored issues
show
Bug introduced by
The variable $Zone_as0 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...
112
    }
113
114
    /*
115
     * GET /v2/query?ak=<ak>&&bucket=<bucket>
116
     **/
117 27
    public static function queryZone($ak, $bucket)
118
    {
119 27
        $zone = new Zone();
120 27
        $url = Config::UC_HOST . '/v2/query' . "?ak=$ak&bucket=$bucket";
121 27
        $ret = Client::Get($url);
122 27
        if (!$ret->ok()) {
123 27
            return array(null, new Error($url, $ret));
124
        }
125
        $r = ($ret->body === null) ? array() : $ret->json();
126
        //parse zone;
127
128
        $iovipHost = $r['io']['src']['main'][0];
129
        $zone->iovipHost = $iovipHost;
130
        $accMain = $r['up']['acc']['main'][0];
131
        array_push($zone->cdnUpHosts, $accMain);
132
        if (isset($r['up']['acc']['backup'])) {
133
            foreach ($r['up']['acc']['backup'] as $key => $value) {
134
                array_push($zone->cdnUpHosts, $value);
135
            }
136
        }
137
        $srcMain = $r['up']['src']['main'][0];
138
        array_push($zone->srcUpHosts, $srcMain);
139
        if (isset($r['up']['src']['backup'])) {
140
            foreach ($r['up']['src']['backup'] as $key => $value) {
141
                array_push($zone->srcUpHosts, $value);
142
            }
143
        }
144
145
        //set specific hosts
146
        if (strstr($zone->iovipHost, "z1") !== false) {
147
            $zone->rsHost = "rs-z1.qiniu.com";
148
            $zone->rsfHost = "rsf-z1.qiniu.com";
149
            $zone->apiHost = "api-z1.qiniu.com";
150
        } elseif (strstr($zone->iovipHost, "z2") !== false) {
151
            $zone->rsHost = "rs-z2.qiniu.com";
152
            $zone->rsfHost = "rsf-z2.qiniu.com";
153
            $zone->apiHost = "api-z2.qiniu.com";
154
        } elseif (strstr($zone->iovipHost, "na0") !== false) {
155
            $zone->rsHost = "rs-na0.qiniu.com";
156
            $zone->rsfHost = "rsf-na0.qiniu.com";
157
            $zone->apiHost = "api-na0.qiniu.com";
158
        } elseif (strstr($zone->iovipHost, "as0") !== false) {
159
            $zone->rsHost = "rs-as0.qiniu.com";
160
            $zone->rsfHost = "rsf-as0.qiniu.com";
161
            $zone->apiHost = "api-as0.qiniu.com";
162
        } else {
163
            $zone->rsHost = "rs.qiniu.com";
164
            $zone->rsfHost = "rsf.qiniu.com";
165
            $zone->apiHost = "api.qiniu.com";
166
        }
167
168
        return $zone;
169
    }
170
}
171
172