Completed
Push — master ( 9cf391...4a311c )
by lan tian
8s
created

Agent   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 213
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 14
Bugs 4 Features 4
Metric Value
wmc 18
c 14
b 4
f 4
lcom 2
cbo 1
dl 0
loc 213
ccs 21
cts 21
cp 1
rs 10

11 Methods

Rating   Name   Duplication   Size   Complexity  
A config() 0 8 3
sendTemplateSms() 0 1 ?
B curl() 0 26 4
A result() 0 12 4
A __isset() 0 4 1
A __construct() 0 4 1
sendSms() 0 1 ?
sendContentSms() 0 1 ?
voiceVerify() 0 1 ?
B sockPost() 0 26 4
A __get() 0 4 1
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 of agent.
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 2
        }
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
     * Http post request.
96
     *
97
     * @codeCoverageIgnore
98
     *
99
     * @param       $url
100
     * @param array $query
101
     * @param       $port
102
     *
103
     * @return mixed
104
     */
105
    public static function sockPost($url, $query, $port = 80)
106
    {
107
        $data = '';
108
        $info = parse_url($url);
109
        $fp = fsockopen($info['host'], $port, $errno, $errstr, 30);
110
        if (!$fp) {
111
            return $data;
112
        }
113
        $head = 'POST ' . $info['path'] . " HTTP/1.0\r\n";
114
        $head .= 'Host: ' . $info['host'] . "\r\n";
115
        $head .= 'Referer: http://' . $info['host'] . $info['path'] . "\r\n";
116
        $head .= "Content-type: application/x-www-form-urlencoded\r\n";
117
        $head .= 'Content-Length: ' . strlen(trim($query)) . "\r\n";
118
        $head .= "\r\n";
119
        $head .= trim($query);
120
        $write = fwrite($fp, $head);
0 ignored issues
show
Unused Code introduced by
$write is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
121
        $header = '';
122
        while ($str = trim(fgets($fp, 4096))) {
123
            $header .= $str;
124
        }
125
        while (!feof($fp)) {
126
            $data .= fgets($fp, 4096);
127
        }
128
129
        return $data;
130
    }
131
132
    /**
133
     * cURl
134
     *
135
     * @codeCoverageIgnore
136
     *
137
     * @param string   $url    [请求的URL地址]
138
     * @param array    $params [请求的参数]
139
     * @param int|bool $isPost [是否采用POST形式]
140
     *
141
     * @return array ['request', 'response']
142
     *               request:是否请求成功
143
     *               response:响应数据
144
     */
145
    public static function curl($url, array $params = [], $isPost = false)
146
    {
147
        $request = true;
148
        $ch = curl_init();
149
        curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
150
        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.172 Safari/537.22');
151
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
152
        curl_setopt($ch, CURLOPT_TIMEOUT, 30);
153
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
154
        if ($isPost) {
155
            curl_setopt($ch, CURLOPT_POST, true);
156
            curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
157
            curl_setopt($ch, CURLOPT_URL, $url);
158
        } else {
159
            $params = http_build_query($params);
160
            curl_setopt($ch, CURLOPT_URL, $params ? "$url?$params" : $url);
161
        }
162
        $response = curl_exec($ch);
163
        if ($response === false) {
164
            $request = false;
165
            $response = curl_getinfo($ch);
166
        }
167
        curl_close($ch);
168
169
        return compact('request', 'response');
170
    }
171
172
    /**
173
     * Set/get result data.
174
     *
175
     * @param $name
176
     * @param $value
177
     *
178
     * @return mixed
179
     */
180 36
    public function result($name = null, $value = null)
181
    {
182 36
        if ($name === null) {
183 33
            return $this->result;
184
        }
185 33
        if (array_key_exists($name, $this->result)) {
186 33
            if ($value === null) {
187 6
                return $this->result["$name"];
188
            }
189 33
            $this->result["$name"] = $value;
190 22
        }
191 33
    }
192
193
    /**
194
     * Overload object properties.
195
     *
196
     * @param $name
197
     *
198
     * @return mixed
199
     */
200 12
    public function __get($name)
201
    {
202 12
        return $this->config($name);
203
    }
204
205
    /**
206
     * When using isset() or empty() on inaccessible object properties,
207
     * the __isset() overloading method will be called.
208
     *
209
     * @param $name
210
     *
211
     * @return bool
212
     */
213 3
    public function __isset($name)
214
    {
215 3
        return isset($this->config["$name"]);
216
    }
217
}
218