Completed
Push — master ( 283203...274200 )
by lan tian
11s
created

AliyunAgent::percentEncode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 6
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
 */
12
class AliyunAgent extends Agent implements TemplateSms
13
{
14
    protected static $sendUrl = 'https://sms.aliyuncs.com';
15
16
    public function sendTemplateSms($to, $tempId, array $data)
17
    {
18
        $params = [
19
            'Action'            => 'SingleSendSms',
20
            'SignName'          => $this->signName,
21
            'ParamString'       => $this->getTempDataString($data),
22
            'RecNum'            => $to,
23
            'TemplateCode'      => $tempId,
24
        ];
25
        $this->request($params);
26
    }
27
28
    protected function request(array $params)
29
    {
30
        $params = $this->createParams($params);
31
        $result = $this->curlPost(self::$sendUrl, $params);
32
        $this->setResult($result);
33
    }
34
35
    protected function createParams(array $params)
36
    {
37
        return array_merge([
38
            'Format'            => 'JSON',
39
            'Version'           => '2016-09-27',
40
            'AccessKeyId'       => $this->accessKeyId,
41
            'SignatureMethod'   => 'HMAC-SHA1',
42
            'Timestamp'         => date('Y-m-d\TH:i:s\Z'),
43
            'SignatureVersion'  => '1.0',
44
            'SignatureNonce'    => uniqid(),
45
        ], $params, [
46
            'Signature'         => $this->computeSignature($params),
47
        ]);
48
    }
49
50
    private function computeSignature($parameters)
51
    {
52
        ksort($parameters);
53
        $canonicalizedQueryString = '';
54
        foreach ($parameters as $key => $value) {
55
            $canonicalizedQueryString .= '&' . $this->percentEncode($key) . '=' . $this->percentEncode($value);
56
        }
57
        $stringToSign = 'POST&%2F&' . $this->percentencode(substr($canonicalizedQueryString, 1));
58
59
        return base64_encode(hash_hmac('sha1', $stringToSign, $this->accessKeySecret . '&', true));
60
    }
61
62
    protected function percentEncode($str)
63
    {
64
        $res = urlencode($str);
65
        $res = preg_replace('/\+/', '%20', $res);
66
        $res = preg_replace('/\*/', '%2A', $res);
67
        $res = preg_replace('/%7E/', '~', $res);
68
69
        return $res;
70
    }
71
72 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...
73
    {
74
        if ($result['request']) {
75
            $this->result(Agent::INFO, $result['response']);
76
            $result = json_decode($result['response'], true);
77
            if (isset($result['Message'])) {
78
                $this->result(Agent::CODE, $result['Code']);
79
            } else {
80
                $this->result(Agent::SUCCESS, true);
81
            }
82
        } else {
83
            $this->result(Agent::INFO, 'request failed');
84
        }
85
    }
86
87
    protected function getTempDataString(array $data)
88
    {
89
        $data = array_map(function ($value) {
90
            return (string) $value;
91
        }, $data);
92
93
        return json_encode($data);
94
    }
95
}
96