CDN   A
last analyzed

Complexity

Total Complexity 41

Size/Duplication

Total Lines 324
Duplicated Lines 0 %

Test Coverage

Coverage 42%

Importance

Changes 5
Bugs 0 Features 2
Metric Value
eloc 75
c 5
b 0
f 2
dl 0
loc 324
ccs 42
cts 100
cp 0.42
rs 9.1199
wmc 41

23 Methods

Rating   Name   Duplication   Size   Complexity  
A signatureA() 0 13 5
A signatureB() 0 11 3
A getConfig() 0 3 1
A signatureC() 0 11 3
A signatureD() 0 11 4
A buildFormParams() 0 11 1
A getHttpClient() 0 4 1
A request() 0 11 1
A refreshUrl() 0 5 2
A normalize() 0 3 1
A getMethod() 0 3 1
A addCommonParams() 0 8 1
A addSignature() 0 5 1
A pushOverseaUrl() 0 5 2
A getSignature() 0 7 1
A getCredentials() 0 3 1
A pushUrl() 0 5 2
A pushUrlV2() 0 5 2
A refreshDir() 0 5 2
A refreshOverseaUrl() 0 5 2
A refreshOverseaDir() 0 5 2
A handle() 0 3 1
A signature() 0 3 1

How to fix   Complexity   

Complex Class

Complex classes like CDN often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use CDN, and based on these observations, apply Extract Interface, too.

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 5
    public function handle()
23
    {
24 5
        return $this;
25
    }
26
27
    /**
28
     * @param string $url
29
     * @param string $key
30
     * @param int    $timestamp
31
     * @param string $signName
32
     * @param string $timeName
33
     *
34
     * @return string
35
     */
36 1
    public function signature($url, $key = null, $timestamp = null, $signName = 'sign', $timeName = 't')
37
    {
38 1
        return $this->signatureD($url, $key, $timestamp, $signName, $timeName);
39
    }
40
41
    /**
42
     * @param string $url
43
     * @param string $key
44
     * @param int    $timestamp
45
     * @param string $random
46
     * @param string $signName
47
     *
48
     * @return string
49
     */
50 1
    public function signatureA($url, $key = null, $timestamp = null, $random = null, $signName = 'sign')
51
    {
52 1
        $key = $key ?: $this->getConfig()->get('cdn_key');
53 1
        $timestamp = $timestamp ?: time();
54 1
        $random = $random ?: sha1(uniqid('', true));
55
56 1
        $parsed = parse_url($url);
57 1
        $hash = md5(sprintf('%s-%s-%s-%s-%s', $parsed['path'], $timestamp, $random, 0, $key));
58 1
        $signature = sprintf('%s-%s-%s-%s', $timestamp, $random, 0, $hash);
59 1
        $query = http_build_query([$signName => $signature]);
60 1
        $separator = empty($parsed['query']) ? '?' : '&';
61
62 1
        return $url.$separator.$query;
63
    }
64
65
    /**
66
     * @param string $url
67
     * @param string $key
68
     * @param int    $timestamp
69
     *
70
     * @return string
71
     */
72 1
    public function signatureB($url, $key = null, $timestamp = null)
73
    {
74 1
        $key = $key ?: $this->getConfig()->get('cdn_key');
75 1
        $timestamp = date('YmdHi', $timestamp ?: time());
76
77 1
        $parsed = parse_url($url);
78 1
        $hash = md5($key.$timestamp.$parsed['path']);
79
80 1
        return sprintf(
81 1
            '%s://%s/%s/%s%s',
82 1
            $parsed['scheme'], $parsed['host'], $timestamp, $hash, $parsed['path']
83 1
        );
84
    }
85
86
    /**
87
     * @param string $url
88
     * @param string $key
89
     * @param int    $timestamp
90
     *
91
     * @return string
92
     */
93 1
    public function signatureC($url, $key = null, $timestamp = null)
94
    {
95 1
        $key = $key ?: $this->getConfig()->get('cdn_key');
96 1
        $timestamp = dechex($timestamp ?: time());
97
98 1
        $parsed = parse_url($url);
99 1
        $hash = md5($key.$parsed['path'].$timestamp);
100
101 1
        return sprintf(
102 1
            '%s://%s/%s/%s%s',
103 1
            $parsed['scheme'], $parsed['host'], $hash, $timestamp, $parsed['path']
104 1
        );
105
    }
106
107
    /**
108
     * @param string $url
109
     * @param string $key
110
     * @param int    $timestamp
111
     * @param string $signName
112
     * @param string $timeName
113
     *
114
     * @return string
115
     */
116 2
    public function signatureD($url, $key = null, $timestamp = null, $signName = 'sign', $timeName = 't')
117
    {
118 2
        $key = $key ?: $this->getConfig()->get('cdn_key');
119 2
        $timestamp = dechex($timestamp ?: time());
120
121 2
        $parsed = parse_url($url);
122 2
        $signature = md5($key.$parsed['path'].$timestamp);
123 2
        $query = http_build_query([$signName => $signature, $timeName => $timestamp]);
124 2
        $separator = empty($parsed['query']) ? '?' : '&';
125
126 2
        return $url.$separator.$query;
127
    }
128
129
    /**
130
     * @param $url
131
     *
132
     * @return array
133
     */
134
    public function pushUrl($url)
135
    {
136
        $urls = is_array($url) ? $url : func_get_args();
137
138
        return $this->request($urls, 'urls', 'CdnUrlPusher');
139
    }
140
141
    /**
142
     * @param $url
143
     *
144
     * @return array
145
     */
146
    public function pushOverseaUrl($url)
147
    {
148
        $urls = is_array($url) ? $url : func_get_args();
149
150
        return $this->request($urls, 'urls', 'CdnOverseaPushser');
151
    }
152
153
    /**
154
     * @param $url
155
     *
156
     * @return array
157
     */
158
    public function pushUrlV2($url)
159
    {
160
        $urls = is_array($url) ? $url : func_get_args();
161
162
        return $this->request($urls, 'urls', 'CdnPusherV2');
163
    }
164
165
    /**
166
     * @param $url
167
     *
168
     * @return array
169
     */
170
    public function refreshUrl($url)
171
    {
172
        $urls = is_array($url) ? $url : func_get_args();
173
174
        return $this->request($urls, 'urls', 'RefreshCdnUrl');
175
    }
176
177
    /**
178
     * @param $url
179
     *
180
     * @return array
181
     */
182
    public function refreshOverseaUrl($url)
183
    {
184
        $urls = is_array($url) ? $url : func_get_args();
185
186
        return $this->request($urls, 'urls', 'RefreshCdnOverSeaUrl');
187
    }
188
189
    /**
190
     * @param $dir
191
     *
192
     * @return array
193
     */
194
    public function refreshDir($dir)
195
    {
196
        $dirs = is_array($dir) ? $dir : func_get_args();
197
198
        return $this->request($dirs, 'dirs', 'RefreshCdnDir');
199
    }
200
201
    /**
202
     * @param $dir
203
     *
204
     * @return array
205
     */
206
    public function refreshOverseaDir($dir)
207
    {
208
        $dirs = is_array($dir) ? $dir : func_get_args();
209
210
        return $this->request($dirs, 'dirs', 'RefreshCdnOverSeaDir');
211
    }
212
213
    /**
214
     * @param array  $args
215
     * @param string $key
216
     * @param string $action
217
     *
218
     * @return array
219
     */
220
    protected function request(array $args, $key, $action)
221
    {
222
        $client = $this->getHttpClient();
223
224
        $response = $client->post('/v2/index.php', [
225
            'form_params' => $this->buildFormParams($args, $key, $action),
226
        ]);
227
228
        $contents = $response->getBody()->getContents();
229
230
        return $this->normalize($contents);
231
    }
232
233
    /**
234
     * @return \GuzzleHttp\Client
235
     */
236
    protected function getHttpClient()
237
    {
238
        return new \GuzzleHttp\Client([
239
            'base_uri' => 'https://cdn.api.qcloud.com',
240
        ]);
241
    }
242
243
    /**
244
     * @param array  $values
245
     * @param string $key
246
     * @param string $action
247
     *
248
     * @return array
249
     */
250
    protected function buildFormParams(array $values, $key, $action)
251
    {
252
        $keys = array_map(function ($n) use ($key) {
253
            return sprintf("{$key}.%d", $n);
254
        }, range(0, count($values) - 1));
255
256
        $params = array_combine($keys, $values);
257
258
        $params = $this->addCommonParams($params, $action);
259
260
        return $this->addSignature($params);
261
    }
262
263
    /**
264
     * @param array  $params
265
     * @param string $action
266
     *
267
     * @return array
268
     */
269
    protected function addCommonParams(array $params, $action)
270
    {
271
        return array_merge([
272
            'Action'    => $action,
273
            'SecretId'  => $this->getCredentials()['secretId'],
274
            'Timestamp' => time(),
275
            'Nonce'     => rand(1, 65535),
276
        ], $params);
277
    }
278
279
    /**
280
     * @return array
281
     */
282
    protected function getCredentials()
283
    {
284
        return $this->getConfig()->get('credentials');
285
    }
286
287
    /**
288
     * @param array $params
289
     *
290
     * @return array
291
     */
292
    protected function addSignature(array $params)
293
    {
294
        $params['Signature'] = $this->getSignature($params);
295
296
        return $params;
297
    }
298
299
    /**
300
     * @param array $params
301
     *
302
     * @return string
303
     */
304
    protected function getSignature(array $params)
305
    {
306
        ksort($params);
307
308
        $srcStr = 'POSTcdn.api.qcloud.com/v2/index.php?'.urldecode(http_build_query($params));
309
310
        return base64_encode(hash_hmac('sha1', $srcStr, $this->getCredentials()['secretKey'], true));
311
    }
312
313
    /**
314
     * @param string $contents
315
     *
316
     * @throws \InvalidArgumentException if the JSON cannot be decoded.
317
     *
318
     * @return array
319
     */
320
    protected function normalize($contents)
321
    {
322
        return \GuzzleHttp\json_decode($contents, true);
323
    }
324
325
    /**
326
     * @return \League\Flysystem\Config
327
     */
328 5
    protected function getConfig()
329
    {
330 5
        return $this->filesystem->getConfig();
331
    }
332
}
333