Completed
Pull Request — master (#288)
by Bai
13:51
created

Zone::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 6
dl 0
loc 16
ccs 8
cts 8
cp 1
crap 1
rs 9.7333
c 0
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
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-jjh.qiniup.com', 'up-xs.qiniup.com'),
46
            array('upload.qiniup.com', 'upload-jjh.qiniup.com', 'upload-xs.qiniup.com'),
47
            'rs.qbox.me',
48
            'rsf.qbox.me',
49
            'api.qiniu.com',
50
            'iovip.qbox.me'
51
        );
52
        return $Zone_z0;
53
    }
54
55
    //华东机房内网上传
56
    public static function zoneZ0()
57
    {
58
        $Zone_z01 = new Zone(
59
            array("free-qvm-z0-xs.qiniup.com"),
60
            'rs.qbox.me',
0 ignored issues
show
Documentation introduced by
'rs.qbox.me' is of type string, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
61
            'rsf.qbox.me',
62
            'api.qiniu.com',
63
            'iovip.qbox.me'
64
        );
65
        return $Zone_z01;
66
    }
67
68
    //华北机房内网上传
69
    public static function zoneZ1()
70
    {
71
        $Zone_z12 = new Zone(
72
            array("free-qvm-z1-zz.qiniup.com"),
73
            "rs-z1.qbox.me",
0 ignored issues
show
Documentation introduced by
'rs-z1.qbox.me' is of type string, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
74
            "rsf-z1.qbox.me",
75
            "api-z1.qiniu.com",
76
            "iovip-z1.qbox.me"
77
        );
78
        return $Zone_z12;
79
    }
80
81
    //华北机房
82
    public static function zone1()
83
    {
84
        $Zone_z1 = new Zone(
85
            array('up-z1.qiniup.com'),
86
            array('upload-z1.qiniup.com'),
87
            "rs-z1.qbox.me",
88
            "rsf-z1.qbox.me",
89
            "api-z1.qiniu.com",
90
            "iovip-z1.qbox.me"
91
        );
92
93
        return $Zone_z1;
94
    }
95
96
    //华南机房
97
    public static function zone2()
98
    {
99
        $Zone_z2 = new Zone(
100
            array('up-z2.qiniup.com', 'up-dg.qiniup.com', 'up-fs.qiniup.com'),
101
            array('upload-z2.qiniup.com', 'upload-dg.qiniup.com', 'upload-fs.qiniup.com'),
102
            "rs-z2.qbox.me",
103
            "rsf-z2.qbox.me",
104
            "api-z2.qiniu.com",
105
            "iovip-z2.qbox.me"
106
        );
107
        return $Zone_z2;
108
    }
109
110
    //北美机房
111
    public static function zoneNa0()
112
    {
113
        //北美机房
114
        $Zone_na0 = new Zone(
115
            array('up-na0.qiniup.com'),
116
            array('upload-na0.qiniup.com'),
117
            "rs-na0.qbox.me",
118
            "rsf-na0.qbox.me",
119
            "api-na0.qiniu.com",
120
            "iovip-na0.qbox.me"
121
        );
122
        return $Zone_na0;
123
    }
124
125
    //新加坡机房
126
    public static function zoneAs0()
127
    {
128
        //新加坡机房
129
        $Zone_as0 = new Zone(
130
            array('up-as0.qiniup.com'),
131
            array('upload-as0.qiniup.com'),
132
            "rs-as0.qbox.me",
133
            "rsf-as0.qbox.me",
134
            "api-as0.qiniu.com",
135
            "iovip-as0.qbox.me"
136
        );
137
        return $Zone_as0;
138
    }
139
140
    /*
141
     * GET /v2/query?ak=<ak>&&bucket=<bucket>
142
     **/
143 27
    public static function queryZone($ak, $bucket)
144
    {
145 27
        $zone = new Zone();
146 27
        $url = Config::UC_HOST . '/v2/query' . "?ak=$ak&bucket=$bucket";
147 27
        $ret = Client::Get($url);
148 27
        if (!$ret->ok()) {
149 27
            return array(null, new Error($url, $ret));
150
        }
151
        $r = ($ret->body === null) ? array() : $ret->json();
152
        //print_r($ret);
153
        //parse zone;
154
155
        $iovipHost = $r['io']['src']['main'][0];
156
        $zone->iovipHost = $iovipHost;
157
        $accMain = $r['up']['acc']['main'][0];
158
        array_push($zone->cdnUpHosts, $accMain);
159
        if (isset($r['up']['acc']['backup'])) {
160
            foreach ($r['up']['acc']['backup'] as $key => $value) {
161
                array_push($zone->cdnUpHosts, $value);
162
            }
163
        }
164
        $srcMain = $r['up']['src']['main'][0];
165
        array_push($zone->srcUpHosts, $srcMain);
166
        if (isset($r['up']['src']['backup'])) {
167
            foreach ($r['up']['src']['backup'] as $key => $value) {
168
                array_push($zone->srcUpHosts, $value);
169
            }
170
        }
171
172
        //set specific hosts
173
        if (strstr($zone->iovipHost, "z1") !== false) {
174
            $zone->rsHost = "rs-z1.qbox.me";
175
            $zone->rsfHost = "rsf-z1.qbox.me";
176
            $zone->apiHost = "api-z1.qiniu.com";
177
        } elseif (strstr($zone->iovipHost, "z2") !== false) {
178
            $zone->rsHost = "rs-z2.qbox.me";
179
            $zone->rsfHost = "rsf-z2.qbox.me";
180
            $zone->apiHost = "api-z2.qiniu.com";
181
        } elseif (strstr($zone->iovipHost, "na0") !== false) {
182
            $zone->rsHost = "rs-na0.qbox.me";
183
            $zone->rsfHost = "rsf-na0.qbox.me";
184
            $zone->apiHost = "api-na0.qiniu.com";
185
        } elseif (strstr($zone->iovipHost, "as0") !== false) {
186
            $zone->rsHost = "rs-as0.qbox.me";
187
            $zone->rsfHost = "rsf-as0.qbox.me";
188
            $zone->apiHost = "api-as0.qiniu.com";
189
        } else {
190
            $zone->rsHost = "rs.qbox.me";
191
            $zone->rsfHost = "rsf.qbox.me";
192
            $zone->apiHost = "api.qiniu.com";
193
        }
194
195
        return $zone;
196
    }
197
}
198