Test Failed
Push — master ( fb7a4d...a9e3cd )
by frey
03:47
created

CDN::pushUrlV2()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 1
dl 0
loc 5
ccs 0
cts 3
cp 0
crap 6
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
        $signature = md5($key . $parsed['path'] . $timestamp);
41
        $query = http_build_query(['sign' => $signature, 't' => $timestamp]);
42
        $separator = empty($parsed['query']) ? '?' : '&';
43
44
        return $url . $separator . $query;
45
    }
46
47
    /**
48
     * @param $url
49
     *
50
     * @return array
51
     */
52
    public function pushUrl($url)
53
    {
54
        $urls = is_array($url) ? $url : func_get_args();
55
56
        return $this->request($urls, 'urls', 'CdnUrlPusher');
57
    }
58
59
    /**
60
     * @param $url
61
     *
62
     * @return array
63
     */
64
    public function pushUrlV2($url)
65
    {
66
        $urls = is_array($url) ? $url : func_get_args();
67
68
        return $this->request($urls, 'urls', 'CdnPusherV2');
69
    }
70
71
    /**
72
     * @param $url
73
     *
74
     * @return array
75
     */
76
    public function refreshUrl($url)
77
    {
78
        $urls = is_array($url) ? $url : func_get_args();
79
80
        return $this->request($urls, 'urls', 'RefreshCdnUrl');
81
    }
82
83
    /**
84
     * @param $dir
85
     *
86
     * @return array
87
     */
88
    public function refreshDir($dir)
89
    {
90
        $dirs = is_array($dir) ? $dir : func_get_args();
91
92
        return $this->request($dirs, 'dirs', 'RefreshCdnDir');
93
    }
94
95
    /**
96
     * @param array $args
97
     * @param string $key
98
     * @param string $action
99
     *
100
     * @return array
101
     */
102
    protected function request(array $args, $key, $action)
103
    {
104
        $client = $this->getHttpClient();
105
106
        $response = $client->post('/v2/index.php', [
107
            'form_params' => $this->buildFormParams($args, $key, $action),
108
        ]);
109
110
        $contents = $response->getBody()->getContents();
111
112
        return $this->normalize($contents);
113
    }
114
115
    /**
116
     * @return \GuzzleHttp\Client
117
     */
118
    protected function getHttpClient()
119
    {
120
        return new \GuzzleHttp\Client([
121
            'verify' => false,
122
            'base_uri' => 'https://cdn.api.qcloud.com',
123
        ]);
124
    }
125
126
    /**
127
     * @param array $values
128
     * @param string $key
129
     * @param string $action
130
     *
131
     * @return array
132
     */
133
    protected function buildFormParams(array $values, $key, $action)
134
    {
135
        $keys = array_map(function ($n) use ($key) {
136
            return sprintf("{$key}.%d", $n);
137
        }, range(0, count($values) - 1));
138
139
        $params = array_combine($keys, $values);
140
141
        $params = $this->addCommonParams($params, $action);
142
143
        return $this->addSignature($params);
144
    }
145
146
    /**
147
     * @param array $params
148
     * @param string $action
149
     *
150
     * @return array
151
     */
152
    protected function addCommonParams(array $params, $action)
153
    {
154
        return array_merge([
155
            'Action' => $action,
156
            'SecretId' => $this->getCredentials()['secretId'],
157
            'Timestamp' => time(),
158
            'Nonce' => rand(1, 65535),
159
        ], $params);
160
    }
161
162
    /**
163
     * @return array
164
     */
165
    protected function getCredentials()
166
    {
167
        return $this->getConfig()->get('credentials');
168
    }
169
170
    /**
171
     * @param array $params
172
     *
173
     * @return array
174
     */
175
    protected function addSignature(array $params)
176
    {
177
        $params['Signature'] = $this->getSignature($params);
178
179
        return $params;
180
    }
181
182
    /**
183
     * @param array $params
184
     *
185
     * @return string
186
     */
187
    protected function getSignature(array $params)
188
    {
189
        ksort($params);
190
191
        $srcStr = 'POSTcdn.api.qcloud.com/v2/index.php?' . urldecode(http_build_query($params));
192
193
        return base64_encode(hash_hmac('sha1', $srcStr, $this->getCredentials()['secretKey'], true));
194
    }
195
196
    /**
197
     * @param string $contents
198
     *
199
     * @return array
200
     *
201
     * @throws \InvalidArgumentException if the JSON cannot be decoded.
202
     */
203
    protected function normalize($contents)
204
    {
205
        return \GuzzleHttp\json_decode($contents, true);
206
    }
207
208
    /**
209
     * @return \League\Flysystem\Config
210
     */
211
    protected function getConfig()
212
    {
213
        return $this->filesystem->getConfig();
214
    }
215
}
216