Test Failed
Push — master ( 881ded...876e65 )
by frey
03:30
created

CDN::getConfig()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 0
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Freyo\Flysystem\QcloudCOSv5\Plugins;
4
5
use League\Flysystem\Plugin\AbstractPlugin;
6
7
class CDN extends AbstractPlugin
8
{
9
    /**
10
     * Get the method name.
11
     *
12
     * @return string
13
     */
14
    public function getMethod()
15
    {
16
        return 'cdn';
17
    }
18
19
    /**
20
     * @return $this
21
     */
22
    public function handle()
23
    {
24
        return $this;
25
    }
26
27
    /**
28
     * @param string $url
29
     * @param string $key
30
     * @param int $timestamp
31
     *
32
     * @return string
33
     */
34
    public function signature($url, $key = null, $timestamp = null)
35
    {
36
        $key = $key ?: $this->getConfig()->get('cdn_key');
37
        $timestamp = dechex($timestamp ?: time());
38
39
        $parsed = parse_url($url);
40
41
        $signature = md5($key . $parsed['path'] . $timestamp);
42
43
        $query = http_build_query(['sign' => $signature, 't' => $timestamp]);
44
45
        $separator = empty($parsed['query']) ? '?' : '&';
46
47
        return $url . $separator . $query;
48
    }
49
50
    /**
51
     * @param $url
52
     *
53
     * @return bool
54
     */
55
    public function refreshUrl($url)
56
    {
57
        $urls = is_array($url) ? $url : func_get_args();
58
59
        return $this->request($urls, 'urls', 'RefreshCdnUrl');
60
    }
61
62
    /**
63
     * @param $dir
64
     *
65
     * @return bool
66
     */
67
    public function refreshDir($dir)
68
    {
69
        $dirs = is_array($dir) ? $dir : func_get_args();
70
71
        return $this->request($dirs, 'dirs', 'RefreshCdnDir');
72
    }
73
74
    /**
75
     * @param array $args
76
     * @param string $key
77
     * @param string $action
78
     *
79
     * @return bool
80
     */
81
    protected function request(array $args, $key, $action)
82
    {
83
        $client = $this->getHttpClient();
84
85
        $response = $client->post('/v2/index.php', [
86
            'form_params' => $this->buildFormParams($args, $key, $action),
87
        ]);
88
89
        $contents = $response->getBody()->getContents();
90
91
        return $this->normalize($contents);
92
    }
93
94
    /**
95
     * @return \GuzzleHttp\Client
96
     */
97
    protected function getHttpClient()
98
    {
99
        return new \GuzzleHttp\Client([
100
            'verify' => false,
101
            'base_uri' => 'https://cdn.api.qcloud.com',
102
        ]);
103
    }
104
105
    /**
106
     * @param array $values
107
     * @param string $key
108
     * @param string $action
109
     *
110
     * @return array
111
     */
112
    protected function buildFormParams(array $values, $key, $action)
113
    {
114
        $keys = array_map(function ($n) use ($key) {
115
            return sprintf("{$key}.%d", $n);
116
        }, range(0, count($values) - 1));
117
118
        $params = array_combine($keys, $values);
119
120
        $params = $this->addCommonParams($params, $action);
121
122
        return $this->addSignature($params);
123
    }
124
125
    /**
126
     * @param array $params
127
     * @param string $action
128
     *
129
     * @return array
130
     */
131
    protected function addCommonParams(array $params, $action)
132
    {
133
        return array_merge([
134
            'Action' => $action,
135
            'SecretId' => $this->getCredentials()['secretId'],
136
            'Timestamp' => time(),
137
            'Nonce' => rand(1, 65535),
138
        ], $params);
139
    }
140
141
    /**
142
     * @return array
143
     */
144
    protected function getCredentials()
145
    {
146
        return $this->filesystem->getConfig()->get('credentials');
147
    }
148
149
    /**
150
     * @param array $params
151
     *
152
     * @return array
153
     */
154
    protected function addSignature(array $params)
155
    {
156
        $params['Signature'] = $this->getSignature($params);
157
158
        return $params;
159
    }
160
161
    /**
162
     * @param array $params
163
     *
164
     * @return string
165
     */
166
    protected function getSignature(array $params)
167
    {
168
        ksort($params);
169
170
        $srcStr = 'POSTcdn.api.qcloud.com/v2/index.php?' . urldecode(http_build_query($params));
171
172
        return base64_encode(hash_hmac('sha1', $srcStr, $this->getCredentials()['secretKey'], true));
173
    }
174
175
    /**
176
     * @param string $contents
177
     *
178
     * @return bool
179
     */
180
    protected function normalize($contents)
181
    {
182
        $json = json_decode($contents);
183
184
        if (json_last_error() !== JSON_ERROR_NONE) {
185
            return false;
186
        }
187
188
        return 0 === $json->code;
189
    }
190
191
    /**
192
     * @return \League\Flysystem\Config
193
     */
194
    protected function getConfig()
195
    {
196
        return $this->filesystem->getConfig();
197
    }
198
}
199