Total Complexity | 43 |
Total Lines | 364 |
Duplicated Lines | 0 % |
Changes | 5 | ||
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 $securekey; |
||
25 | |||
26 | protected $tag; |
||
27 | |||
28 | protected $payload; |
||
29 | |||
30 | protected $response; |
||
31 | |||
32 | public function __construct() |
||
33 | { |
||
34 | return $this->setGateway() |
||
35 | ->setApiEndpoint() |
||
36 | ->setUsername() |
||
37 | ->setSenderid() |
||
38 | ->setSecurekey(); |
||
39 | } |
||
40 | |||
41 | public function sendSms() |
||
42 | { |
||
43 | $post = curl_init(); |
||
44 | //curl_setopt($post, CURLOPT_SSLVERSION, 5); // uncomment for systems supporting TLSv1.1 only |
||
45 | //curl_setopt($post, CURLOPT_SSLVERSION, 6); // use for systems supporting TLSv1.2 or comment the line |
||
46 | curl_setopt($post, CURLOPT_SSL_VERIFYHOST, 0); |
||
47 | curl_setopt($post, CURLOPT_SSL_VERIFYPEER, false); |
||
48 | curl_setopt($post, CURLOPT_URL, $this->getApiEndpoint()); |
||
49 | curl_setopt($post, CURLOPT_HTTPHEADER, array('Authorization: Bearer ' . $this->getSecurekey())); |
||
50 | curl_setopt($post, CURLOPT_POSTFIELDS, http_build_query($this->getPayload())); |
||
51 | curl_setopt($post, CURLOPT_RETURNTRANSFER, 1); |
||
52 | $resp['data'] = curl_exec($post); |
||
|
|||
53 | if($resp['data'] === false) { |
||
54 | $resp['error'] = curl_error($post); |
||
55 | } |
||
56 | curl_close($post); |
||
57 | $this->setResponse($resp); |
||
58 | return $this->getResponse(); |
||
59 | } |
||
60 | |||
61 | protected function hashPayload() |
||
62 | { |
||
63 | $payload = $this->buildConfigParams(); |
||
64 | if (array_key_exists('key', $payload)) { |
||
65 | $payload['key'] = hash('sha512', $this->getUsername().$this->getSenderid().$this->getContents().$this->getSecurekey()); |
||
66 | } |
||
67 | //dump("data to hash => ".$this->getUsername().$this->getSenderid().$this->getContents().$this->getSecurekey()); |
||
68 | $this->setPayload($payload); |
||
69 | return $this; |
||
70 | } |
||
71 | |||
72 | |||
73 | protected function buildConfigParams() |
||
74 | { |
||
75 | $configParams = array_combine( |
||
76 | config('smsgateway.' . config('smsgateway.default') . '.apiParams'), |
||
77 | config('smsgateway.' . config('smsgateway.default') . '.apiValues') |
||
78 | ); |
||
79 | |||
80 | $form_params = [ |
||
81 | config('smsgateway.' . config('smsgateway.default') . '.apiMobileNoParam') => $this->getRecipients(), |
||
82 | config('smsgateway.' . config('smsgateway.default') . '.apiSmsParam') => $this->getContents(), |
||
83 | config('smsgateway.' . config('smsgateway.default') . '.apiTagParam') => $this->getTag(), |
||
84 | "smsservicetype" =>"singlemsg", |
||
85 | ]; |
||
86 | $data = array_merge($form_params, $configParams); |
||
87 | |||
88 | //dump($data); |
||
89 | return $data; |
||
90 | } |
||
91 | |||
92 | /** |
||
93 | * Gets the Message for the SMS |
||
94 | * @param string |
||
95 | * @return self |
||
96 | */ |
||
97 | public function withSms($contents = '') |
||
98 | { |
||
99 | $this->setContents($contents); |
||
100 | $this->hashPayload(); |
||
101 | return $this; |
||
102 | } |
||
103 | |||
104 | /** |
||
105 | * Gets the Tag for the SMS |
||
106 | * @param mixed |
||
107 | * @return self |
||
108 | */ |
||
109 | public function withTag($tag) |
||
110 | { |
||
111 | $this->setTag($tag); |
||
112 | return $this; |
||
113 | } |
||
114 | |||
115 | public function toRecipient($mobile = '') |
||
116 | { |
||
117 | return $this->setRecipients($mobile); |
||
118 | } |
||
119 | |||
120 | public function fromGateway($gateway = '') |
||
121 | { |
||
122 | return $this->setGateway($gateway); |
||
123 | } |
||
124 | |||
125 | /** |
||
126 | * @return mixed |
||
127 | */ |
||
128 | protected function getRecipients() |
||
129 | { |
||
130 | return $this->recipients; |
||
131 | } |
||
132 | |||
133 | /** |
||
134 | * @param mixed $recipients |
||
135 | * |
||
136 | * @return self |
||
137 | */ |
||
138 | protected function setRecipients($recipients) |
||
139 | { |
||
140 | $this->recipients = $recipients; |
||
141 | |||
142 | return $this; |
||
143 | } |
||
144 | |||
145 | /** |
||
146 | * @return mixed |
||
147 | */ |
||
148 | protected function getContents() |
||
151 | } |
||
152 | |||
153 | /** |
||
154 | * @param mixed $contents |
||
155 | * |
||
156 | * @return self |
||
157 | */ |
||
158 | protected function setContents($contents) |
||
159 | { |
||
160 | $this->contents = trim($contents); |
||
161 | |||
162 | return $this; |
||
163 | } |
||
164 | |||
165 | /** |
||
166 | * @return mixed |
||
167 | */ |
||
168 | protected function getGateway() |
||
169 | { |
||
170 | return $this->gateway; |
||
171 | } |
||
172 | |||
173 | /** |
||
174 | * @param mixed $gateway |
||
175 | * |
||
176 | * @return self |
||
177 | */ |
||
178 | protected function setGateway($gateway = '') |
||
179 | { |
||
180 | $this->gateway = $gateway == '' |
||
181 | ? config('smsgateway.default') |
||
182 | : trim($gateway); |
||
183 | return $this; |
||
184 | } |
||
185 | |||
186 | /** |
||
187 | * @return mixed |
||
188 | */ |
||
189 | protected function getUsername() |
||
192 | } |
||
193 | |||
194 | /** |
||
195 | * @param mixed $username |
||
196 | * |
||
197 | * @return self |
||
198 | */ |
||
199 | protected function setUsername($username = '') |
||
206 | } |
||
207 | |||
208 | /** |
||
209 | * @return mixed |
||
210 | */ |
||
211 | protected function getSenderid() |
||
212 | { |
||
213 | return $this->senderid; |
||
214 | } |
||
215 | |||
216 | /** |
||
217 | * @param mixed $senderid |
||
218 | * |
||
219 | * @return self |
||
220 | */ |
||
221 | protected function setSenderid($senderid = '') |
||
222 | { |
||
223 | $this->senderid = $senderid == '' |
||
224 | ? config('smsgateway.' . $this->getGateway() . '.apiValues.apiSenderId') |
||
225 | : trim($senderid); |
||
226 | //dump(config('smsgateway.' . $this->getGateway() . '.apiValues.apiSenderId')); |
||
227 | return $this; |
||
228 | } |
||
229 | |||
230 | /** |
||
231 | * @return mixed |
||
232 | */ |
||
233 | protected function getSecurekey() |
||
234 | { |
||
235 | return $this->securekey; |
||
236 | } |
||
237 | |||
238 | /** |
||
239 | * @param mixed $securekey |
||
240 | * |
||
241 | * @return self |
||
242 | */ |
||
243 | protected function setSecurekey($securekey = '') |
||
244 | { |
||
245 | $this->securekey = $securekey == '' |
||
246 | ? config('smsgateway.' . $this->getGateway() . '.apiValues.apiSecureKey') |
||
247 | : trim($securekey); |
||
248 | |||
249 | return $this; |
||
250 | } |
||
251 | |||
252 | /** |
||
253 | * @return mixed |
||
254 | */ |
||
255 | public function getTag() |
||
256 | { |
||
257 | return $this->tag; |
||
258 | } |
||
259 | |||
260 | /** |
||
261 | * @param mixed $tag |
||
262 | * |
||
263 | * @return self |
||
264 | */ |
||
265 | protected function setTag($tag) |
||
266 | { |
||
267 | $this->tag = $tag; |
||
268 | |||
269 | return $this; |
||
270 | } |
||
271 | |||
272 | /** |
||
273 | * @return mixed |
||
274 | */ |
||
275 | public function getResponse() |
||
276 | { |
||
277 | return json_encode($this->response, JSON_PRETTY_PRINT); |
||
278 | } |
||
279 | |||
280 | /** |
||
281 | * @param mixed $response |
||
282 | * |
||
283 | * @return self |
||
284 | */ |
||
285 | protected function setResponse($response) |
||
286 | { |
||
287 | $this->response = $response; |
||
288 | |||
289 | return $this; |
||
290 | } |
||
291 | |||
292 | /** |
||
293 | * @return mixed |
||
294 | */ |
||
295 | protected function getPayload() |
||
296 | { |
||
297 | return $this->payload; |
||
298 | } |
||
299 | |||
300 | /** |
||
301 | * @param mixed $payload |
||
302 | * |
||
303 | * @return self |
||
304 | */ |
||
305 | protected function setPayload($payload) |
||
306 | { |
||
307 | $this->payload = $payload; |
||
308 | |||
309 | return $this; |
||
310 | } |
||
311 | |||
312 | /** |
||
313 | * @return mixed |
||
314 | */ |
||
315 | protected function getApiEndpoint() |
||
318 | } |
||
319 | |||
320 | /** |
||
321 | * @param mixed $apiEndpoint |
||
322 | * |
||
323 | * @return self |
||
324 | */ |
||
325 | protected function setApiEndpoint() |
||
326 | { |
||
327 | $this->apiEndpoint = config('smsgateway.' . $this->getGateway() . '.apiEndpoint'); |
||
328 | return $this; |
||
329 | } |
||
330 | |||
331 | public function asOtpSms() |
||
332 | { |
||
333 | $payload = $this->getPayload(); |
||
334 | if (array_key_exists('smsservicetype', $payload)) { |
||
335 | $payload['smsservicetype'] = 'otpmsg'; |
||
336 | $this->setPayload($payload); |
||
337 | } |
||
338 | return $this; |
||
339 | } |
||
340 | |||
341 | public function asUnicodeOtpSms() |
||
349 | } |
||
350 | |||
351 | public function asUnicodeSms() |
||
352 | { |
||
353 | $payload = $this->getPayload(); |
||
354 | if (array_key_exists('smsservicetype', $payload)) { |
||
355 | $payload['smsservicetype'] = 'unicodemsg'; |
||
356 | $this->setPayload($payload); |
||
357 | } |
||
358 | return $this; |
||
359 | } |
||
360 | |||
361 | public function asBulkUnicodeSms() |
||
364 | } |
||
365 | |||
366 | public function asBulkSms() |
||
367 | { |
||
368 | $payload = $this->getPayload(); |
||
369 | if (array_key_exists('smsservicetype', $payload)) { |
||
370 | $payload['smsservicetype'] = 'bulkmsg'; |
||
374 | } |
||
375 | } |
||
376 |