1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Laravelsms\Sms\Contracts; |
4
|
|
|
|
5
|
|
|
abstract class Sms |
6
|
|
|
{ |
7
|
|
|
protected $signName; |
8
|
|
|
protected $templateId; |
9
|
|
|
protected $content; |
10
|
|
|
protected $templateVar = []; |
11
|
|
|
protected $verifyCode; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @return string |
15
|
|
|
*/ |
16
|
|
|
private function getAgentName() |
17
|
|
|
{ |
18
|
|
|
$formattedClassName = explode('\\', get_called_class()); |
19
|
|
|
if (count($formattedClassName) > 0) { |
20
|
|
|
$agentFileName = end($formattedClassName); |
21
|
|
|
$agents = config("sms.agents"); |
22
|
|
|
foreach ($agents as $key => $value) { |
23
|
|
|
if (strcmp($value['executableFile'], $agentFileName) == 0) |
24
|
|
|
return $key; |
25
|
|
|
} |
26
|
|
|
} |
27
|
|
|
throw new \InvalidArgumentException("Unauthorized access."); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @return string |
32
|
|
|
*/ |
33
|
|
|
protected function getTemplateContentByConfig() |
34
|
|
|
{ |
35
|
|
|
$name = $this->getAgentName(); |
36
|
|
|
return config("sms.agents.{$name}.templateContent"); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param integer $time |
41
|
|
|
*/ |
42
|
|
|
public function setContentByVerifyCode($time = null) |
43
|
|
|
{ |
44
|
|
|
$this->verifyCode = $this->makeRandom(); |
45
|
|
|
if (empty($this->content)) { |
46
|
|
|
$this->content = $this->getTemplateContentByConfig(); |
47
|
|
|
} |
48
|
|
|
$this->content = str_replace('{verifyCode}', $this->verifyCode, $this->content); |
49
|
|
|
if (!empty($time)) { |
50
|
|
|
$this->content = str_replace('{time}', $time, $this->content); |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param array $templateVar |
56
|
|
|
*/ |
57
|
|
|
public function setContentByCustomVar($templateVar = []) |
58
|
|
|
{ |
59
|
|
|
if (empty($this->content)) { |
60
|
|
|
$this->content = $this->getTemplateContentByConfig(); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
$count = count($templateVar); |
64
|
|
|
if (is_array($templateVar) && $count > 0) { |
65
|
|
|
foreach ($templateVar as $key => $value) { |
66
|
|
|
$this->content = str_replace("{" . $key . "}", $value, $this->content); |
67
|
|
|
} |
68
|
|
|
} else { |
69
|
|
|
$this->content = ''; |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param string $content |
75
|
|
|
*/ |
76
|
|
|
public function setContent($content) |
77
|
|
|
{ |
78
|
|
|
$this->content = is_string(trim($content)) ? $content : ''; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @return string |
83
|
|
|
*/ |
84
|
|
|
public function getContent() |
85
|
|
|
{ |
86
|
|
|
return $this->content; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param array $templateVar |
91
|
|
|
* @param bool $hasKey |
92
|
|
|
*/ |
93
|
|
|
public function setTemplateVar($templateVar = [], $hasKey = false) |
94
|
|
|
{ |
95
|
|
|
$count = count($templateVar); |
96
|
|
|
|
97
|
|
|
if (is_array($templateVar) && $count > 0) { |
98
|
|
|
foreach ($templateVar as $key => $value) { |
99
|
|
|
if ($hasKey) { |
100
|
|
View Code Duplication |
if ($value == 'verifyCode') { |
|
|
|
|
101
|
|
|
$this->verifyCode = $this->makeRandom(); |
102
|
|
|
$this->templateVar[$key] = "{$this->verifyCode}"; |
103
|
|
|
} else { |
104
|
|
|
$this->templateVar[$key] = "{$value}"; |
105
|
|
|
} |
106
|
|
View Code Duplication |
} else { |
|
|
|
|
107
|
|
|
if ($value == 'verifyCode') { |
108
|
|
|
$this->verifyCode = $this->makeRandom(); |
109
|
|
|
$this->templateVar[] = "{$this->verifyCode}"; |
110
|
|
|
} else { |
111
|
|
|
$this->templateVar[] = "{$value}"; |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
} else { |
116
|
|
|
$this->templateVar = []; |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @return string |
122
|
|
|
*/ |
123
|
|
|
public function getTemplateVar() |
124
|
|
|
{ |
125
|
|
|
return $this->templateVar; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @param string $signName |
130
|
|
|
*/ |
131
|
|
|
public function setSignName($signName = null) |
132
|
|
|
{ |
133
|
|
|
$this->signName = trim($signName) ?: trim(config('sms.signName'), '{}'); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @return string |
138
|
|
|
*/ |
139
|
|
|
public function getSignName() |
140
|
|
|
{ |
141
|
|
|
return $this->signName; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @param mixed $id |
146
|
|
|
*/ |
147
|
|
|
public function setTemplateId($id = null) |
148
|
|
|
{ |
149
|
|
|
$this->templateId = $id ?: 1; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* @return string |
154
|
|
|
*/ |
155
|
|
|
public function getTemplateId() |
156
|
|
|
{ |
157
|
|
|
return $this->templateId; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @return void |
162
|
|
|
*/ |
163
|
|
|
abstract protected function transformConfig(); |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* @param string $mobile |
167
|
|
|
* @param bool $send |
168
|
|
|
* @return mixed |
169
|
|
|
*/ |
170
|
|
|
abstract protected function singlesSend($mobile, $send = true); |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* @param string $url |
174
|
|
|
* @param array $params |
175
|
|
|
* @return array |
176
|
|
|
*/ |
177
|
|
|
abstract protected function curl($url, $params); |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* @param $ch |
181
|
|
|
* @return array |
182
|
|
|
*/ |
183
|
|
|
abstract protected function transformerResponse($ch); |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* @param $ch |
187
|
|
|
* @return array |
188
|
|
|
*/ |
189
|
|
|
protected function httpResponse($ch) |
190
|
|
|
{ |
191
|
|
|
$retry = 0; |
192
|
|
|
do { |
193
|
|
|
$jsonData = curl_exec($ch); |
194
|
|
|
$retry++; |
195
|
|
|
} while (curl_errno($ch) && $retry < 3); |
196
|
|
|
|
197
|
|
|
if (curl_errno($ch)) { |
198
|
|
|
$response = ['error' => 1, 'msg' => curl_error($ch)]; |
199
|
|
|
} else { |
200
|
|
|
$response = ['error' => 0, 'jsonData' => $jsonData]; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
return $response; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* @return int |
208
|
|
|
*/ |
209
|
|
|
protected function makeRandom() |
210
|
|
|
{ |
211
|
|
|
return random_int(100000, 999999); |
212
|
|
|
} |
213
|
|
|
} |
214
|
|
|
|
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.