Passed
Push — master ( f367c4...59f7b3 )
by
unknown
22:08
created

Config::getApiHost()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 2
dl 0
loc 11
ccs 0
cts 0
cp 0
crap 6
rs 10
c 0
b 0
f 0
1
<?php
2
namespace Qiniu;
3
4
final class Config
5
{
6
    const SDK_VER = '7.10.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 QUERY_REGION_HOST = 'kodo-config.qiniuapi.com';
15
    const RTCAPI_HOST = 'http://rtc.qiniuapi.com';
16
    const ARGUS_HOST = 'ai.qiniuapi.com';
17
    const CASTER_HOST = 'pili-caster.qiniuapi.com';
18
    const SMS_HOST="https://sms.qiniuapi.com";
19
    const RTCAPI_VERSION = 'v3';
20
    const SMS_VERSION='v1';
21
22
    // Zone 空间对应的存储区域
23
    public $region;
24
    //BOOL 是否使用https域名
25
    public $useHTTPS;
26
    //BOOL 是否使用CDN加速上传域名
27
    public $useCdnDomains;
28
    /**
29
     * @var Region
30
     */
31 69
    public $zone;
32
    // Zone Cache
33 69
    private $regionCache;
34 69
    // UC Host
35 69
    private $ucHost;
36 69
    private $queryRegionHost;
37 69
    // backup UC Hosts
38
    private $backupQueryRegionHosts;
39 18
    // backup UC Hosts max retry time
40
    public $backupUcHostsRetryTimes;
41 18
42 18
    // 构造函数
43
    public function __construct(Region $z = null)
44
    {
45 18
        $this->zone = $z;
46
        $this->useHTTPS = false;
47
        $this->useCdnDomains = false;
48 18
        $this->regionCache = array();
49 18
        $this->ucHost = Config::UC_HOST;
50
        $this->queryRegionHost = Config::QUERY_REGION_HOST;
51
        $this->backupQueryRegionHosts = array(
52
            "uc.qbox.me",
53 18
            "api.qiniu.com"
54
        );
55
        $this->backupUcHostsRetryTimes = 2;
56 3
    }
57
58 3
    public function setUcHost($ucHost)
59 3
    {
60
        $this->ucHost = $ucHost;
61
        $this->setQueryRegionHost($ucHost);
62 3
    }
63
64
    public function getUcHost()
65 3
    {
66 3
        if ($this->useHTTPS === true) {
67
            $scheme = "https://";
68
        } else {
69
            $scheme = "http://";
70 3
        }
71
72
        return $scheme . $this->ucHost;
73
    }
74
75
    public function setQueryRegionHost($host, $backupHosts = array())
76
    {
77
        $this->queryRegionHost = $host;
78
        $this->backupQueryRegionHosts = $backupHosts;
79
    }
80
81
    public function getQueryRegionHost()
82
    {
83
        if ($this->useHTTPS === true) {
84
            $scheme = "https://";
85
        } else {
86
            $scheme = "http://";
87
        }
88
89
        return $scheme . $this->queryRegionHost;
90
    }
91
92
    public function setBackupQueryRegionHosts($hosts = array())
93
    {
94
        $this->backupQueryRegionHosts = $hosts;
95
    }
96
97
    public function getBackupQueryRegionHosts()
98
    {
99 6
        return $this->backupQueryRegionHosts;
100
    }
101 6
102
    public function getUpHost($accessKey, $bucket)
103 6
    {
104
        $region = $this->getRegion($accessKey, $bucket);
105
        if ($this->useHTTPS === true) {
106 6
            $scheme = "https://";
107
        } else {
108
            $scheme = "http://";
109 6
        }
110
111
        $host = $region->srcUpHosts[0];
112
        if ($this->useCdnDomains === true) {
113
            $host = $region->cdnUpHosts[0];
114
        }
115
116
        return $scheme . $host;
117
    }
118
119
    public function getUpHostV2($accessKey, $bucket)
120
    {
121
        list($region, $err) = $this->getRegionV2($accessKey, $bucket);
122
        if ($err != null) {
123
            return array(null, $err);
124
        }
125 24
126
        if ($this->useHTTPS === true) {
127 24
            $scheme = "https://";
128
        } else {
129 24
            $scheme = "http://";
130 6
        }
131 24
132 3
        $host = $region->srcUpHosts[0];
133 3
        if ($this->useCdnDomains === true) {
134 3
            $host = $region->cdnUpHosts[0];
135 21
        }
136 21
137
        return array($scheme . $host, null);
138 24
    }
139
140
    public function getUpBackupHost($accessKey, $bucket)
141
    {
142
        $region = $this->getRegion($accessKey, $bucket);
143
        if ($this->useHTTPS === true) {
144
            $scheme = "https://";
145
        } else {
146
            $scheme = "http://";
147
        }
148
149
        $host = $region->cdnUpHosts[0];
150
        if ($this->useCdnDomains === true) {
151
            $host = $region->srcUpHosts[0];
152
        }
153
154
        return $scheme . $host;
155
    }
156
157
    public function getUpBackupHostV2($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
        $host = $region->cdnUpHosts[0];
171
        if ($this->useCdnDomains === true) {
172
            $host = $region->srcUpHosts[0];
173
        }
174
175
        return array($scheme . $host, null);
176
    }
177
178
    public function getRsHost($accessKey, $bucket)
179
    {
180
        $region = $this->getRegion($accessKey, $bucket);
181
182
        if ($this->useHTTPS === true) {
183
            $scheme = "https://";
184
        } else {
185
            $scheme = "http://";
186
        }
187
188
        return $scheme . $region->rsHost;
189
    }
190
191
    public function getRsHostV2($accessKey, $bucket)
192
    {
193
        list($region, $err) = $this->getRegionV2($accessKey, $bucket);
194
        if ($err != null) {
195
            return array(null, $err);
196
        }
197
198
        if ($this->useHTTPS === true) {
199
            $scheme = "https://";
200
        } else {
201
            $scheme = "http://";
202
        }
203
204
        return array($scheme . $region->rsHost, null);
205
    }
206
207
    public function getRsfHost($accessKey, $bucket)
208
    {
209
        $region = $this->getRegion($accessKey, $bucket);
210
211
        if ($this->useHTTPS === true) {
212
            $scheme = "https://";
213
        } else {
214
            $scheme = "http://";
215
        }
216
217
        return $scheme . $region->rsfHost;
218
    }
219
220
    public function getRsfHostV2($accessKey, $bucket)
221
    {
222
        list($region, $err) = $this->getRegionV2($accessKey, $bucket);
223
        if ($err != null) {
224
            return array(null, $err);
225
        }
226
227
        if ($this->useHTTPS === true) {
228
            $scheme = "https://";
229
        } else {
230
            $scheme = "http://";
231
        }
232
233
        return array($scheme . $region->rsfHost, null);
234
    }
235
236
    public function getIovipHost($accessKey, $bucket)
237
    {
238
        $region = $this->getRegion($accessKey, $bucket);
239
240
        if ($this->useHTTPS === true) {
241
            $scheme = "https://";
242
        } else {
243
            $scheme = "http://";
244
        }
245
246
        return $scheme . $region->iovipHost;
247
    }
248
249
    public function getIovipHostV2($accessKey, $bucket)
250
    {
251
        list($region, $err) = $this->getRegionV2($accessKey, $bucket);
252
        if ($err != null) {
253
            return array(null, $err);
254
        }
255
256
        if ($this->useHTTPS === true) {
257
            $scheme = "https://";
258
        } else {
259
            $scheme = "http://";
260
        }
261
262
        return array($scheme . $region->iovipHost, null);
263
    }
264
265
    public function getApiHost($accessKey, $bucket)
266
    {
267
        $region = $this->getRegion($accessKey, $bucket);
268
269
        if ($this->useHTTPS === true) {
270
            $scheme = "https://";
271
        } else {
272
            $scheme = "http://";
273
        }
274
275
        return $scheme . $region->apiHost;
276
    }
277
278
    public function getApiHostV2($accessKey, $bucket)
279
    {
280
        list($region, $err) = $this->getRegionV2($accessKey, $bucket);
281
        if ($err != null) {
282
            return array(null, $err);
283
        }
284
285
        if ($this->useHTTPS === true) {
286
            $scheme = "https://";
287
        } else {
288
            $scheme = "http://";
289
        }
290
291
        return array($scheme . $region->apiHost, null);
292
    }
293
294
295
    /**
296
     * 从缓存中获取区域
297
     *
298
     * @param string $cacheId 缓存 ID
299
     * @return null|Region
300
     */
301
    private function getRegionCache($cacheId)
302
    {
303
        if (isset($this->regionCache[$cacheId]) &&
304
            isset($this->regionCache[$cacheId]["deadline"]) &&
305
            time() < $this->regionCache[$cacheId]["deadline"]
306
        ) {
307
            return $this->regionCache[$cacheId]["region"];
308
        }
309
310
        return null;
311
    }
312
313
    /**
314
     * 将区域设置到缓存中
315
     *
316
     * @param string $cacheId 缓存 ID
317
     * @param Region $region 缓存 ID
318
     * @return void
319
     */
320
    private function setRegionCache($cacheId, $region)
321
    {
322
        $this->regionCache[$cacheId] = array(
323
            "region" => $region,
324
        );
325
        if (isset($region->ttl)) {
326
            $this->regionCache[$cacheId]["deadline"] = time() + $region->ttl;
327
        }
328
    }
329
330
    /**
331
     * 从缓存中获取区域
332
     *
333
     * @param string $accessKey
334
     * @param string $bucket
335
     * @return Region
336
     *
337
     * @throws \Exception
338
     */
339
    private function getRegion($accessKey, $bucket)
340
    {
341
        if (isset($this->zone)) {
342
            return $this->zone;
343
        }
344
345
        $cacheId = "$accessKey:$bucket";
346
        $regionCache = $this->getRegionCache($cacheId);
347
        if ($regionCache) {
348
            return $regionCache;
349
        }
350
351
        $region = Zone::queryZone(
352
            $accessKey,
353
            $bucket,
354
            $this->getQueryRegionHost(),
355
            $this->getBackupQueryRegionHosts(),
356
            $this->backupUcHostsRetryTimes
357
        );
358
        if (is_array($region)) {
359
            list($region, $err) = $region;
360
            if ($err != null) {
361
                throw new \Exception($err->message());
362
            }
363
        }
364
365
        $this->setRegionCache($cacheId, $region);
366
        return $region;
367
    }
368
369
    private function getRegionV2($accessKey, $bucket)
370
    {
371
        if (isset($this->zone)) {
372
            return array($this->zone, null);
373
        }
374
375
        $cacheId = "$accessKey:$bucket";
376
        $regionCache = $this->getRegionCache($cacheId);
377
        if (isset($regionCache)) {
378
            return array($regionCache, null);
379
        }
380
381
        $region = Zone::queryZone(
382
            $accessKey,
383
            $bucket,
384
            $this->getQueryRegionHost(),
385
            $this->getBackupQueryRegionHosts(),
386
            $this->backupUcHostsRetryTimes
387
        );
388
        if (is_array($region)) {
389
            list($region, $err) = $region;
390
            return array($region, $err);
391
        }
392
393
        $this->setRegionCache($cacheId, $region);
394
        return array($region, null);
395
    }
396
}
397