| Conditions | 6 |
| Paths | 10 |
| Total Lines | 52 |
| Code Lines | 39 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 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 |
||
| 83 | public function send(string $identifier, string $message) { |
||
| 84 | $config = $this->getConfig(); |
||
| 85 | $endpoint = $config->getEndpoint(); |
||
| 86 | $sender = $config->getSender(); |
||
| 87 | $smsAccount = $config->getAccount(); |
||
| 88 | |||
| 89 | $this->attrs['AK'] = $config->getApplicationKey(); |
||
| 90 | $this->attrs['AS'] = $config->getApplicationSecret(); |
||
| 91 | $this->attrs['CK'] = $config->getConsumerKey(); |
||
| 92 | if (!isset($this->endpoints[$endpoint])) |
||
| 93 | throw new InvalidSmsProviderException("Endpoint $endpoint not found"); |
||
| 94 | $this->attrs['endpoint'] = $this->endpoints[$endpoint]; |
||
| 95 | |||
| 96 | $this->getTimeDelta(); |
||
| 97 | |||
| 98 | $header = $this->getHeader('GET',$this->attrs['endpoint'].'/sms'); |
||
| 99 | $response = $this->client->get($this->attrs['endpoint'].'/sms',[ |
||
| 100 | 'headers' => $header, |
||
| 101 | ]); |
||
| 102 | $smsServices = json_decode($response->getBody(),true); |
||
|
|
|||
| 103 | |||
| 104 | $smsAccountFound = false; |
||
| 105 | foreach ($smsServices as $smsService) { |
||
| 106 | if ($smsService === $smsAccount) { |
||
| 107 | $smsAccountFound = true; |
||
| 108 | break; |
||
| 109 | } |
||
| 110 | } |
||
| 111 | if ($smsAccountFound === false) { |
||
| 112 | throw new InvalidSmsProviderException("SMS account $smsAccount not found"); |
||
| 113 | } |
||
| 114 | $content = [ |
||
| 115 | "charset"=> "UTF-8", |
||
| 116 | "message"=> $message, |
||
| 117 | "noStopClause"=> true, |
||
| 118 | "priority"=> "high", |
||
| 119 | "receivers"=> [ $identifier ], |
||
| 120 | "senderForResponse"=> false, |
||
| 121 | "sender"=> $sender, |
||
| 122 | "validityPeriod"=> 3600 |
||
| 123 | ]; |
||
| 124 | $body = json_encode($content); |
||
| 125 | |||
| 126 | $header = $this->getHeader('POST',$this->attrs['endpoint']."/sms/$smsAccount/jobs",$body); |
||
| 127 | $response = $this->client->post($this->attrs['endpoint']."/sms/$smsAccount/jobs",[ |
||
| 128 | 'headers' => $header, |
||
| 129 | 'json' => $content, |
||
| 130 | ]); |
||
| 131 | $resultPostJob = json_decode($response->getBody(),true); |
||
| 132 | |||
| 133 | if (count($resultPostJob["validReceivers"]) === 0) { |
||
| 134 | throw new SmsTransmissionException("Bad receiver $identifier"); |
||
| 135 | } |
||
| 186 |