Completed
Push — master ( a678c2...6277d2 )
by
unknown
21s queued 14s
created

Config::prependBackupUcHosts()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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