YunPianAgent::singlesSend()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 10

Duplication

Lines 18
Ratio 100 %

Importance

Changes 0
Metric Value
dl 18
loc 18
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 10
nc 2
nop 2
1
<?php
2
3
namespace Laravelsms\Sms\Agents;
4
5
use Laravelsms\Sms\Contracts\Sms;
6
use Illuminate\Support\Arr;
7
8
class YunPianAgent extends Sms
9
{
10
    protected $config = [];
11
12
    private $apiKey;
13
14
    private $host = 'https://sms.yunpian.com';
15
    private $singleSendUrl = '/v2/sms/single_send.json';
16
17
    public function __construct($config)
18
    {
19
        $this->config = $config;
20
        $this->transformConfig();
21
    }
22
23
    protected function transformConfig()
24
    {
25
        $this->apiKey = Arr::pull($this->config, 'apiKey');
26
        $this->setSignName();
27
    }
28
29 View Code Duplication
    public function singlesSend($mobile, $send = true)
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...
30
    {
31
        $url = $this->host . $this->singleSendUrl;
32
33
        $postText = "【{$this->signName}】" . $this->content;
34
35
        $postData = [
36
            'text' => $postText,
37
            'apikey' => $this->apiKey,
38
            'mobile' => $mobile,
39
        ];
40
41
        if ($send) {
42
            return $this->curl($url, $postData);
43
        }
44
45
        return $postData;
46
    }
47
48
    /**
49
     * @param $url
50
     * @param array $postData
51
     * @return array $result
52
     * @return int $result[].code 返回0则成功,返回其它则错误
53
     * @return string $result[].msg 返回消息
54
     * @return mixed $result[].verifyCode 验证码
55
     */
56
    protected function curl($url, $postData)
57
    {
58
        $headers = array(
59
            'Accept:text/plain;charset=utf-8',
60
            'Content-Type:application/x-www-form-urlencoded', 'charset=utf-8'
61
        );
62
63
        $ch = curl_init();
64
        curl_setopt($ch, CURLOPT_TIMEOUT, 10);
65
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
66
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
67
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
68
        curl_setopt($ch, CURLOPT_URL, $url);
69
        curl_setopt($ch, CURLOPT_POST, 1);
70
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($postData));
71
        $httpResponse = $this->httpResponse($ch);
72
        $result = $this->transformerResponse($httpResponse);
73
        curl_close($ch);
74
75
        return $result;
76
    }
77
78
    protected function transformerResponse($httpResponse)
79
    {
80
        if (empty($httpResponse['error'])) {
81
            $result = array_except(
82
                json_decode($httpResponse['jsonData'], true),
83
                ['count', 'fee', 'sid', 'mobile', 'unit']
84
            );
85
            $result = array_merge(['verifyCode' => $this->verifyCode], $result);
86
        } else {
87
            $result = ['code' => time(), 'msg' => $httpResponse['error']];
88
        }
89
90
        return $result;
91
    }
92
}
93