Test Failed
Push — master ( a56895...fb7a4d )
by frey
03:30
created

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