Conditions | 7 |
Paths | 78 |
Total Lines | 52 |
Code Lines | 41 |
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 |
||
94 | public function sendMultipleSms($smsText, array $destinationNumber = [], $smsEncoding = 'AUTO', $throws = true) |
||
95 | { |
||
96 | try { |
||
97 | if ($smsEncoding === '') { |
||
98 | $smsEncoding = 'AUTO'; |
||
99 | } |
||
100 | if (empty($destinationNumber)) { |
||
101 | throw new HttpException('Destination must not empty.'); |
||
102 | } |
||
103 | $url = Config::getBaseUrl() . '/sms/v1/' . Config::$subAccountId . '/single'; |
||
104 | $guzzleClient = new Client(); |
||
105 | $body = array('clientBatchId' => Helper::random(50), |
||
106 | 'messages' => array(), |
||
107 | 'template' => array('source' => Config::$smsFrom, |
||
108 | 'text' => $smsText, 'encoding' => $smsEncoding, |
||
109 | 'expiry' => Helper::generateExpired(2880) |
||
110 | ), |
||
111 | 'includeMessagesInResponse' => true |
||
112 | ); |
||
113 | foreach ($destinationNumber as $key => $value) { |
||
114 | $clientData = array(); |
||
115 | $clientData['clientMessageId'] = Helper::random(50); |
||
116 | $clientData['source'] = \Wavecell\Config::$smsFrom; |
||
117 | $clientData['destination'] = $value; |
||
118 | $clientData['text'] = $smsText; |
||
119 | $clientData['encoding'] = $smsEncoding; |
||
120 | $clientData['expiry'] = Helper::generateExpired(Config::$smsExpireInMinutes); |
||
121 | uksort($clientData, 'strcmp'); |
||
122 | array_push($body['messages'], $clientData); |
||
123 | } |
||
124 | uksort($body, 'strcmp'); |
||
125 | $curls = array_merge(Config::$curlOptions, $this->curlOpts); |
||
126 | $this->response = $guzzleClient->post($url, [ |
||
127 | 'headers' => [ |
||
128 | 'Authorization' => 'Bearer ' . Config::$secretKey, |
||
129 | 'Content-Type' => 'application/json', |
||
130 | 'curl' => $curls |
||
131 | ], |
||
132 | 'json' => $body |
||
133 | ]); |
||
134 | } catch (RequestException $exception) { |
||
135 | $this->response = $exception->getResponse(); |
||
136 | if ($throws) { |
||
137 | $body = (string)$this->response->getBody(); |
||
138 | $code = (int)$this->response->getStatusCode(); |
||
139 | $content = json_decode($body); |
||
140 | throw new HttpException(isset($content->message) ? $content->message : 'Request not processed.', $code); |
||
141 | } else { |
||
142 | return $this->response; |
||
143 | } |
||
144 | } |
||
145 | return (string)$this->response->getBody(); |
||
146 | } |
||
221 |