SubMailAgent::transformConfig()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 8
Ratio 100 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 8
loc 8
rs 9.4285
cc 1
eloc 6
nc 1
nop 0
1
<?php
2
3
namespace Laravelsms\Sms\Agents;
4
5
use Laravelsms\Sms\Contracts\Sms;
6
use Illuminate\Support\Arr;
7
8
class SubMailAgent extends Sms
9
{
10
    protected $config = [];
11
12
    private $appId;
13
    private $apiKey;
14
15
    private $host = 'https://api.mysubmail.com';
16
    private $singleSendUrl = '/message/xsend.json';
17
    private $timestampUrl = '/service/timestamp.json';
18
19
    public function __construct($config)
20
    {
21
        $this->config = $config;
22
        $this->transformConfig();
23
    }
24
25 View Code Duplication
    protected function transformConfig()
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...
26
    {
27
        $credentials = Arr::pull($this->config, 'credentials');
28
        $this->appId = Arr::pull($credentials, 'appid');
29
        $this->apiKey = Arr::pull($credentials, 'apiKey');
30
        $this->setTemplateId(Arr::pull($this->config, 'templateId'));
31
        $this->setSignName();
32
    }
33
34
    public function singlesSend($mobile, $send = true)
35
    {
36
        $url = $this->host . $this->singleSendUrl;
37
38
        $timestamp = $this->getTimestamp();
39
40
        $postData = [
41
            'appid' => $this->appId,
42
            'to' => $mobile,
43
            'project' => $this->templateId,
44
            'vars' => json_encode($this->templateVar),
45
            'timestamp' => $timestamp,
46
            'sign_type' => 'sha1',
47
        ];
48
        $signature = $this->computeSignature($postData);
49
50
        $postData = array_merge(['signature' => $signature], $postData);
51
52
        if ($send) {
53
            return $this->curl($url, $postData);
54
        }
55
56
        return $postData;
57
    }
58
59
    /**
60
     * @param $url
61
     * @param array $postData
62
     * @return array $result
63
     * @return int $result[].code 返回0则成功,返回其它则错误
64
     * @return string $result[].msg 返回消息
65
     * @return mixed $result[].verifyCode 验证码
66
     */
67
    protected function curl($url, $postData)
68
    {
69
        $headers = array('X-HTTP-Method-Override: post');
70
71
        $ch = curl_init();
72
        curl_setopt($ch, CURLOPT_TIMEOUT, 10);
73
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
74
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
75
        curl_setopt($ch, CURLOPT_URL, $url);
76
        curl_setopt($ch, CURLOPT_POST, true);
77
        curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
78
        $httpResponse = $this->httpResponse($ch);
79
        $result = $this->transformerResponse($httpResponse);
80
        curl_close($ch);
81
82
        return $result;
83
    }
84
85 View Code Duplication
    protected function transformerResponse($httpResponse)
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...
86
    {
87
        if (empty($httpResponse['error'])) {
88
            $output = trim($httpResponse['jsonData'], "\xEF\xBB\xBF");
89
            $response = json_decode($output, true);
90
            if ($response['status'] == 'success') {
91
                $result = ['code' => 0, 'msg' => '发送成功', 'verifyCode' => $this->verifyCode];
92
            } else {
93
                $result = ['code' => $response['code'], 'msg' => $response['msg']];
94
            }
95
            unset($response);
96
        } else {
97
            $result = ['code' => time(), 'msg' => $httpResponse['error']];
98
        }
99
100
        return $result;
101
    }
102
103
    private function computeSignature($parameters)
104
    {
105
        ksort($parameters);
106
        reset($parameters);
107
108
        $canonicalizedQueryString = "";
109
        foreach ($parameters as $key => $value) {
110
            $canonicalizedQueryString .= $key . "=" . $value . "&";
111
        }
112
        $canonicalizedQueryString = rtrim($canonicalizedQueryString, '&');
113
114
        $signature = sha1($this->appId . $this->apiKey . $canonicalizedQueryString . $this->appId . $this->apiKey);
115
116
        return $signature;
117
    }
118
119
    private function getTimestamp()
120
    {
121
        $url = $this->host . $this->timestampUrl;
122
        $ch = curl_init($url);
123
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
124
        curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
125
        $output = curl_exec($ch);
126
        $timestamp = json_decode($output, true);
127
128
        return $timestamp['timestamp'];
129
    }
130
}
131