Completed
Push — master ( c18819...86bd9f )
by frey
03:46
created

CDN::getHttpClient()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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