Passed
Push — master ( be2d4f...7625ce )
by frey
01:09 queued 11s
created

TencentCloudClient::canonicalize()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 7
c 0
b 0
f 0
dl 0
loc 14
rs 10
cc 4
nc 5
nop 2
1
<?php
2
3
namespace Freyo\ApiGateway\Kernel;
4
5
class TencentCloudClient extends BaseClient
6
{
7
    /**
8
     * Make a API request.
9
     *
10
     * @param string $url
11
     * @param string $method
12
     * @param array $options
13
     * @param bool $returnRaw
14
     *
15
     * @return \Psr\Http\Message\ResponseInterface|\Freyo\ApiGateway\Kernel\Support\Collection|array|object|string
16
     * @throws Exceptions\InvalidConfigException
17
     */
18
    public function request($url, $method = 'GET', array $options = [], $returnRaw = false)
19
    {
20
        if (empty($this->middlewares)) {
21
            $this->registerHttpMiddlewares();
22
        }
23
24
        $response = $this->performRequest($url, $method, $options);
0 ignored issues
show
Bug introduced by
The method performRequest() does not exist on Freyo\ApiGateway\Kernel\TencentCloudClient. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

24
        /** @scrutinizer ignore-call */ 
25
        $response = $this->performRequest($url, $method, $options);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
25
26
        return $returnRaw ? $response : $this->castResponseToType($response, $this->app->config->get('response_type'));
0 ignored issues
show
Bug Best Practice introduced by
The property config does not exist on Freyo\ApiGateway\Kernel\ServiceContainer. Since you implemented __get, consider adding a @property annotation.
Loading history...
27
    }
28
29
    /**
30
     * GET request.
31
     *
32
     * @param string $url
33
     * @param array $query
34
     *
35
     * @return \Psr\Http\Message\ResponseInterface|\Freyo\ApiGateway\Kernel\Support\Collection|array|object|string
36
     * @throws Exceptions\InvalidConfigException
37
     */
38
    public function httpGet($url, array $query = [])
39
    {
40
        $query = array_merge($this->getCommonParameters(), $query);
41
42
        $query['Signature'] = $this->signature(
43
            'GET', $this->baseUri . $url, $query
44
        );
45
46
        return $this->request($url, 'GET', ['query' => $query]);
47
    }
48
49
    /**
50
     * POST request.
51
     *
52
     * @param string $url
53
     * @param array $data
54
     *
55
     * @return \Psr\Http\Message\ResponseInterface|\Freyo\ApiGateway\Kernel\Support\Collection|array|object|string
56
     * @throws Exceptions\InvalidConfigException
57
     */
58
    public function httpPost($url, array $data = [])
59
    {
60
        $data = array_merge($this->getCommonParameters(), $data);
61
62
        $data['Signature'] = $this->signature(
63
            'POST', $this->baseUri . $url, $data
64
        );
65
66
        return $this->request($url, 'POST', ['form_params' => $data]);
67
    }
68
69
    /**
70
     * @param $method
71
     * @param $url
72
     * @param array $params
73
     *
74
     * @return string
75
     */
76
    protected function signature($method, $url, array $params = [])
77
    {
78
        ksort($params);
79
80
        $srcStr = sprintf('%s%s%s?%s',
81
            $method,
82
            parse_url($url, PHP_URL_HOST),
83
            parse_url($url, PHP_URL_PATH),
84
            $this->canonicalize($params)
85
        );
86
87
        return base64_encode(
88
            hash_hmac('sha1', $srcStr, $this->app->getSecretKey(), true)
89
        );
90
    }
91
92
    /**
93
     * @return array
94
     */
95
    protected function getCommonParameters()
96
    {
97
        return [
98
            'Timestamp' => time(),
99
            'Nonce' => rand(1, 65535),
100
            'SecretId' => $this->app->getSecretId(),
101
            'Region' => $this->app->getRegion(),
102
        ];
103
    }
104
105
    /**
106
     * @param array $input
107
     * @param string $keyPrefix
108
     *
109
     * @return string
110
     */
111
    protected function canonicalize(array $input, $keyPrefix = '')
112
    {
113
        $resource = [];
114
115
        foreach ($input as $key => $value) {
116
117
            $key = $keyPrefix ? $keyPrefix . '.' . $key : $key;
118
119
            $resource[] = is_array($value)
120
                ? $this->canonicalize($value, $key)
121
                : $key . '=' . $value;
122
        }
123
124
        return implode('&', $resource);
125
    }
126
}