Test Failed
Push — master ( 8d4205...c9a664 )
by alpha
02:25
created

AliyunOssConfig::getProtocol()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 2
1
<?php
2
3
namespace AlphaSnow\AliyunOss;
4
5
use Illuminate\Support\Collection;
6
7
class AliyunOssConfig extends Collection
8
{
9
    /**
10
     * @return string
11
     */
12 5
    public function getUrlDomain()
13
    {
14 5
        if ($this->get('domain')) {
15 1
            return $this->getProtocol().'://'.$this->get('domain');
16
        }
17 4
        return $this->getEndpointDomain();
18
    }
19
20
    /**
21
     * @return string
22
     */
23 4
    protected function getEndpointDomain()
24
    {
25 4
        return $this->getProtocol().'://'.$this->get('bucket').'.'.$this->get('endpoint');
26
    }
27
    /**
28
     * @return string
29
     */
30 2
    protected function getInternalDomain()
31
    {
32 2
        return $this->getProtocol().'://'.$this->get('bucket').'.'.$this->get('internal');
33
    }
34
35
    /**
36
     * @return string
37
     */
38 5
    protected function getProtocol()
39
    {
40 5
        return $this->get('use_ssl', false) ? 'https' : 'http';
41
    }
42
43
    /**
44
     * @return string
45
     */
46 4
    public function getOssEndpoint()
47
    {
48 4
        if ($internal = $this->get('internal')) {
49 4
            return $internal;
50
        }
51 1
        if ($this->isCName()) {
52
            return $this->get('domain');
53
        }
54 1
        return $this->get('endpoint');
55
    }
56
57
    /**
58
     * @return bool
59
     */
60 4
    protected function isCName()
61
    {
62 4
        return $this->get('use_domain_endpoint') && $this->get('domain');
63
    }
64
65
    /**
66
     * @return array
67
     */
68 3
    public function getOssClientParameters()
69
    {
70
        return [
71 3
            'accessKeyId' => $this->get('access_id'),
72 3
            'accessKeySecret' => $this->get('access_key'),
73 3
            'endpoint' => $this->getOssEndpoint(),
74 3
            'isCName' => $this->isCName(),
75 3
            'securityToken' => $this->get('security_token', null)
76
        ];
77
    }
78
79 2
    public function correctUrl($url)
80
    {
81
        // correct internal domain
82 2
        if ($this->get('internal')) {
83 2
            return str_replace($this->getInternalDomain(), $this->getUrlDomain(), $url);
84
        }
85
86
        // correct domain
87
        if ($this->get('domain') && $this->get('use_domain_endpoint') == false) {
88
            return str_replace($this->getEndpointDomain(), $this->getUrlDomain(), $url);
89
        }
90
91
        return $url;
92
    }
93
}
94