Test Failed
Push — master ( 38eb2d...881ded )
by frey
04:06
created

CDN::signature()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

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