AliyunAgent::createParams()   A
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 16
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 13
nc 1
nop 1
1
<?php
2
3
namespace Toplan\PhpSms;
4
5
/**
6
 * Class AliyunAgent
7
 *
8
 * @property string $accessKeyId
9
 * @property string $accessKeySecret
10
 * @property string $signName
11
 * @property string $regionId
12
 */
13
class AliyunAgent extends Agent implements TemplateSms
14
{
15
    protected static $sendUrl = 'https://dysmsapi.aliyuncs.com/';
16
17
    public function sendTemplateSms($to, $tempId, array $data)
18
    {
19
        $params = [
20
            'Action'            => 'SendSms',
21
            'SignName'          => $this->signName,
22
            'TemplateParam'     => $this->getTempDataString($data),
23
            'PhoneNumbers'      => $to,
24
            'TemplateCode'      => $tempId,
25
        ];
26
        $this->request($params);
27
    }
28
29
    protected function request(array $params)
30
    {
31
        $params = $this->createParams($params);
32
        $result = $this->curlPost(self::$sendUrl, [], [
33
            CURLOPT_POSTFIELDS => http_build_query($params),
34
        ]);
35
        $this->setResult($result);
36
    }
37
38
    protected function createParams(array $params)
39
    {
40
        $params = array_merge([
41
            'RegionId'          => $this->regionId ?: 'cn-shenzhen',
42
            'Format'            => 'JSON',
43
            'Version'           => '2017-05-25',
44
            'AccessKeyId'       => $this->accessKeyId,
45
            'SignatureMethod'   => 'HMAC-SHA1',
46
            'Timestamp'         => gmdate('Y-m-d\TH:i:s\Z'),
47
            'SignatureVersion'  => '1.0',
48
            'SignatureNonce'    => uniqid(),
49
        ], $params);
50
        $params['Signature'] = $this->computeSignature($params);
51
52
        return $this->params($params);
53
    }
54
55
    private function computeSignature($parameters)
56
    {
57
        ksort($parameters);
58
        $canonicalizedQueryString = '';
59
        foreach ($parameters as $key => $value) {
60
            $canonicalizedQueryString .= '&' . $this->percentEncode($key) . '=' . $this->percentEncode($value);
61
        }
62
        $stringToSign = 'POST&%2F&' . $this->percentencode(substr($canonicalizedQueryString, 1));
63
64
        return base64_encode(hash_hmac('sha1', $stringToSign, $this->accessKeySecret . '&', true));
65
    }
66
67
    protected function percentEncode($str)
68
    {
69
        $res = urlencode($str);
70
        $res = preg_replace('/\+/', '%20', $res);
71
        $res = preg_replace('/\*/', '%2A', $res);
72
        $res = preg_replace('/%7E/', '~', $res);
73
74
        return $res;
75
    }
76
77 View Code Duplication
    protected function setResult($result)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
78
    {
79
        if ($result['request']) {
80
            $this->result(Agent::INFO, $result['response']);
81
            $result = json_decode($result['response'], true);
82
            $this->result(Agent::CODE, $result['Code']);
83
            if ($result['Code'] === 'OK') {
84
                $this->result(Agent::SUCCESS, true);
85
            }
86
        } else {
87
            $this->result(Agent::INFO, 'request failed');
88
        }
89
    }
90
91
    protected function getTempDataString(array $data)
92
    {
93
        $data = array_map(function ($value) {
94
            return (string) $value;
95
        }, $data);
96
97
        return json_encode($data);
98
    }
99
}
100