Completed
Push — master ( 09a10c...a1d7e5 )
by
unknown
08:01
created

Config::getRsHost()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 7
nc 2
nop 2
dl 0
loc 12
ccs 0
cts 7
cp 0
crap 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
namespace Qiniu;
3
4
final class Config
5
{
6
    const SDK_VER = '7.2.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 = 'https://uc.qbox.me';              //UC Host
14
15
    // Zone 空间对应的机房
16
    public $zone;
17
    //BOOL 是否使用https域名
18
    public $useHTTPS;
19
    //BOOL 是否使用CDN加速上传域名
20
    public $useCdnDomains;
21
    // Zone Cache
22
    private $zoneCache;
23
24
    // 构造函数
25 69
    public function __construct(Zone $z = null)
26
    {
27 69
        $this->zone = $z;
28 69
        $this->useHTTPS = false;
29 69
        $this->useCdnDomains = false;
30 69
        $this->zoneCache = array();
31 69
    }
32
33 18
    public function getUpHost($accessKey, $bucket)
34
    {
35 18
        $zone = $this->getZone($accessKey, $bucket);
36 18
        if ($this->useHTTPS === true) {
37
            $scheme = "https://";
38
        } else {
39 18
            $scheme = "http://";
40
        }
41
42 18
        $host = $zone->srcUpHosts[0];
43 18
        if ($this->useCdnDomains === true) {
44
            $host = $zone->cdnUpHosts[0];
45
        }
46
47 18
        return $scheme . $host;
48
    }
49
50 3
    public function getUpBackupHost($accessKey, $bucket)
51
    {
52 3
        $zone=$this->getZone($accessKey, $bucket);
53 3
        if ($this->useHTTPS === true) {
54
            $scheme = "https://";
55
        } else {
56 3
            $scheme = "http://";
57
        }
58
59 3
        $host = $zone->cdnUpHosts[0];
60 3
        if ($this->useCdnDomains === true) {
61
            $host = $zone->srcUpHosts[0];
62
        }
63
64 3
        return $scheme . $host;
65
    }
66
67
    public function getRsHost($accessKey, $bucket)
68
    {
69
        $zone = $this->getZone($accessKey, $bucket);
70
71
        if ($this->useHTTPS === true) {
72
            $scheme = "https://";
73
        } else {
74
            $scheme = "http://";
75
        }
76
77
        return $scheme . $zone->rsHost;
78
    }
79
80
    public function getRsfHost($accessKey, $bucket)
81
    {
82
        $zone = $this->getZone($accessKey, $bucket);
83
84
        if ($this->useHTTPS === true) {
85
            $scheme = "https://";
86
        } else {
87
            $scheme = "http://";
88
        }
89
90
        return $scheme . $zone->rsfHost;
91
    }
92
93 6
    public function getIovipHost($accessKey, $bucket)
94
    {
95 6
        $zone = $this->getZone($accessKey, $bucket);
96
97 6
        if ($this->useHTTPS === true) {
98
            $scheme = "https://";
99
        } else {
100 6
            $scheme = "http://";
101
        }
102
103 6
        return $scheme . $zone->iovipHost;
104
    }
105
106
    public function getApiHost($accessKey, $bucket)
107
    {
108
        $zone = $this->getZone($accessKey, $bucket);
109
110
        if ($this->useHTTPS === true) {
111
            $scheme = "https://";
112
        } else {
113
            $scheme = "http://";
114
        }
115
116
        return $scheme . $zone->apiHost;
117
    }
118
119 24
    private function getZone($accessKey, $bucket)
120
    {
121 24
        $cacheId = "$accessKey:$bucket";
122
123 24
        if (isset($this->zoneCache[$cacheId])) {
124 6
            print("from cache\n");
125 6
            $zone = $this->zoneCache[$cacheId];
126 24
        } elseif (isset($this->zone)) {
127 3
            print("from set\n");
128 3
            $zone = $this->zone;
129 3
            $this->zoneCache[$cacheId] = $zone;
130 2
        } else {
131 21
            print("from query\n");
132 21
            $zone = Zone::QueryZone($accessKey, $bucket);
133 21
            $this->zoneCache[$cacheId] = $zone;
134
        }
135 24
        return $zone;
136
    }
137
}
138