Completed
Pull Request — master (#6)
by lan tian
02:14
created

Agent::getResult()   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 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Toplan\PhpSms;
4
5
abstract class Agent
6
{
7
    /**
8
     * agent config
9
     *
10
     * @var array
11
     */
12
    protected $config;
13
14
    /**
15
     * sent result info
16
     *
17
     * @var array
18
     */
19
    protected $result = [
20
        'success' => false,
21
        'info'    => '',
22
        'code'    => 0,
23
    ];
24
25
    /**
26
     * construct for create a instance
27
     *
28
     * @param array $config
29
     */
30 18
    public function __construct(array $config = [])
31
    {
32 18
        $this->config = $config;
33 18
    }
34
35
    /**
36
     * sms send process
37
     *
38
     * @param       $tempId
39
     * @param       $to
40
     * @param array $data
41
     * @param       $content
42
     */
43
    abstract public function sendSms($tempId, $to, array $data, $content);
44
45
    /**
46
     * content sms send process
47
     *
48
     * @param $to
49
     * @param $content
50
     */
51
    abstract public function sendContentSms($to, $content);
52
53
    /**
54
     * template sms send process
55
     *
56
     * @param       $tempId
57
     * @param       $to
58
     * @param array $data
59
     */
60
    abstract public function sendTemplateSms($tempId, $to, array $data);
61
62
    /**
63
     * voice verify
64
     *
65
     * @param $to
66
     * @param $code
67
     */
68
    abstract public function voiceVerify($to, $code);
69
70
    /**
71
     * http post request
72
     * @codeCoverageIgnore
73
     *
74
     * @param       $url
75
     * @param array $query
76
     * @param       $port
77
     *
78
     * @return mixed
79
     */
80
    public function sockPost($url, $query, $port = 80)
81
    {
82
        $data = '';
83
        $info = parse_url($url);
84
        $fp = fsockopen($info['host'], $port, $errno, $errstr, 30);
85
        if (!$fp) {
86
            return $data;
87
        }
88
        $head = 'POST ' . $info['path'] . " HTTP/1.0\r\n";
89
        $head .= 'Host: ' . $info['host'] . "\r\n";
90
        $head .= 'Referer: http://' . $info['host'] . $info['path'] . "\r\n";
91
        $head .= "Content-type: application/x-www-form-urlencoded\r\n";
92
        $head .= 'Content-Length: ' . strlen(trim($query)) . "\r\n";
93
        $head .= "\r\n";
94
        $head .= trim($query);
95
        $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...
96
        $header = '';
97
        while ($str = trim(fgets($fp, 4096))) {
98
            $header .= $str;
99
        }
100
        while (!feof($fp)) {
101
            $data .= fgets($fp, 4096);
102
        }
103
104
        return $data;
105
    }
106
107
    /**
108
     * get result
109
     *
110
     * @return array
111
     */
112 27
    public function getResult()
113
    {
114 27
        return $this->result;
115
    }
116
117
    /**
118
     * overload object attribute
119
     *
120
     * @param $name
121
     *
122
     * @return mixed
123
     */
124 3
    public function __get($name)
125
    {
126 3
        if (array_key_exists($name, $this->config)) {
127 3
            return $this->config["$name"];
128
        }
129
130 3
        return;
131
    }
132
}
133