Total Complexity | 36 |
Total Lines | 331 |
Duplicated Lines | 0 % |
Changes | 5 | ||
Bugs | 1 | Features | 0 |
1 | <?php |
||
10 | class TurboSMS implements TurboSMSInterface |
||
11 | { |
||
12 | use ViberAddition, StartTimeAddition; |
||
1 ignored issue
–
show
|
|||
13 | |||
14 | protected $api; |
||
15 | protected $viberSender; |
||
16 | protected $smsSender; |
||
17 | protected $startTime; |
||
18 | protected $isTest; |
||
19 | |||
20 | protected $baseUri = 'https://api.turbosms.ua/'; |
||
21 | |||
22 | /** |
||
23 | * TurboSMS constructor main settings. |
||
24 | */ |
||
25 | public function __construct() |
||
31 | } |
||
32 | |||
33 | /** |
||
34 | * @return integer|null |
||
35 | */ |
||
36 | public function getBalance() |
||
52 | } |
||
53 | |||
54 | /** |
||
55 | * @return array |
||
56 | */ |
||
57 | public function getBalanceJson() |
||
58 | { |
||
59 | $module = 'user'; |
||
60 | $method = 'balance.json'; |
||
61 | |||
62 | $url = $this->baseUri.$module.'/'.$method; |
||
63 | $body = []; |
||
64 | |||
65 | $answers = $this->getResponse($url, $body); |
||
66 | |||
67 | return $answers; |
||
68 | } |
||
69 | |||
70 | /** |
||
71 | * @param string|array $messageId |
||
72 | * @return array |
||
73 | */ |
||
74 | public function getItemsStatus($messageId) |
||
75 | { |
||
76 | $module = 'message'; |
||
77 | $method = 'status.json'; |
||
78 | |||
79 | $messages = collect($messageId)->values()->all(); |
||
80 | |||
81 | $url = $this->baseUri.$module.'/'.$method; |
||
82 | $body = [ |
||
83 | 'messages' => $messages, |
||
84 | ]; |
||
85 | |||
86 | $answers = $this->getResponse($url, $body); |
||
87 | |||
88 | return $answers; |
||
89 | } |
||
90 | |||
91 | /** |
||
92 | * @param string|array $recipients |
||
93 | * @param string $text |
||
94 | * @param string|null $type |
||
95 | * @return array |
||
96 | */ |
||
97 | public function sendMessages($recipients, $text, $type = null) |
||
98 | { |
||
99 | $module = 'message'; |
||
100 | $method = 'send.json'; |
||
101 | |||
102 | if (! $text) { |
||
103 | return [ |
||
104 | 'success' => false, |
||
105 | 'result' => null, |
||
106 | 'info' => __('turbosms::turbosms.empty_text'), |
||
107 | ]; |
||
108 | } |
||
109 | |||
110 | $phone = collect($recipients); |
||
111 | $phones = $this->phonesTrim($phone); |
||
112 | $phones = $phones->values()->all(); |
||
113 | |||
114 | $url = $this->baseUri.$module.'/'.$method; |
||
115 | $body = [ |
||
116 | 'recipients' => $phones, |
||
117 | ]; |
||
118 | |||
119 | //SMS |
||
120 | if ($type == 'sms' || ! $type) { |
||
121 | $body = $this->bodySMS($body, $text); |
||
122 | } |
||
123 | //VIBER |
||
124 | if ($type == 'viber') { |
||
125 | $body = $this->bodyViber($body, $text); |
||
126 | } |
||
127 | //Гибридная доставка |
||
128 | if ($type == 'both') { |
||
129 | $body = $this->bodySMS($body, $text); |
||
130 | $body = $this->bodyViber($body, $text); |
||
131 | } |
||
132 | |||
133 | //Доставка в определенное время |
||
134 | if ($this->startTime) { |
||
135 | $body['start_time'] = $this->startTime; |
||
136 | } |
||
137 | |||
138 | $answers = $this->getResponse($url, $body); |
||
139 | |||
140 | return $answers; |
||
141 | } |
||
142 | |||
143 | /** |
||
144 | * @param string $url |
||
145 | * @param array $body |
||
146 | * @return array |
||
147 | */ |
||
148 | public function getResponse($url, $body) |
||
149 | { |
||
150 | if ($this->isTest) { |
||
151 | return [ |
||
152 | 'success' => false, |
||
153 | 'result' => [ |
||
154 | 'url' => $url, |
||
155 | 'body' => $body, |
||
156 | ], |
||
157 | 'info' => __('turbosms::turbosms.test_mode'), |
||
158 | ]; |
||
159 | } |
||
160 | |||
161 | $response = Http::timeout(3) |
||
162 | ->retry(2, 200) |
||
163 | ->withHeaders([ |
||
164 | 'Accept' => 'application/json', |
||
165 | 'Authorization' => 'Bearer '.$this->api, |
||
166 | 'Content-Type' => 'application/json', |
||
167 | ])->post($url, $body); |
||
168 | |||
169 | if ($response->failed()) { |
||
170 | return [ |
||
171 | 'success' => false, |
||
172 | 'result' => null, |
||
173 | 'info' => __('turbosms::turbosms.error_data'), |
||
174 | ]; |
||
175 | } |
||
176 | |||
177 | $answer = $response->json(); |
||
178 | if (! isset($answer['response_result']) || ! $answer['response_result']) { |
||
179 | $error = __('turbosms::turbosms.response_status.'.$answer['response_status']); |
||
180 | |||
181 | return [ |
||
182 | 'success' => false, |
||
183 | 'result' => null, |
||
184 | 'info' => $error, |
||
185 | ]; |
||
186 | } |
||
187 | |||
188 | $info = __('turbosms::turbosms.response_status.'.$answer['response_status']); |
||
189 | |||
190 | return [ |
||
191 | 'success' => true, |
||
192 | 'result' => $answer['response_result'], |
||
193 | 'info' => $info, |
||
194 | ]; |
||
195 | } |
||
196 | |||
197 | // =================== SUPPORT =================== |
||
198 | |||
199 | /** |
||
200 | * @return null|string |
||
201 | */ |
||
202 | public function getApi() |
||
203 | { |
||
204 | if (is_null($this->api)) { |
||
205 | $this->api = config('turbosms.api_key'); |
||
206 | } |
||
207 | |||
208 | return $this->api; |
||
209 | } |
||
210 | |||
211 | /** |
||
212 | * @param string $api |
||
213 | * @return this |
||
214 | */ |
||
215 | public function setApi($api) |
||
216 | { |
||
217 | $this->api = $api; |
||
218 | |||
219 | return $this; |
||
220 | } |
||
221 | |||
222 | /** |
||
223 | * @return null|string |
||
224 | */ |
||
225 | public function getViberSender() |
||
226 | { |
||
227 | if (is_null($this->viberSender)) { |
||
1 ignored issue
–
show
|
|||
228 | $this->viberSender = config('turbosms.viber_sender'); |
||
229 | } |
||
230 | |||
231 | return $this->viberSender; |
||
232 | } |
||
233 | |||
234 | /** |
||
235 | * @param string $viberSender |
||
236 | * @return this |
||
237 | */ |
||
238 | public function setViberSender($viberSender) |
||
239 | { |
||
240 | $this->viberSender = $viberSender; |
||
241 | |||
242 | return $this; |
||
243 | } |
||
244 | |||
245 | /** |
||
246 | * @return string $smsSender |
||
247 | */ |
||
248 | public function getSMSSender() |
||
249 | { |
||
250 | if (is_null($this->smsSender)) { |
||
1 ignored issue
–
show
|
|||
251 | $this->smsSender = config('turbosms.sms_sender'); |
||
252 | } |
||
253 | |||
254 | return $this->smsSender; |
||
255 | } |
||
256 | |||
257 | /** |
||
258 | * @param string $smsSender |
||
259 | * @return this |
||
260 | */ |
||
261 | public function setSMSSender($smsSender) |
||
262 | { |
||
263 | $this->smsSender = $smsSender; |
||
264 | |||
265 | return $this; |
||
266 | } |
||
267 | |||
268 | |||
269 | /** |
||
270 | * Убираем у телефонов пробелы, скобки, минусы и плюсы. |
||
271 | * |
||
272 | * @param string|array $phones |
||
273 | * @return array $phones |
||
274 | */ |
||
275 | public function phonesTrim($phones) |
||
283 | } |
||
284 | |||
285 | /** |
||
286 | * Формируем $boby для SMS |
||
287 | * |
||
288 | * @param array $body |
||
289 | * @param string $text |
||
290 | * @return array $body |
||
291 | */ |
||
292 | public function bodySMS($body, $text) |
||
300 | } |
||
301 | |||
302 | /** |
||
303 | * Формируем $boby для Viber |
||
304 | * |
||
305 | * @param array $body |
||
306 | * @param string $text |
||
307 | * @return array $body |
||
308 | */ |
||
309 | public function bodyViber($body, $text) |
||
341 | } |
||
342 | } |
||
343 |