Completed
Push — master ( f18dcd...2545e4 )
by r
14:31 queued 12:47
created

Config::getZone()   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 CASTER_HOST = 'pili-caster.qiniuapi.com';
17
    const RTCAPI_VERSION = 'v3';
18
19
    // Zone 空间对应的存储区域
20
    public $region;
21
    //BOOL 是否使用https域名
22
    public $useHTTPS;
23
    //BOOL 是否使用CDN加速上传域名
24
    public $useCdnDomains;
25
    // Zone Cache
26
    private $regionCache;
27
28 69
    // 构造函数
29
    public function __construct(Region $z = null)
30 69
    {
31 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...
32 69
        $this->useHTTPS = false;
33 69
        $this->useCdnDomains = false;
34 69
        $this->regionCache = array();
35
    }
36 18
37
    public function getUpHost($accessKey, $bucket)
38 18
    {
39 18
        $region = $this->getRegion($accessKey, $bucket);
40
        if ($this->useHTTPS === true) {
41
            $scheme = "https://";
42 18
        } else {
43
            $scheme = "http://";
44
        }
45 18
46 17
        $host = $region->srcUpHosts[0];
47
        if ($this->useCdnDomains === true) {
48
            $host = $region->cdnUpHosts[0];
49
        }
50 17
51
        return $scheme . $host;
52
    }
53
54
    public function getUpBackupHost($accessKey, $bucket)
55
    {
56
        $region = $this->getRegion($accessKey, $bucket);
57
        if ($this->useHTTPS === true) {
58
            $scheme = "https://";
59
        } else {
60
            $scheme = "http://";
61
        }
62
63
        $host = $region->cdnUpHosts[0];
64
        if ($this->useCdnDomains === true) {
65
            $host = $region->srcUpHosts[0];
66
        }
67
68
        return $scheme . $host;
69
    }
70
71
    public function getRsHost($accessKey, $bucket)
72
    {
73
        $region = $this->getRegion($accessKey, $bucket);
74
75
        if ($this->useHTTPS === true) {
76
            $scheme = "https://";
77
        } else {
78
            $scheme = "http://";
79
        }
80
81
        return $scheme . $region->rsHost;
82
    }
83
84
    public function getRsfHost($accessKey, $bucket)
85
    {
86
        $region = $this->getRegion($accessKey, $bucket);
87
88
        if ($this->useHTTPS === true) {
89
            $scheme = "https://";
90
        } else {
91
            $scheme = "http://";
92
        }
93
94
        return $scheme . $region->rsfHost;
95
    }
96 6
97
    public function getIovipHost($accessKey, $bucket)
98 6
    {
99
        $region = $this->getRegion($accessKey, $bucket);
100 6
101
        if ($this->useHTTPS === true) {
102
            $scheme = "https://";
103 6
        } else {
104
            $scheme = "http://";
105
        }
106 6
107
        return $scheme . $region->iovipHost;
108
    }
109
110
    public function getApiHost($accessKey, $bucket)
111
    {
112
        $region = $this->getRegion($accessKey, $bucket);
113
114
        if ($this->useHTTPS === true) {
115
            $scheme = "https://";
116
        } else {
117
            $scheme = "http://";
118
        }
119
120
        return $scheme . $region->apiHost;
121
    }
122 24
123
    private function getRegion($accessKey, $bucket)
124 24
    {
125
        $cacheId = "$accessKey:$bucket";
126 24
127 3
        if (isset($this->regionCache[$cacheId])) {
128 24
            $region = $this->regionCache[$cacheId];
129 3
        } elseif (isset($this->zone)) {
130 3
            $region = $this->zone;
131 3
            $this->regionCache[$cacheId] = $region;
132 21
        } else {
133 21
            $region = Zone::queryZone($accessKey, $bucket);
134
            $this->regionCache[$cacheId] = $region;
135 24
        }
136
        return $region;
137
    }
138
}
139