Total Complexity | 46 |
Total Lines | 389 |
Duplicated Lines | 0 % |
Changes | 7 | ||
Bugs | 0 | Features | 0 |
Complex classes like SmsGateway often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use SmsGateway, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
10 | class SmsGateway implements SmsGatewayContract |
||
11 | { |
||
12 | protected $recipients; |
||
13 | |||
14 | protected $contents; |
||
15 | |||
16 | protected $gateway; |
||
17 | |||
18 | protected $apiEndpoint; |
||
19 | |||
20 | protected $username; |
||
21 | |||
22 | protected $senderid; |
||
23 | |||
24 | protected $templateid; |
||
25 | |||
26 | protected $securekey; |
||
27 | |||
28 | protected $tag; |
||
29 | |||
30 | protected $payload; |
||
31 | |||
32 | protected $response; |
||
33 | |||
34 | public function __construct() |
||
35 | { |
||
36 | return $this->setGateway() |
||
37 | ->setApiEndpoint() |
||
38 | ->setUsername() |
||
39 | ->setSenderid() |
||
40 | ->setSecurekey(); |
||
41 | } |
||
42 | |||
43 | public function sendSms() |
||
44 | { |
||
45 | $post = curl_init(); |
||
46 | //curl_setopt($post, CURLOPT_SSLVERSION, 5); // uncomment for systems supporting TLSv1.1 only |
||
47 | //curl_setopt($post, CURLOPT_SSLVERSION, 6); // use for systems supporting TLSv1.2 or comment the line |
||
48 | curl_setopt($post, CURLOPT_SSL_VERIFYHOST, 0); |
||
49 | curl_setopt($post, CURLOPT_SSL_VERIFYPEER, false); |
||
50 | curl_setopt($post, CURLOPT_URL, $this->getApiEndpoint()); |
||
51 | curl_setopt($post, CURLOPT_HTTPHEADER, array('Authorization: Bearer ' . $this->getSecurekey())); |
||
52 | curl_setopt($post, CURLOPT_POSTFIELDS, http_build_query($this->getPayload())); |
||
53 | curl_setopt($post, CURLOPT_RETURNTRANSFER, 1); |
||
54 | $resp['data'] = curl_exec($post); |
||
|
|||
55 | if($resp['data'] === false) { |
||
56 | $resp['error'] = curl_error($post); |
||
57 | } |
||
58 | curl_close($post); |
||
59 | $this->setResponse($resp); |
||
60 | return $this->getResponse(); |
||
61 | } |
||
62 | |||
63 | protected function hashPayload() |
||
64 | { |
||
65 | $payload = $this->buildConfigParams(); |
||
66 | if (array_key_exists('key', $payload)) { |
||
67 | $payload['key'] = hash('sha512', $this->getUsername().$this->getSenderid().$this->getContents().$this->getSecurekey()); |
||
68 | } |
||
69 | //dump("data to hash => ".$this->getUsername().$this->getSenderid().$this->getContents().$this->getSecurekey()); |
||
70 | $this->setPayload($payload); |
||
71 | return $this; |
||
72 | } |
||
73 | |||
74 | |||
75 | protected function buildConfigParams() |
||
76 | { |
||
77 | $configParams = array_combine( |
||
78 | config('smsgateway.' . config('smsgateway.default') . '.apiParams'), |
||
79 | config('smsgateway.' . config('smsgateway.default') . '.apiValues') |
||
80 | ); |
||
81 | |||
82 | $form_params = [ |
||
83 | config('smsgateway.' . config('smsgateway.default') . '.apiMobileNoParam') => $this->getRecipients(), |
||
84 | config('smsgateway.' . config('smsgateway.default') . '.apiSmsParam') => $this->getContents(), |
||
85 | config('smsgateway.' . config('smsgateway.default') . '.apiTemplateIdParam') => $this->getTemplateId(), |
||
86 | config('smsgateway.' . config('smsgateway.default') . '.apiTagParam') => $this->getTag(), |
||
87 | "smsservicetype" =>"singlemsg", |
||
88 | ]; |
||
89 | $data = array_merge($form_params, $configParams); |
||
90 | |||
91 | //dump($data); |
||
92 | return $data; |
||
93 | } |
||
94 | |||
95 | /** |
||
96 | * Gets the Message for the SMS |
||
97 | * @param string |
||
98 | * @return self |
||
99 | */ |
||
100 | public function withSms($contents = '') |
||
101 | { |
||
102 | $this->setContents($contents); |
||
103 | $this->hashPayload(); |
||
104 | return $this; |
||
105 | } |
||
106 | |||
107 | /** |
||
108 | * @return mixed |
||
109 | */ |
||
110 | public function getTemplateId() |
||
111 | { |
||
112 | return $this->templateid; |
||
113 | } |
||
114 | |||
115 | /** |
||
116 | * @param mixed $templateId |
||
117 | * |
||
118 | * @return self |
||
119 | */ |
||
120 | public function withTemplateId($templateId = '') |
||
121 | { |
||
122 | $this->templateid = $templateId == '' |
||
123 | ? config('smsgateway.' . $this->getGateway() . '.apiValues.apiTemplateId') |
||
124 | : trim($templateId); |
||
125 | //dump(config('smsgateway.' . $this->getGateway() . '.apiValues.apiTemplateId')); |
||
126 | return $this; |
||
127 | } |
||
128 | |||
129 | /** |
||
130 | * Gets the Tag for the SMS |
||
131 | * @param mixed |
||
132 | * @return self |
||
133 | */ |
||
134 | public function withTag($tag) |
||
135 | { |
||
136 | $this->setTag($tag); |
||
137 | return $this; |
||
138 | } |
||
139 | |||
140 | public function toRecipient($mobile = '') |
||
141 | { |
||
142 | return $this->setRecipients($mobile); |
||
143 | } |
||
144 | |||
145 | public function fromGateway($gateway = '') |
||
146 | { |
||
147 | return $this->setGateway($gateway); |
||
148 | } |
||
149 | |||
150 | /** |
||
151 | * @return mixed |
||
152 | */ |
||
153 | protected function getRecipients() |
||
154 | { |
||
155 | return $this->recipients; |
||
156 | } |
||
157 | |||
158 | /** |
||
159 | * @param mixed $recipients |
||
160 | * |
||
161 | * @return self |
||
162 | */ |
||
163 | protected function setRecipients($recipients) |
||
164 | { |
||
165 | $this->recipients = $recipients; |
||
166 | |||
167 | return $this; |
||
168 | } |
||
169 | |||
170 | /** |
||
171 | * @return mixed |
||
172 | */ |
||
173 | protected function getContents() |
||
176 | } |
||
177 | |||
178 | /** |
||
179 | * @param mixed $contents |
||
180 | * |
||
181 | * @return self |
||
182 | */ |
||
183 | protected function setContents($contents) |
||
184 | { |
||
185 | $this->contents = trim($contents); |
||
186 | |||
187 | return $this; |
||
188 | } |
||
189 | |||
190 | /** |
||
191 | * @return mixed |
||
192 | */ |
||
193 | protected function getGateway() |
||
194 | { |
||
195 | return $this->gateway; |
||
196 | } |
||
197 | |||
198 | /** |
||
199 | * @param mixed $gateway |
||
200 | * |
||
201 | * @return self |
||
202 | */ |
||
203 | protected function setGateway($gateway = '') |
||
204 | { |
||
205 | $this->gateway = $gateway == '' |
||
206 | ? config('smsgateway.default') |
||
207 | : trim($gateway); |
||
208 | return $this; |
||
209 | } |
||
210 | |||
211 | /** |
||
212 | * @return mixed |
||
213 | */ |
||
214 | protected function getUsername() |
||
217 | } |
||
218 | |||
219 | /** |
||
220 | * @param mixed $username |
||
221 | * |
||
222 | * @return self |
||
223 | */ |
||
224 | protected function setUsername($username = '') |
||
231 | } |
||
232 | |||
233 | /** |
||
234 | * @return mixed |
||
235 | */ |
||
236 | protected function getSenderid() |
||
237 | { |
||
238 | return $this->senderid; |
||
239 | } |
||
240 | |||
241 | /** |
||
242 | * @param mixed $senderid |
||
243 | * |
||
244 | * @return self |
||
245 | */ |
||
246 | protected function setSenderid($senderid = '') |
||
247 | { |
||
248 | $this->senderid = $senderid == '' |
||
249 | ? config('smsgateway.' . $this->getGateway() . '.apiValues.apiSenderId') |
||
250 | : trim($senderid); |
||
251 | //dump(config('smsgateway.' . $this->getGateway() . '.apiValues.apiSenderId')); |
||
252 | return $this; |
||
253 | } |
||
254 | |||
255 | /** |
||
256 | * @return mixed |
||
257 | */ |
||
258 | protected function getSecurekey() |
||
259 | { |
||
260 | return $this->securekey; |
||
261 | } |
||
262 | |||
263 | /** |
||
264 | * @param mixed $securekey |
||
265 | * |
||
266 | * @return self |
||
267 | */ |
||
268 | protected function setSecurekey($securekey = '') |
||
269 | { |
||
270 | $this->securekey = $securekey == '' |
||
271 | ? config('smsgateway.' . $this->getGateway() . '.apiValues.apiSecureKey') |
||
272 | : trim($securekey); |
||
273 | |||
274 | return $this; |
||
275 | } |
||
276 | |||
277 | /** |
||
278 | * @return mixed |
||
279 | */ |
||
280 | public function getTag() |
||
281 | { |
||
282 | return $this->tag; |
||
283 | } |
||
284 | |||
285 | /** |
||
286 | * @param mixed $tag |
||
287 | * |
||
288 | * @return self |
||
289 | */ |
||
290 | protected function setTag($tag) |
||
291 | { |
||
292 | $this->tag = $tag; |
||
293 | |||
294 | return $this; |
||
295 | } |
||
296 | |||
297 | /** |
||
298 | * @return mixed |
||
299 | */ |
||
300 | public function getResponse() |
||
301 | { |
||
302 | return json_encode($this->response, JSON_PRETTY_PRINT); |
||
303 | } |
||
304 | |||
305 | /** |
||
306 | * @param mixed $response |
||
307 | * |
||
308 | * @return self |
||
309 | */ |
||
310 | protected function setResponse($response) |
||
311 | { |
||
312 | $this->response = $response; |
||
313 | |||
314 | return $this; |
||
315 | } |
||
316 | |||
317 | /** |
||
318 | * @return mixed |
||
319 | */ |
||
320 | protected function getPayload() |
||
321 | { |
||
322 | return $this->payload; |
||
323 | } |
||
324 | |||
325 | /** |
||
326 | * @param mixed $payload |
||
327 | * |
||
328 | * @return self |
||
329 | */ |
||
330 | protected function setPayload($payload) |
||
331 | { |
||
332 | $this->payload = $payload; |
||
333 | |||
334 | return $this; |
||
335 | } |
||
336 | |||
337 | /** |
||
338 | * @return mixed |
||
339 | */ |
||
340 | protected function getApiEndpoint() |
||
343 | } |
||
344 | |||
345 | /** |
||
346 | * @param mixed $apiEndpoint |
||
347 | * |
||
348 | * @return self |
||
349 | */ |
||
350 | protected function setApiEndpoint() |
||
351 | { |
||
352 | $this->apiEndpoint = config('smsgateway.' . $this->getGateway() . '.apiEndpoint'); |
||
353 | return $this; |
||
354 | } |
||
355 | |||
356 | public function asOtpSms() |
||
357 | { |
||
358 | $payload = $this->getPayload(); |
||
359 | if (array_key_exists('smsservicetype', $payload)) { |
||
360 | $payload['smsservicetype'] = 'otpmsg'; |
||
361 | $this->setPayload($payload); |
||
362 | } |
||
363 | return $this; |
||
364 | } |
||
365 | |||
366 | public function asUnicodeOtpSms() |
||
374 | } |
||
375 | |||
376 | public function asUnicodeSms() |
||
377 | { |
||
378 | $payload = $this->getPayload(); |
||
379 | if (array_key_exists('smsservicetype', $payload)) { |
||
380 | $payload['smsservicetype'] = 'unicodemsg'; |
||
381 | $this->setPayload($payload); |
||
382 | } |
||
383 | return $this; |
||
384 | } |
||
385 | |||
386 | public function asBulkUnicodeSms() |
||
389 | } |
||
390 | |||
391 | public function asBulkSms() |
||
392 | { |
||
393 | $payload = $this->getPayload(); |
||
394 | if (array_key_exists('smsservicetype', $payload)) { |
||
395 | $payload['smsservicetype'] = 'bulkmsg'; |
||
396 | $this->setPayload($payload); |
||
399 | } |
||
400 | } |
||
401 |