Passed
Pull Request — master (#386)
by
unknown
22:40
created

Config::getUpHostV2()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 5.1576

Importance

Changes 0
Metric Value
cc 4
eloc 11
nc 5
nop 2
dl 0
loc 19
ccs 7
cts 12
cp 0.5833
crap 5.1576
rs 9.9
c 0
b 0
f 0
1
<?php
2
namespace Qiniu;
3
4
final class Config
5
{
6
    const SDK_VER = '7.6.0';
7
8
    const BLOCK_SIZE = 4194304; //4*1024*1024 分块上传块大小,该参数为接口规格,不能修改
9
10
    const RSF_HOST = 'rsf.qiniu.com';
11
    const API_HOST = 'api.qiniu.com';
12
    const RS_HOST = 'rs.qiniu.com';      //RS Host
13
    const UC_HOST = 'uc.qbox.me';              //UC Host
14
    const RTCAPI_HOST = 'http://rtc.qiniuapi.com';
15
    const ARGUS_HOST = 'ai.qiniuapi.com';
16
    const CASTER_HOST = 'pili-caster.qiniuapi.com';
17
    const SMS_HOST="https://sms.qiniuapi.com";
18
    const RTCAPI_VERSION = 'v3';
19
    const SMS_VERSION='v1';
20
21
    // Zone 空间对应的存储区域
22
    public $region;
23
    //BOOL 是否使用https域名
24
    public $useHTTPS;
25
    //BOOL 是否使用CDN加速上传域名
26
    public $useCdnDomains;
27
    // Zone Cache
28
    private $regionCache;
29
30
    // 构造函数
31 69
    public function __construct(Region $z = null)
32
    {
33 69
        $this->zone = $z;
0 ignored issues
show
Bug Best Practice introduced by
The property zone does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
34 69
        $this->useHTTPS = false;
35 69
        $this->useCdnDomains = false;
36 69
        $this->regionCache = array();
37 69
    }
38
39 18
    public function getUpHost($accessKey, $bucket)
40
    {
41 18
        $region = $this->getRegion($accessKey, $bucket);
42 18
        if ($this->useHTTPS === true) {
43
            $scheme = "https://";
44
        } else {
45 18
            $scheme = "http://";
46
        }
47
48 18
        $host = $region->srcUpHosts[0];
49 18
        if ($this->useCdnDomains === true) {
50
            $host = $region->cdnUpHosts[0];
51
        }
52
53 18
        return $scheme . $host;
54
    }
55
56 3
    public function getUpHostV2($accessKey, $bucket)
57
    {
58 3
        list($region, $err) = $this->getRegionV2($accessKey, $bucket);
59 3
        if ($err != null) {
60
            return array(null, $err);
61
        }
62 3
63
        if ($this->useHTTPS === true) {
64
            $scheme = "https://";
65 3
        } else {
66 3
            $scheme = "http://";
67
        }
68
69
        $host = $region->srcUpHosts[0];
70 3
        if ($this->useCdnDomains === true) {
71
            $host = $region->cdnUpHosts[0];
72
        }
73
74
        return array($scheme . $host, null);
75
    }
76
77
    public function getUpBackupHost($accessKey, $bucket)
78
    {
79
        $region = $this->getRegion($accessKey, $bucket);
80
        if ($this->useHTTPS === true) {
81
            $scheme = "https://";
82
        } else {
83
            $scheme = "http://";
84
        }
85
86
        $host = $region->cdnUpHosts[0];
87
        if ($this->useCdnDomains === true) {
88
            $host = $region->srcUpHosts[0];
89
        }
90
91
        return $scheme . $host;
92
    }
93
94
    public function getUpBackupHostV2($accessKey, $bucket)
95
    {
96
        list($region, $err) = $this->getRegionV2($accessKey, $bucket);
97
        if ($err != null) {
98
            return array(null, $err);
99 6
        }
100
101 6
        if ($this->useHTTPS === true) {
102
            $scheme = "https://";
103 6
        } else {
104
            $scheme = "http://";
105
        }
106 6
107
        $host = $region->cdnUpHosts[0];
108
        if ($this->useCdnDomains === true) {
109 6
            $host = $region->srcUpHosts[0];
110
        }
111
112
        return array($scheme . $host, null);
113
    }
114
115
    public function getRsHost($accessKey, $bucket)
116
    {
117
        $region = $this->getRegion($accessKey, $bucket);
118
119
        if ($this->useHTTPS === true) {
120
            $scheme = "https://";
121
        } else {
122
            $scheme = "http://";
123
        }
124
125 24
        return $scheme . $region->rsHost;
126
    }
127 24
128
    public function getRsHostV2($accessKey, $bucket)
129 24
    {
130 6
        list($region, $err) = $this->getRegionV2($accessKey, $bucket);
131 24
        if ($err != null) {
132 3
            return array(null, $err);
133 3
        }
134 3
135 21
        if ($this->useHTTPS === true) {
136 21
            $scheme = "https://";
137
        } else {
138 24
            $scheme = "http://";
139
        }
140
141
        return array($scheme . $region->rsHost, null);
142
    }
143
144
    public function getRsfHost($accessKey, $bucket)
145
    {
146
        $region = $this->getRegion($accessKey, $bucket);
147
148
        if ($this->useHTTPS === true) {
149
            $scheme = "https://";
150
        } else {
151
            $scheme = "http://";
152
        }
153
154
        return $scheme . $region->rsfHost;
155
    }
156
157
    public function getRsfHostV2($accessKey, $bucket)
158
    {
159
        list($region, $err) = $this->getRegionV2($accessKey, $bucket);
160
        if ($err != null) {
161
            return array(null, $err);
162
        }
163
164
        if ($this->useHTTPS === true) {
165
            $scheme = "https://";
166
        } else {
167
            $scheme = "http://";
168
        }
169
170
        return array($scheme . $region->rsfHost, null);
171
    }
172
173
    public function getIovipHost($accessKey, $bucket)
174
    {
175
        $region = $this->getRegion($accessKey, $bucket);
176
177
        if ($this->useHTTPS === true) {
178
            $scheme = "https://";
179
        } else {
180
            $scheme = "http://";
181
        }
182
183
        return $scheme . $region->iovipHost;
184
    }
185
186
    public function getIovipHostV2($accessKey, $bucket)
187
    {
188
        list($region, $err) = $this->getRegionV2($accessKey, $bucket);
189
        if ($err != null) {
190
            return array(null, $err);
191
        }
192
193
        if ($this->useHTTPS === true) {
194
            $scheme = "https://";
195
        } else {
196
            $scheme = "http://";
197
        }
198
199
        return array($scheme . $region->iovipHost, null);
200
    }
201
202
    public function getApiHost($accessKey, $bucket)
203
    {
204
        $region = $this->getRegion($accessKey, $bucket);
205
206
        if ($this->useHTTPS === true) {
207
            $scheme = "https://";
208
        } else {
209
            $scheme = "http://";
210
        }
211
212
        return $scheme . $region->apiHost;
213
    }
214
215
    public function getApiHostV2($accessKey, $bucket)
216
    {
217
        list($region, $err) = $this->getRegionV2($accessKey, $bucket);
218
        if ($err != null) {
219
            return array(null, $err);
220
        }
221
222
        if ($this->useHTTPS === true) {
223
            $scheme = "https://";
224
        } else {
225
            $scheme = "http://";
226
        }
227
228
        return array($scheme . $region->apiHost, null);
229
    }
230
231
    private function getRegion($accessKey, $bucket)
232
    {
233
        $cacheId = "$accessKey:$bucket";
234
235
        if (isset($this->regionCache[$cacheId])) {
236
            $region = $this->regionCache[$cacheId];
237
        } elseif (isset($this->zone)) {
238
            $region = $this->zone;
239
            $this->regionCache[$cacheId] = $region;
240
        } else {
241
            $region = Zone::queryZone($accessKey, $bucket);
242
            if (is_array($region)) {
243
                list($region, $err) = $region;
244
                if ($err != null) {
245
                    throw new \Exception($err->message());
246
                }
247
            }
248
            $this->regionCache[$cacheId] = $region;
249
        }
250
        return $region;
251
    }
252
253
    private function getRegionV2($accessKey, $bucket)
254
    {
255
        $cacheId = "$accessKey:$bucket";
256
257
        if (isset($this->regionCache[$cacheId])) {
258
            $region = $this->regionCache[$cacheId];
259
        } elseif (isset($this->zone)) {
260
            $region = $this->zone;
261
            $this->regionCache[$cacheId] = $region;
262
        } else {
263
            $region = Zone::queryZone($accessKey, $bucket);
264
            if (is_array($region)) {
265
                list($region, $err) = $region;
266
                return array($region, $err);
267
            }
268
            $this->regionCache[$cacheId] = $region;
269
        }
270
271
        return array($region, null);
272
    }
273
}
274