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

QcloudAgent::genSign()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 1
1
<?php
2
3
namespace Toplan\PhpSms;
4
5
/**
6
 * Class SendCloudAgent
7
 *
8
 * @property string $appId
9
 * @property string $appKey
10
 * @property string $nationCode
11
 */
12
class QcloudAgent extends Agent implements TemplateSms, ContentSms, VoiceCode, ContentVoice
13
{
14
    protected $sendSms = 'https://yun.tim.qq.com/v5/tlssmssvr/sendsms';
15
    protected $sendVoiceCode = 'https://yun.tim.qq.com/v5/tlsvoicesvr/sendvoice';
16
    protected $sendVoicePrompt = 'https://yun.tim.qq.com/v5/tlsvoicesvr/sendvoiceprompt';
17
    protected $random;
18
19 View Code Duplication
    public function sendContentSms($to, $content)
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...
20
    {
21
        $params = [
22
            'type'   => 0, // 0:普通短信 1:营销短信
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
23
            'msg'    => $content,
24
            'tel'    => $to,
25
            'time'   => time(),
26
        ];
27
        $this->random = $this->getRandom();
28
        $sendUrl = "{$this->sendSms}?sdkappid={$this->appId}&random={$this->random}";
29
        $this->request($sendUrl, $params);
30
    }
31
32 View Code Duplication
    public function sendTemplateSms($to, $tempId, array $data)
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...
33
    {
34
        $params = [
35
            'tel'    => $to,
36
            'tpl_id' => $tempId,
37
            'params' => array_values($data),
38
            'time'   => time(),
39
        ];
40
        $this->random = $this->getRandom();
41
        $sendUrl = "{$this->sendSms}?sdkappid={$this->appId}&random={$this->random}";
42
        $this->request($sendUrl, $params);
43
    }
44
45
    public function sendVoiceCode($to, $code)
46
    {
47
        $params = [
48
            'tel'    => $to,
49
            'msg'    => $code,
50
        ];
51
        $sendUrl = "{$this->sendVoiceCode}?sdkappid={$this->appId}&random={$this->random}";
52
        $this->request($sendUrl, $params);
53
    }
54
55
    public function sendContentVoice($to, $content)
56
    {
57
        $params = [
58
            'tel'        => $to,
59
            'prompttype' => 2,
60
            'promptfile' => $content,
61
        ];
62
        $sendUrl = "{$this->sendVoicePrompt}?sdkappid={$this->appId}&random={$this->random}";
63
        $this->request($sendUrl, $params);
64
    }
65
66
    protected function request($sendUrl, array $params)
67
    {
68
        $params['sig'] = $this->genSign($params);
69
        $params = $this->params($params);
70
        $result = $this->curlPost($sendUrl, [], [
71
            CURLOPT_POSTFIELDS => json_encode($params),
72
        ]);
73
        $this->setResult($result);
74
    }
75
76
    protected function genSign($params)
77
    {
78
        $phone = $params['tel']['mobile'];
79
        $signature = "appkey={$this->appKey}&random={$this->random}&time={$params['time']}&mobile={$phone}";
80
81
        return hash('sha256', $signature, false);
82
    }
83
84 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...
85
    {
86
        if ($result['request']) {
87
            $this->result(Agent::INFO, $result['response']);
88
            $result = json_decode($result['response'], true);
89
            if (isset($result['result'])) {
90
                $this->result(Agent::SUCCESS, $result['result'] === 0);
91
                $this->result(Agent::CODE, $result['result']);
92
            }
93
        } else {
94
            $this->result(Agent::INFO, 'request failed');
95
        }
96
    }
97
98
    protected function getRandom()
99
    {
100
        return rand(100000, 999999);
101
    }
102
}
103