Completed
Pull Request — master (#293)
by
unknown
21:25
created

Config::getRegion()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 2
dl 0
loc 15
ccs 11
cts 11
cp 1
crap 3
rs 9.7666
c 0
b 0
f 0
1
<?php
2
namespace Qiniu;
3
4
final class Config
5
{
6
    const SDK_VER = '7.3.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 = 'argus.atlab.ai';
16
    const RTCAPI_VERSION = 'v3';
17
18
    // Zone 空间对应的机房
19
    public $region;
20
    //BOOL 是否使用https域名
21
    public $useHTTPS;
22
    //BOOL 是否使用CDN加速上传域名
23
    public $useCdnDomains;
24
    // Zone Cache
25
    private $regionCache;
26
27
    // 构造函数
28 69
    public function __construct(Region $z = null)
29
    {
30 69
        $this->zone = $z;
0 ignored issues
show
Bug introduced by
The property zone does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
31 69
        $this->useHTTPS = false;
32 69
        $this->useCdnDomains = false;
33 69
        $this->regionCache = array();
34 69
    }
35
36 18
    public function getUpHost($accessKey, $bucket)
37
    {
38 18
        $region = $this->getRegion($accessKey, $bucket);
39 18
        if ($this->useHTTPS === true) {
40
            $scheme = "https://";
41
        } else {
42 18
            $scheme = "http://";
43
        }
44
45 18
        $host = $region->srcUpHosts[0];
46 17
        if ($this->useCdnDomains === true) {
47
            $host = $region->cdnUpHosts[0];
48
        }
49
50 17
        return $scheme . $host;
51
    }
52
53
    public function getUpBackupHost($accessKey, $bucket)
54
    {
55
        $region = $this->getRegion($accessKey, $bucket);
56
        if ($this->useHTTPS === true) {
57
            $scheme = "https://";
58
        } else {
59
            $scheme = "http://";
60
        }
61
62
        $host = $region->cdnUpHosts[0];
63
        if ($this->useCdnDomains === true) {
64
            $host = $region->srcUpHosts[0];
65
        }
66
67
        return $scheme . $host;
68
    }
69
70
    public function getRsHost($accessKey, $bucket)
71
    {
72
        $region = $this->getRegion($accessKey, $bucket);
73
74
        if ($this->useHTTPS === true) {
75
            $scheme = "https://";
76
        } else {
77
            $scheme = "http://";
78
        }
79
80
        return $scheme . $region->rsHost;
81
    }
82
83
    public function getRsfHost($accessKey, $bucket)
84
    {
85
        $region = $this->getRegion($accessKey, $bucket);
86
87
        if ($this->useHTTPS === true) {
88
            $scheme = "https://";
89
        } else {
90
            $scheme = "http://";
91
        }
92
93
        return $scheme . $region->rsfHost;
94
    }
95
96 6
    public function getIovipHost($accessKey, $bucket)
97
    {
98 6
        $region = $this->getRegion($accessKey, $bucket);
99
100 6
        if ($this->useHTTPS === true) {
101
            $scheme = "https://";
102
        } else {
103 6
            $scheme = "http://";
104
        }
105
106 6
        return $scheme . $region->iovipHost;
107
    }
108
109
    public function getApiHost($accessKey, $bucket)
110
    {
111
        $region = $this->getRegion($accessKey, $bucket);
112
113
        if ($this->useHTTPS === true) {
114
            $scheme = "https://";
115
        } else {
116
            $scheme = "http://";
117
        }
118
119
        return $scheme . $region->apiHost;
120
    }
121
122 24
    private function getRegion($accessKey, $bucket)
123
    {
124 24
        $cacheId = "$accessKey:$bucket";
125
126 24
        if (isset($this->regionCache[$cacheId])) {
127 3
            $region = $this->regionCache[$cacheId];
128 24
        } elseif (isset($this->zone)) {
129 3
            $region = $this->zone;
130 3
            $this->regionCache[$cacheId] = $region;
131 3
        } else {
132 21
            $region = Zone::queryZone($accessKey, $bucket);
133 21
            $this->regionCache[$cacheId] = $region;
134
        }
135 24
        return $region;
136
    }
137
}
138