Completed
Pull Request — master (#88)
by lan tian
02:01
created

Agent::sockPost()   B

Complexity

Conditions 4
Paths 5

Size

Total Lines 26
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 26
rs 8.5806
ccs 0
cts 0
cp 0
nc 5
cc 4
eloc 20
nop 3
crap 20
1
<?php
2
3
namespace Toplan\PhpSms;
4
5
abstract class Agent
6
{
7
    const SUCCESS = 'success';
8
    const INFO = 'info';
9
    const CODE = 'code';
10
11
    /**
12
     * The configuration information.
13
     *
14
     * @var array
15
     */
16
    protected $config = [];
17
18
    /**
19
     * The result data.
20
     *
21
     * @var array
22
     */
23
    protected $result = [
24
        self::SUCCESS => false,
25
        self::INFO    => null,
26
        self::CODE    => 0,
27
    ];
28
29
    /**
30
     * Constructor.
31
     *
32
     * @param array $config
33
     */
34 30
    public function __construct(array $config = [])
35
    {
36 30
        $this->config($config);
37 30
    }
38
39
    /**
40
     * Get or set the configuration information of agent.
41
     *
42
     * @param mixed $key
43
     * @param mixed $value
44
     * @param bool  $override
45
     *
46
     * @return mixed
47
     */
48 30
    public function config($key = null, $value = null, $override = false)
49
    {
50 30
        if (is_array($key) && is_bool($value)) {
51 3
            $override = $value;
52 3
        }
53
54 30
        return Util::operateArray($this->config, $key, $value, null, null, $override);
55
    }
56
57
    /**
58
     * SMS send process.
59
     *
60
     * @param       $to
61
     * @param       $content
62
     * @param       $tempId
63
     * @param array $tempData
64
     */
65
    abstract public function sendSms($to, $content, $tempId, array $tempData);
66
67
    /**
68
     * Content SMS send process.
69
     *
70
     * @param $to
71
     * @param $content
72
     */
73
    abstract public function sendContentSms($to, $content);
74
75
    /**
76
     * Template SMS send process.
77
     *
78
     * @param       $to
79
     * @param       $tempId
80
     * @param array $tempData
81
     */
82
    abstract public function sendTemplateSms($to, $tempId, array $tempData);
83
84
    /**
85
     * Voice verify send process.
86
     *
87
     * @param       $to
88
     * @param       $code
89
     * @param       $tempId
90
     * @param array $tempData
91
     */
92
    abstract public function voiceVerify($to, $code, $tempId, array $tempData);
93
94
    /**
95
     * cURl
96
     *
97
     * @codeCoverageIgnore
98
     *
99
     * @param string $url    [请求的URL地址]
100
     * @param array  $params [请求的参数]
101
     * @param bool   $post   [是否采用POST形式]
102
     * @param array  $opts   [curl设置项]
103
     *
104
     * @return array ['request', 'response']
105
     *               request:是否请求成功
106
     *               response:响应数据
107
     */
108
    public static function curl($url, $params = [], $post = false, array $opts = [])
109
    {
110
        if (is_array($post)) {
111
            $opts = $post;
112
            $post = false;
113
        }
114
        if (is_bool($params)) {
115
            $post = $params;
116
            $params = [];
117
        }
118
119
        $ch = curl_init();
120
        curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
121
        curl_setopt($ch, CURLOPT_HEADER, false);
122
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
123
        curl_setopt($ch, CURLOPT_TIMEOUT, 30);
124
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
125
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
126
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
127
        if ($post) {
128
            curl_setopt($ch, CURLOPT_POST, true);
129
            curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
130
            curl_setopt($ch, CURLOPT_URL, $url);
131
        } else {
132
            $params = http_build_query($params);
133
            curl_setopt($ch, CURLOPT_URL, $params ? "$url?$params" : $url);
134
        }
135
        foreach ($opts as $key => $value) {
136
            curl_setopt($ch, $key, $value);
137
        }
138
        $response = curl_exec($ch);
139
140
        $request = $response !== false;
141
        if (!$request) {
142
            $response = curl_getinfo($ch);
143
        }
144
        curl_close($ch);
145
146
        return compact('request', 'response');
147
    }
148
149
    /**
150
     * Get or set the result data.
151
     *
152
     * @param $name
153
     * @param $value
154
     *
155
     * @return mixed
156
     */
157 36
    public function result($name = null, $value = null)
158
    {
159 36
        if ($name === null) {
160 33
            return $this->result;
161
        }
162 33
        if (array_key_exists($name, $this->result)) {
163 33
            if ($value === null) {
164 6
                return $this->result["$name"];
165
            }
166 33
            $this->result["$name"] = $value;
167 33
        }
168 33
    }
169
170
    /**
171
     * Overload object properties.
172
     *
173
     * @param $name
174
     *
175
     * @return mixed
176
     */
177 12
    public function __get($name)
178
    {
179 12
        return $this->config($name);
180
    }
181
182
    /**
183
     * When using isset() or empty() on inaccessible object properties,
184
     * the __isset() overloading method will be called.
185
     *
186
     * @param $name
187
     *
188
     * @return bool
189
     */
190 3
    public function __isset($name)
191
    {
192 3
        return isset($this->config[$name]);
193
    }
194
}
195