Completed
Push — master ( 0b34e6...57deed )
by
unknown
17s queued 15s
created

Config::getUcHost()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.032

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 0
dl 0
loc 9
ccs 4
cts 5
cp 0.8
crap 2.032
rs 10
c 0
b 0
f 0
1
<?php
2
namespace Qiniu;
3
4
final class Config
5
{
6
    const SDK_VER = '7.8.0';
7
8
    const BLOCK_SIZE = 4194304; //4*1024*1024 分块上传块大小,该参数为接口规格,不能修改
9
10
    const RSF_HOST = 'rsf.qiniuapi.com';
11
    const API_HOST = 'api.qiniuapi.com';
12
    const RS_HOST = 'rs.qiniuapi.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
    /**
28
     * @var Region
29
     */
30
    public $zone;
31 69
    // Zone Cache
32
    private $regionCache;
33 69
    // UC Host
34 69
    private $ucHost;
35 69
36 69
    // 构造函数
37 69
    public function __construct(Region $z = null)
38
    {
39 18
        $this->zone = $z;
40
        $this->useHTTPS = false;
41 18
        $this->useCdnDomains = false;
42 18
        $this->regionCache = array();
43
        $this->ucHost = Config::UC_HOST;
44
    }
45 18
46
    public function setUcHost($ucHost)
47
    {
48 18
        $this->ucHost = $ucHost;
49 18
    }
50
51
    public function getUcHost()
52
    {
53 18
        if ($this->useHTTPS === true) {
54
            $scheme = "https://";
55
        } else {
56 3
            $scheme = "http://";
57
        }
58 3
59 3
        return $scheme . $this->ucHost;
60
    }
61
62 3
    public function getUpHost($accessKey, $bucket)
63
    {
64
        $region = $this->getRegion($accessKey, $bucket);
65 3
        if ($this->useHTTPS === true) {
66 3
            $scheme = "https://";
67
        } else {
68
            $scheme = "http://";
69
        }
70 3
71
        $host = $region->srcUpHosts[0];
72
        if ($this->useCdnDomains === true) {
73
            $host = $region->cdnUpHosts[0];
74
        }
75
76
        return $scheme . $host;
77
    }
78
79
    public function getUpHostV2($accessKey, $bucket)
80
    {
81
        list($region, $err) = $this->getRegionV2($accessKey, $bucket);
82
        if ($err != null) {
83
            return array(null, $err);
84
        }
85
86
        if ($this->useHTTPS === true) {
87
            $scheme = "https://";
88
        } else {
89
            $scheme = "http://";
90
        }
91
92
        $host = $region->srcUpHosts[0];
93
        if ($this->useCdnDomains === true) {
94
            $host = $region->cdnUpHosts[0];
95
        }
96
97
        return array($scheme . $host, null);
98
    }
99 6
100
    public function getUpBackupHost($accessKey, $bucket)
101 6
    {
102
        $region = $this->getRegion($accessKey, $bucket);
103 6
        if ($this->useHTTPS === true) {
104
            $scheme = "https://";
105
        } else {
106 6
            $scheme = "http://";
107
        }
108
109 6
        $host = $region->cdnUpHosts[0];
110
        if ($this->useCdnDomains === true) {
111
            $host = $region->srcUpHosts[0];
112
        }
113
114
        return $scheme . $host;
115
    }
116
117
    public function getUpBackupHostV2($accessKey, $bucket)
118
    {
119
        list($region, $err) = $this->getRegionV2($accessKey, $bucket);
120
        if ($err != null) {
121
            return array(null, $err);
122
        }
123
124
        if ($this->useHTTPS === true) {
125 24
            $scheme = "https://";
126
        } else {
127 24
            $scheme = "http://";
128
        }
129 24
130 6
        $host = $region->cdnUpHosts[0];
131 24
        if ($this->useCdnDomains === true) {
132 3
            $host = $region->srcUpHosts[0];
133 3
        }
134 3
135 21
        return array($scheme . $host, null);
136 21
    }
137
138 24
    public function getRsHost($accessKey, $bucket)
139
    {
140
        $region = $this->getRegion($accessKey, $bucket);
141
142
        if ($this->useHTTPS === true) {
143
            $scheme = "https://";
144
        } else {
145
            $scheme = "http://";
146
        }
147
148
        return $scheme . $region->rsHost;
149
    }
150
151
    public function getRsHostV2($accessKey, $bucket)
152
    {
153
        list($region, $err) = $this->getRegionV2($accessKey, $bucket);
154
        if ($err != null) {
155
            return array(null, $err);
156
        }
157
158
        if ($this->useHTTPS === true) {
159
            $scheme = "https://";
160
        } else {
161
            $scheme = "http://";
162
        }
163
164
        return array($scheme . $region->rsHost, null);
165
    }
166
167
    public function getRsfHost($accessKey, $bucket)
168
    {
169
        $region = $this->getRegion($accessKey, $bucket);
170
171
        if ($this->useHTTPS === true) {
172
            $scheme = "https://";
173
        } else {
174
            $scheme = "http://";
175
        }
176
177
        return $scheme . $region->rsfHost;
178
    }
179
180
    public function getRsfHostV2($accessKey, $bucket)
181
    {
182
        list($region, $err) = $this->getRegionV2($accessKey, $bucket);
183
        if ($err != null) {
184
            return array(null, $err);
185
        }
186
187
        if ($this->useHTTPS === true) {
188
            $scheme = "https://";
189
        } else {
190
            $scheme = "http://";
191
        }
192
193
        return array($scheme . $region->rsfHost, null);
194
    }
195
196
    public function getIovipHost($accessKey, $bucket)
197
    {
198
        $region = $this->getRegion($accessKey, $bucket);
199
200
        if ($this->useHTTPS === true) {
201
            $scheme = "https://";
202
        } else {
203
            $scheme = "http://";
204
        }
205
206
        return $scheme . $region->iovipHost;
207
    }
208
209
    public function getIovipHostV2($accessKey, $bucket)
210
    {
211
        list($region, $err) = $this->getRegionV2($accessKey, $bucket);
212
        if ($err != null) {
213
            return array(null, $err);
214
        }
215
216
        if ($this->useHTTPS === true) {
217
            $scheme = "https://";
218
        } else {
219
            $scheme = "http://";
220
        }
221
222
        return array($scheme . $region->iovipHost, null);
223
    }
224
225
    public function getApiHost($accessKey, $bucket)
226
    {
227
        $region = $this->getRegion($accessKey, $bucket);
228
229
        if ($this->useHTTPS === true) {
230
            $scheme = "https://";
231
        } else {
232
            $scheme = "http://";
233
        }
234
235
        return $scheme . $region->apiHost;
236
    }
237
238
    public function getApiHostV2($accessKey, $bucket)
239
    {
240
        list($region, $err) = $this->getRegionV2($accessKey, $bucket);
241
        if ($err != null) {
242
            return array(null, $err);
243
        }
244
245
        if ($this->useHTTPS === true) {
246
            $scheme = "https://";
247
        } else {
248
            $scheme = "http://";
249
        }
250
251
        return array($scheme . $region->apiHost, null);
252
    }
253
254
255
    /**
256
     * 从缓存中获取区域
257
     *
258
     * @param string $cacheId 缓存 ID
259
     * @return null|Region
260
     */
261
    private function getRegionCache($cacheId)
262
    {
263
        if (isset($this->regionCache[$cacheId]) &&
264
            isset($this->regionCache[$cacheId]["deadline"]) &&
265
            time() < $this->regionCache[$cacheId]["deadline"]
266
        ) {
267
            return $this->regionCache[$cacheId]["region"];
268
        }
269
270
        return null;
271
    }
272
273
    /**
274
     * 将区域设置到缓存中
275
     *
276
     * @param string $cacheId 缓存 ID
277
     * @param Region $region 缓存 ID
278
     * @return void
279
     */
280
    private function setRegionCache($cacheId, $region)
281
    {
282
        $this->regionCache[$cacheId] = array(
283
            "region" => $region,
284
        );
285
        if (isset($region->ttl)) {
286
            $this->regionCache[$cacheId]["deadline"] = time() + $region->ttl;
287
        }
288
    }
289
290
    /**
291
     * 从缓存中获取区域
292
     *
293
     * @param string $accessKey
294
     * @param string $bucket
295
     * @return Region
296
     *
297
     * @throws \Exception
298
     */
299
    private function getRegion($accessKey, $bucket)
300
    {
301
        if (isset($this->zone)) {
302
            return $this->zone;
303
        }
304
305
        $cacheId = "$accessKey:$bucket";
306
        $regionCache = $this->getRegionCache($cacheId);
307
        if ($regionCache) {
308
            return $regionCache;
309
        }
310
311
        $region = Zone::queryZone($accessKey, $bucket, $this->getUcHost());
312
        if (is_array($region)) {
313
            list($region, $err) = $region;
314
            if ($err != null) {
315
                throw new \Exception($err->message());
316
            }
317
        }
318
319
        $this->setRegionCache($cacheId, $region);
320
        return $region;
321
    }
322
323
    private function getRegionV2($accessKey, $bucket)
324
    {
325
        if (isset($this->zone)) {
326
            return array($this->zone, null);
327
        }
328
329
        $cacheId = "$accessKey:$bucket";
330
        $regionCache = $this->getRegionCache($cacheId);
331
        if (isset($regionCache)) {
332
            return array($regionCache, null);
333
        }
334
335
        $region = Zone::queryZone($accessKey, $bucket, $this->getUcHost());
336
        if (is_array($region)) {
337
            list($region, $err) = $region;
338
            return array($region, $err);
339
        }
340
341
        $this->setRegionCache($cacheId, $region);
342
        return array($region, null);
343
    }
344
}
345