| Conditions | 7 |
| Paths | 36 |
| Total Lines | 63 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 47 | public function send($text = null): bool |
||
| 48 | { |
||
| 49 | if ($text) { |
||
| 50 | $this->setText($text); |
||
| 51 | } |
||
| 52 | |||
| 53 | // get sessionID |
||
| 54 | |||
| 55 | try { |
||
| 56 | $response = $this->client->send($this->request, [ |
||
| 57 | 'query' => [ |
||
| 58 | 'cmd' => 'login', |
||
| 59 | 'owner_email' => config('laravel-sms.smslive247.owner_email'), |
||
| 60 | 'subacct' => config('laravel-sms.smslive247.subacct_username'), |
||
| 61 | 'subacctpwd' => config('laravel-sms.smslive247.subacct_password'), |
||
| 62 | ], |
||
| 63 | ]); |
||
| 64 | |||
| 65 | $response = json_decode($response->getBody()->getContents(), true); |
||
| 66 | |||
| 67 | $split = explode(':', $response); |
||
| 68 | |||
| 69 | if ($split[0] == 'OK' && isset($split[1])) { |
||
| 70 | $sessionId = trim($split[1]); |
||
| 71 | } else { |
||
| 72 | $this->response = last($split); |
||
| 73 | |||
| 74 | return false; |
||
| 75 | } |
||
| 76 | } catch (\Exception $exception) { |
||
| 77 | $this->httpError = $exception; |
||
| 78 | |||
| 79 | return false; |
||
| 80 | } |
||
| 81 | try { |
||
| 82 | $response = $this->client->send($this->request, [ |
||
| 83 | 'query' => [ |
||
| 84 | 'cmd' => 'sendmsg', |
||
| 85 | 'sessionid' => $sessionId, |
||
| 86 | 'sendto' => implode(',', $this->recipients), |
||
| 87 | 'sender' => $this->sender ?? config('laravel-sms.sender'), |
||
| 88 | 'message' => $this->text, |
||
| 89 | 'msgtype' => (int) $this->messageType, |
||
| 90 | ], |
||
| 91 | ]); |
||
| 92 | |||
| 93 | $response = json_decode($response->getBody()->getContents(), true); |
||
| 94 | $split = explode(':', $response); |
||
| 95 | $this->response = last($split); |
||
| 96 | |||
| 97 | return $split[0] == 'OK'; |
||
| 98 | } catch (ClientException $e) { |
||
| 99 | logger()->error('HTTP Exception in '.__CLASS__.': '.__METHOD__.'=>'.$e->getMessage()); |
||
| 100 | $this->httpError = $e; |
||
| 101 | |||
| 102 | return false; |
||
| 103 | } catch (\Exception $e) { |
||
| 104 | logger()->error('SMS Exception in '.__CLASS__.': '.__METHOD__.'=>'.$e->getMessage()); |
||
| 105 | $this->httpError = $e; |
||
| 106 | |||
| 107 | return false; |
||
| 108 | } |
||
| 109 | } |
||
| 110 | } |
||
| 111 |