1 | <?php |
||||
2 | |||||
3 | |||||
4 | namespace Jaby\Sms\Drivers; |
||||
5 | |||||
6 | |||||
7 | use Jaby\Sms\SmsInterface; |
||||
8 | |||||
9 | class smsir implements SmsInterface |
||||
10 | { |
||||
11 | protected $drive = 'smsir'; |
||||
12 | |||||
13 | protected $method; |
||||
14 | |||||
15 | protected $username; |
||||
16 | |||||
17 | protected $password; |
||||
18 | |||||
19 | protected $from; |
||||
20 | |||||
21 | protected $pattern_code; |
||||
22 | |||||
23 | protected $to; |
||||
24 | |||||
25 | protected $input_data; |
||||
26 | |||||
27 | protected $url; |
||||
28 | |||||
29 | protected $numbers; |
||||
30 | |||||
31 | protected $data; |
||||
32 | |||||
33 | protected $text; |
||||
34 | |||||
35 | protected $templateId; |
||||
36 | |||||
37 | /** |
||||
38 | * farazsms constructor. |
||||
39 | */ |
||||
40 | public function __construct() |
||||
41 | { |
||||
42 | $this->username = config('sms.drivers.' . $this->drive . '.username'); |
||||
43 | $this->password = config('sms.drivers.' . $this->drive . '.password'); |
||||
44 | $this->from = config('sms.drivers.' . $this->drive . '.from'); |
||||
45 | $this->url = config('sms.drivers.' . $this->drive . '.urlPattern'); |
||||
46 | } |
||||
47 | |||||
48 | /** |
||||
49 | * @return bool|mixed|string |
||||
50 | */ |
||||
51 | public function send() |
||||
52 | { |
||||
53 | if ($this->method == 'pattern') |
||||
54 | $res = $this->sendPattern(); |
||||
55 | else |
||||
56 | $res = $this->message($this->text); |
||||
57 | return $res; |
||||
58 | } |
||||
59 | |||||
60 | /** |
||||
61 | * @param $text |
||||
62 | * @return $this|mixed |
||||
63 | */ |
||||
64 | public function text($text) |
||||
65 | { |
||||
66 | $this->text = $text; |
||||
67 | |||||
68 | return $this; |
||||
69 | } |
||||
70 | |||||
71 | /** |
||||
72 | * @param null $pattern_code |
||||
0 ignored issues
–
show
Documentation
Bug
introduced
by
![]() |
|||||
73 | * @return $this|mixed |
||||
74 | */ |
||||
75 | public function pattern($pattern_code = null) |
||||
76 | { |
||||
77 | $this->method = 'pattern'; |
||||
78 | if ($pattern_code) |
||||
0 ignored issues
–
show
|
|||||
79 | $this->pattern_code = $pattern_code; |
||||
80 | return $this; |
||||
81 | } |
||||
82 | |||||
83 | /** |
||||
84 | * @param array $data |
||||
85 | * @return $this|mixed |
||||
86 | */ |
||||
87 | public function data(array $data) |
||||
88 | { |
||||
89 | $this->data = $data; |
||||
90 | |||||
91 | return $this; |
||||
92 | } |
||||
93 | |||||
94 | /** |
||||
95 | * @param $from |
||||
96 | * @return $this|mixed |
||||
97 | */ |
||||
98 | public function from($from) |
||||
99 | { |
||||
100 | $this->from = $from; |
||||
101 | |||||
102 | return $this; |
||||
103 | } |
||||
104 | |||||
105 | /** |
||||
106 | * @param array $numbers |
||||
107 | * @return $this|mixed |
||||
108 | */ |
||||
109 | public function to(array $numbers) |
||||
110 | { |
||||
111 | $this->numbers = $numbers; |
||||
112 | |||||
113 | return $this; |
||||
114 | } |
||||
115 | |||||
116 | /** |
||||
117 | * @param int|null $templateId |
||||
118 | * @return $this|mixed |
||||
119 | */ |
||||
120 | public function templateId($templateId = null) |
||||
121 | { |
||||
122 | $this->templateId = $templateId; |
||||
123 | |||||
124 | return $this; |
||||
125 | } |
||||
126 | |||||
127 | /** |
||||
128 | * @return bool|mixed|string |
||||
129 | */ |
||||
130 | public function sendPattern() |
||||
131 | { |
||||
132 | $inputs = $this->setPatternExceptions(); |
||||
133 | $this->setOTP($inputs); |
||||
134 | |||||
135 | $url = $this->url; |
||||
136 | $body = [ |
||||
137 | "mobile" => $inputs['mobile'][0], |
||||
138 | "templateId" => $inputs['templateId'], |
||||
139 | "parameters" => $inputs['parameters'] |
||||
140 | ]; |
||||
141 | |||||
142 | $handler = curl_init($url); |
||||
143 | curl_setopt($handler, CURLOPT_CUSTOMREQUEST, "POST"); |
||||
144 | curl_setopt($handler, CURLOPT_POSTFIELDS, $body); |
||||
145 | curl_setopt($handler, CURLOPT_RETURNTRANSFER, true); |
||||
146 | return curl_exec($handler); |
||||
147 | } |
||||
148 | |||||
149 | private function setPatternExceptions() |
||||
150 | { |
||||
151 | $parameters = $this->data; |
||||
152 | $this->validateData($parameters); |
||||
153 | |||||
154 | $mobile = $this->numbers; |
||||
155 | $this->validateMobile($mobile); |
||||
156 | |||||
157 | $templateId = $this->templateId; |
||||
158 | $this->validateTemplateId($templateId); |
||||
159 | |||||
160 | $checkParameter = (gettype($parameters[0]) != 'array') ? $parameters[0] : $parameters; |
||||
161 | $this->validateParameterStructure($checkParameter); |
||||
162 | |||||
163 | return compact('parameters', 'mobile', 'templateId'); |
||||
164 | } |
||||
165 | |||||
166 | private function validateData($parameters) |
||||
167 | { |
||||
168 | if (is_null($parameters)) |
||||
169 | throw new \Exception('The data must be set'); |
||||
170 | if (count($parameters) > 1) |
||||
171 | throw new \Exception('The data must have just one OTP code'); |
||||
172 | } |
||||
173 | |||||
174 | /** |
||||
175 | * @param $mobile |
||||
176 | * @return void |
||||
177 | * @throws \Exception |
||||
178 | */ |
||||
179 | private function validateMobile($mobile) |
||||
180 | { |
||||
181 | if (empty($mobile)) |
||||
182 | throw new \Exception('The mobile number must be set'); |
||||
183 | if (count($mobile) > 1) |
||||
184 | throw new \Exception('The OTP code must send to just one mobile number'); |
||||
185 | } |
||||
186 | |||||
187 | private function validateTemplateId($templateId) |
||||
188 | { |
||||
189 | if (is_null($templateId)) |
||||
190 | throw new \Exception('The templateId must be set'); |
||||
191 | } |
||||
192 | |||||
193 | private function validateParameterStructure($param) |
||||
194 | { |
||||
195 | if (!isset($param['name'])) |
||||
196 | throw new \Exception('The `name` parameter not defined in data'); |
||||
197 | if (!isset($param['value'])) |
||||
198 | throw new \Exception('The `value` parameter not defined in data'); |
||||
199 | } |
||||
200 | |||||
201 | /** |
||||
202 | * @param $inputs |
||||
203 | */ |
||||
204 | private function setOTP(&$inputs) |
||||
205 | { |
||||
206 | $pattern_code = $this->pattern_code; |
||||
207 | if (is_null($pattern_code)) { |
||||
208 | if (is_null($inputs['parameters'][0]['value'])) { |
||||
209 | $inputs['parameters'][0]['value'] = rand(100000, 999999); |
||||
210 | } |
||||
211 | } else |
||||
212 | $inputs['parameters'][0]['value'] = $pattern_code; |
||||
213 | |||||
214 | } |
||||
215 | |||||
216 | /** |
||||
217 | * @param $text |
||||
218 | * @return mixed |
||||
219 | */ |
||||
220 | public function message($text) |
||||
221 | { |
||||
222 | $inputs = $this->setMessageExceptions(); |
||||
223 | |||||
224 | $param = array |
||||
225 | ( |
||||
226 | 'lineNumber' => $this->from, |
||||
227 | 'MessageText' => $text, |
||||
228 | 'Mobiles' => json_encode($inputs['numbers']), |
||||
229 | ); |
||||
230 | |||||
231 | $handler = curl_init($this->url); |
||||
232 | curl_setopt($handler, CURLOPT_CUSTOMREQUEST, "POST"); |
||||
233 | curl_setopt($handler, CURLOPT_POSTFIELDS, $param); |
||||
234 | curl_setopt($handler, CURLOPT_RETURNTRANSFER, true); |
||||
235 | curl_setopt($handler, CURLOPT_HTTPHEADER, array( |
||||
236 | "X-API-KEY: {$inputs['apiKey']}", |
||||
237 | )); |
||||
238 | $response = curl_exec($handler); |
||||
239 | $response2 = json_decode($response); |
||||
0 ignored issues
–
show
It seems like
$response can also be of type true ; however, parameter $json of json_decode() does only seem to accept string , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
240 | |||||
241 | return $response2; |
||||
242 | } |
||||
243 | |||||
244 | private function setMessageExceptions() |
||||
245 | { |
||||
246 | $apiKey = config('sms.drivers.' . $this->drive . '.apiKey'); |
||||
247 | if ($apiKey == '') |
||||
248 | throw new \Exception('The apiKey of SMS.ir muse be set in config'); |
||||
249 | |||||
250 | $this->url = config('sms.drivers.' . $this->drive . '.urlNormal'); |
||||
251 | if ($this->url == '') |
||||
252 | throw new \Exception('The url of SMS.ir muse be set in config'); |
||||
253 | |||||
254 | $numbers = $this->numbers; |
||||
255 | if (count($numbers) < 1) |
||||
256 | throw new \Exception('The numbers of mobiles must be set'); |
||||
257 | |||||
258 | if ($this->from == '') |
||||
259 | throw new \Exception('The lineNumber of SMS.ir muse be set in config (set it in `from` key)'); |
||||
260 | |||||
261 | return compact('apiKey', 'numbers'); |
||||
262 | } |
||||
263 | } |
||||
264 |