Completed
Pull Request — master (#293)
by
unknown
15:58 queued 14:39
created

Config::getRegion()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3.0067

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 2
dl 0
loc 15
ccs 10
cts 11
cp 0.9091
crap 3.0067
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
    // 构造函数
29 69
    public function __construct(Region $z = null)
30
    {
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 69
    }
36
37 15
    public function getUpHost($accessKey, $bucket)
38
    {
39 15
        $region = $this->getRegion($accessKey, $bucket);
40 15
        if ($this->useHTTPS === true) {
41
            $scheme = "https://";
42
        } else {
43 15
            $scheme = "http://";
44
        }
45
46 15
        $host = $region->srcUpHosts[0];
47 3
        if ($this->useCdnDomains === true) {
48
            $host = $region->cdnUpHosts[0];
49
        }
50
51 3
        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
97 6
    public function getIovipHost($accessKey, $bucket)
98
    {
99 6
        $region = $this->getRegion($accessKey, $bucket);
100
101 6
        if ($this->useHTTPS === true) {
102
            $scheme = "https://";
103
        } else {
104 6
            $scheme = "http://";
105
        }
106
107 6
        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
123 21
    private function getRegion($accessKey, $bucket)
124
    {
125 21
        $cacheId = "$accessKey:$bucket";
126
127 21
        if (isset($this->regionCache[$cacheId])) {
128
            $region = $this->regionCache[$cacheId];
129 21
        } elseif (isset($this->zone)) {
130 3
            $region = $this->zone;
131 3
            $this->regionCache[$cacheId] = $region;
132 3
        } else {
133 18
            $region = Zone::queryZone($accessKey, $bucket);
134 18
            $this->regionCache[$cacheId] = $region;
135
        }
136 21
        return $region;
137
    }
138
}
139