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