| Total Complexity | 45 |
| Total Lines | 378 |
| Duplicated Lines | 0 % |
| Changes | 6 | ||
| 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() |
||
| 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() |
||
| 90 | } |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Gets the Message for the SMS |
||
| 94 | * @param string |
||
| 95 | * @return self |
||
| 96 | */ |
||
| 97 | public function withSms($contents = '') |
||
| 102 | } |
||
| 103 | |||
| 104 | /** |
||
| 105 | * @param mixed $templateId |
||
| 106 | * |
||
| 107 | * @return self |
||
| 108 | */ |
||
| 109 | public function withTemplateId($templateId = '') |
||
| 110 | { |
||
| 111 | $this->senderid = $templateId == '' |
||
| 112 | ? config('smsgateway.' . $this->getGateway() . '.apiValues.apiTemplateId') |
||
| 113 | : trim($templateId); |
||
| 114 | //dump(config('smsgateway.' . $this->getGateway() . '.apiValues.apiTemplateId')); |
||
| 115 | return $this; |
||
| 116 | } |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Gets the Tag for the SMS |
||
| 120 | * @param mixed |
||
| 121 | * @return self |
||
| 122 | */ |
||
| 123 | public function withTag($tag) |
||
| 124 | { |
||
| 125 | $this->setTag($tag); |
||
| 126 | return $this; |
||
| 127 | } |
||
| 128 | |||
| 129 | public function toRecipient($mobile = '') |
||
| 130 | { |
||
| 131 | return $this->setRecipients($mobile); |
||
| 132 | } |
||
| 133 | |||
| 134 | public function fromGateway($gateway = '') |
||
| 135 | { |
||
| 136 | return $this->setGateway($gateway); |
||
| 137 | } |
||
| 138 | |||
| 139 | /** |
||
| 140 | * @return mixed |
||
| 141 | */ |
||
| 142 | protected function getRecipients() |
||
| 143 | { |
||
| 144 | return $this->recipients; |
||
| 145 | } |
||
| 146 | |||
| 147 | /** |
||
| 148 | * @param mixed $recipients |
||
| 149 | * |
||
| 150 | * @return self |
||
| 151 | */ |
||
| 152 | protected function setRecipients($recipients) |
||
| 153 | { |
||
| 154 | $this->recipients = $recipients; |
||
| 155 | |||
| 156 | return $this; |
||
| 157 | } |
||
| 158 | |||
| 159 | /** |
||
| 160 | * @return mixed |
||
| 161 | */ |
||
| 162 | protected function getContents() |
||
| 165 | } |
||
| 166 | |||
| 167 | /** |
||
| 168 | * @param mixed $contents |
||
| 169 | * |
||
| 170 | * @return self |
||
| 171 | */ |
||
| 172 | protected function setContents($contents) |
||
| 173 | { |
||
| 174 | $this->contents = trim($contents); |
||
| 175 | |||
| 176 | return $this; |
||
| 177 | } |
||
| 178 | |||
| 179 | /** |
||
| 180 | * @return mixed |
||
| 181 | */ |
||
| 182 | protected function getGateway() |
||
| 183 | { |
||
| 184 | return $this->gateway; |
||
| 185 | } |
||
| 186 | |||
| 187 | /** |
||
| 188 | * @param mixed $gateway |
||
| 189 | * |
||
| 190 | * @return self |
||
| 191 | */ |
||
| 192 | protected function setGateway($gateway = '') |
||
| 193 | { |
||
| 194 | $this->gateway = $gateway == '' |
||
| 195 | ? config('smsgateway.default') |
||
| 196 | : trim($gateway); |
||
| 197 | return $this; |
||
| 198 | } |
||
| 199 | |||
| 200 | /** |
||
| 201 | * @return mixed |
||
| 202 | */ |
||
| 203 | protected function getUsername() |
||
| 206 | } |
||
| 207 | |||
| 208 | /** |
||
| 209 | * @param mixed $username |
||
| 210 | * |
||
| 211 | * @return self |
||
| 212 | */ |
||
| 213 | protected function setUsername($username = '') |
||
| 220 | } |
||
| 221 | |||
| 222 | /** |
||
| 223 | * @return mixed |
||
| 224 | */ |
||
| 225 | protected function getSenderid() |
||
| 226 | { |
||
| 227 | return $this->senderid; |
||
| 228 | } |
||
| 229 | |||
| 230 | /** |
||
| 231 | * @param mixed $senderid |
||
| 232 | * |
||
| 233 | * @return self |
||
| 234 | */ |
||
| 235 | protected function setSenderid($senderid = '') |
||
| 236 | { |
||
| 237 | $this->senderid = $senderid == '' |
||
| 238 | ? config('smsgateway.' . $this->getGateway() . '.apiValues.apiSenderId') |
||
| 239 | : trim($senderid); |
||
| 240 | //dump(config('smsgateway.' . $this->getGateway() . '.apiValues.apiSenderId')); |
||
| 241 | return $this; |
||
| 242 | } |
||
| 243 | |||
| 244 | /** |
||
| 245 | * @return mixed |
||
| 246 | */ |
||
| 247 | protected function getSecurekey() |
||
| 248 | { |
||
| 249 | return $this->securekey; |
||
| 250 | } |
||
| 251 | |||
| 252 | /** |
||
| 253 | * @param mixed $securekey |
||
| 254 | * |
||
| 255 | * @return self |
||
| 256 | */ |
||
| 257 | protected function setSecurekey($securekey = '') |
||
| 258 | { |
||
| 259 | $this->securekey = $securekey == '' |
||
| 260 | ? config('smsgateway.' . $this->getGateway() . '.apiValues.apiSecureKey') |
||
| 261 | : trim($securekey); |
||
| 262 | |||
| 263 | return $this; |
||
| 264 | } |
||
| 265 | |||
| 266 | /** |
||
| 267 | * @return mixed |
||
| 268 | */ |
||
| 269 | public function getTag() |
||
| 270 | { |
||
| 271 | return $this->tag; |
||
| 272 | } |
||
| 273 | |||
| 274 | /** |
||
| 275 | * @param mixed $tag |
||
| 276 | * |
||
| 277 | * @return self |
||
| 278 | */ |
||
| 279 | protected function setTag($tag) |
||
| 280 | { |
||
| 281 | $this->tag = $tag; |
||
| 282 | |||
| 283 | return $this; |
||
| 284 | } |
||
| 285 | |||
| 286 | /** |
||
| 287 | * @return mixed |
||
| 288 | */ |
||
| 289 | public function getResponse() |
||
| 290 | { |
||
| 291 | return json_encode($this->response, JSON_PRETTY_PRINT); |
||
| 292 | } |
||
| 293 | |||
| 294 | /** |
||
| 295 | * @param mixed $response |
||
| 296 | * |
||
| 297 | * @return self |
||
| 298 | */ |
||
| 299 | protected function setResponse($response) |
||
| 300 | { |
||
| 301 | $this->response = $response; |
||
| 302 | |||
| 303 | return $this; |
||
| 304 | } |
||
| 305 | |||
| 306 | /** |
||
| 307 | * @return mixed |
||
| 308 | */ |
||
| 309 | protected function getPayload() |
||
| 310 | { |
||
| 311 | return $this->payload; |
||
| 312 | } |
||
| 313 | |||
| 314 | /** |
||
| 315 | * @param mixed $payload |
||
| 316 | * |
||
| 317 | * @return self |
||
| 318 | */ |
||
| 319 | protected function setPayload($payload) |
||
| 320 | { |
||
| 321 | $this->payload = $payload; |
||
| 322 | |||
| 323 | return $this; |
||
| 324 | } |
||
| 325 | |||
| 326 | /** |
||
| 327 | * @return mixed |
||
| 328 | */ |
||
| 329 | protected function getApiEndpoint() |
||
| 332 | } |
||
| 333 | |||
| 334 | /** |
||
| 335 | * @param mixed $apiEndpoint |
||
| 336 | * |
||
| 337 | * @return self |
||
| 338 | */ |
||
| 339 | protected function setApiEndpoint() |
||
| 340 | { |
||
| 341 | $this->apiEndpoint = config('smsgateway.' . $this->getGateway() . '.apiEndpoint'); |
||
| 342 | return $this; |
||
| 343 | } |
||
| 344 | |||
| 345 | public function asOtpSms() |
||
| 346 | { |
||
| 347 | $payload = $this->getPayload(); |
||
| 348 | if (array_key_exists('smsservicetype', $payload)) { |
||
| 349 | $payload['smsservicetype'] = 'otpmsg'; |
||
| 350 | $this->setPayload($payload); |
||
| 351 | } |
||
| 352 | return $this; |
||
| 353 | } |
||
| 354 | |||
| 355 | public function asUnicodeOtpSms() |
||
| 363 | } |
||
| 364 | |||
| 365 | public function asUnicodeSms() |
||
| 366 | { |
||
| 367 | $payload = $this->getPayload(); |
||
| 368 | if (array_key_exists('smsservicetype', $payload)) { |
||
| 369 | $payload['smsservicetype'] = 'unicodemsg'; |
||
| 370 | $this->setPayload($payload); |
||
| 371 | } |
||
| 372 | return $this; |
||
| 373 | } |
||
| 374 | |||
| 375 | public function asBulkUnicodeSms() |
||
| 378 | } |
||
| 379 | |||
| 380 | public function asBulkSms() |
||
| 381 | { |
||
| 382 | $payload = $this->getPayload(); |
||
| 383 | if (array_key_exists('smsservicetype', $payload)) { |
||
| 384 | $payload['smsservicetype'] = 'bulkmsg'; |
||
| 385 | $this->setPayload($payload); |
||
| 388 | } |
||
| 389 | } |
||
| 390 |