Completed
Push — master ( 382c3e...13cabb )
by lan tian
9s
created

Agent::__isset()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 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    => '',
26
        self::CODE    => 0,
27
    ];
28
29
    /**
30
     * Constructor.
31
     *
32
     * @param array $config
33
     */
34 27
    public function __construct(array $config = [])
35
    {
36 27
        $this->config = $config;
37 27
    }
38
39
    /**
40
     * SMS send process.
41
     *
42
     * @param       $tempId
43
     * @param       $to
44
     * @param array $tempData
45
     * @param       $content
46
     */
47
    abstract public function sendSms($tempId, $to, array $tempData, $content);
48
49
    /**
50
     * Content SMS send process.
51
     *
52
     * @param $to
53
     * @param $content
54
     */
55
    abstract public function sendContentSms($to, $content);
56
57
    /**
58
     * Template SMS send process.
59
     *
60
     * @param       $tempId
61
     * @param       $to
62
     * @param array $tempData
63
     */
64
    abstract public function sendTemplateSms($tempId, $to, array $tempData);
65
66
    /**
67
     * Voice verify send process.
68
     *
69
     * @param $to
70
     * @param $code
71
     */
72
    abstract public function voiceVerify($to, $code);
73
74
    /**
75
     * Http post request.
76
     *
77
     * @codeCoverageIgnore
78
     *
79
     * @param       $url
80
     * @param array $query
81
     * @param       $port
82
     *
83
     * @return mixed
84
     */
85
    public static function sockPost($url, $query, $port = 80)
86
    {
87
        $data = '';
88
        $info = parse_url($url);
89
        $fp = fsockopen($info['host'], $port, $errno, $errstr, 30);
90
        if (!$fp) {
91
            return $data;
92
        }
93
        $head = 'POST ' . $info['path'] . " HTTP/1.0\r\n";
94
        $head .= 'Host: ' . $info['host'] . "\r\n";
95
        $head .= 'Referer: http://' . $info['host'] . $info['path'] . "\r\n";
96
        $head .= "Content-type: application/x-www-form-urlencoded\r\n";
97
        $head .= 'Content-Length: ' . strlen(trim($query)) . "\r\n";
98
        $head .= "\r\n";
99
        $head .= trim($query);
100
        $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...
101
        $header = '';
102
        while ($str = trim(fgets($fp, 4096))) {
103
            $header .= $str;
104
        }
105
        while (!feof($fp)) {
106
            $data .= fgets($fp, 4096);
107
        }
108
109
        return $data;
110
    }
111
112
    /**
113
     * cURl
114
     *
115
     * @codeCoverageIgnore
116
     *
117
     * @param string   $url    [请求的URL地址]
118
     * @param array    $params [请求的参数]
119
     * @param int|bool $isPost [是否采用POST形式]
120
     *
121
     * @return array ['request', 'response']
122
     *               request:是否请求成功
123
     *               response:响应数据
124
     */
125
    public static function curl($url, array $params = [], $isPost = false)
126
    {
127
        $request = true;
128
        $ch = curl_init();
129
        curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
130
        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');
131
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
132
        curl_setopt($ch, CURLOPT_TIMEOUT, 30);
133
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
134
        if ($isPost) {
135
            curl_setopt($ch, CURLOPT_POST, true);
136
            curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
137
            curl_setopt($ch, CURLOPT_URL, $url);
138
        } else {
139
            $params = http_build_query($params);
140
            curl_setopt($ch, CURLOPT_URL, $params ? "$url?$params" : $url);
141
        }
142
        $response = curl_exec($ch);
143
        if ($response === false) {
144
            $request = false;
145
            $response = curl_getinfo($ch);
146
        }
147
        curl_close($ch);
148
149
        return compact('request', 'response');
150
    }
151
152
    /**
153
     * Set/get result data.
154
     *
155
     * @param $name
156
     * @param $value
157
     *
158
     * @return mixed
159
     */
160 36
    public function result($name = null, $value = null)
161
    {
162 36
        if ($name === null) {
163 33
            return $this->result;
164
        }
165 9
        if (array_key_exists($name, $this->result)) {
166 9
            if ($value === null) {
167 6
                return $this->result["$name"];
168
            }
169 9
            $this->result["$name"] = $value;
170 6
        }
171 9
    }
172
173
    /**
174
     * Overload object properties.
175
     *
176
     * @param $name
177
     *
178
     * @return mixed
179
     */
180 9
    public function __get($name)
181
    {
182 9
        if (array_key_exists($name, $this->config)) {
183 9
            return $this->config["$name"];
184
        }
185 3
    }
186
187
    /**
188
     * When using isset() or empty() on inaccessible object properties,
189
     * the __isset() overloading method will be called.
190
     *
191
     * @param $name
192
     *
193
     * @return bool
194
     */
195 3
    public function __isset($name)
196
    {
197 3
        return isset($this->config["$name"]);
198
    }
199
}
200