| Conditions | 11 |
| Paths | 4 |
| Total Lines | 37 |
| Code Lines | 24 |
| 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 |
||
| 46 | #[\Override] |
||
| 47 | public function send(IUser $user, string $identifier, string $message, array $extra = []): void { |
||
| 48 | $client = $this->clientService->newClient(); |
||
| 49 | // determine type of gateway |
||
| 50 | $response = $client->get($this->getUrl() . '/v1/about'); |
||
| 51 | if ($response->getStatusCode() === 200) { |
||
| 52 | // New style gateway https://gitlab.com/morph027/signal-cli-dbus-rest-api |
||
| 53 | $response = $client->post( |
||
| 54 | $this->getUrl() . '/v1/send/' . $identifier, |
||
| 55 | [ |
||
| 56 | 'json' => [ 'message' => $message ], |
||
| 57 | ] |
||
| 58 | ); |
||
| 59 | $body = (string) $response->getBody(); |
||
| 60 | $json = json_decode($body, true); |
||
| 61 | if ($response->getStatusCode() !== 201 || is_null($json) || !is_array($json) || !isset($json['timestamp'])) { |
||
| 62 | $status = $response->getStatusCode(); |
||
| 63 | throw new MessageTransmissionException("error reported by Signal gateway, status=$status, body=$body}"); |
||
| 64 | } |
||
| 65 | } else { |
||
| 66 | // Try old deprecated gateway https://gitlab.com/morph027/signal-web-gateway |
||
| 67 | $response = $client->post( |
||
| 68 | $this->getUrl() . '/v1/send/' . $identifier, |
||
| 69 | [ |
||
| 70 | 'body' => [ |
||
| 71 | 'to' => $identifier, |
||
| 72 | 'message' => $message, |
||
| 73 | ], |
||
| 74 | 'json' => [ 'message' => $message ], |
||
| 75 | ] |
||
| 76 | ); |
||
| 77 | $body = $response->getBody(); |
||
| 78 | $json = json_decode($body, true); |
||
|
|
|||
| 79 | |||
| 80 | if ($response->getStatusCode() !== 200 || is_null($json) || !is_array($json) || !isset($json['success']) || $json['success'] !== true) { |
||
| 81 | $status = $response->getStatusCode(); |
||
| 82 | throw new MessageTransmissionException("error reported by Signal gateway, status=$status, body=$body}"); |
||
| 83 | } |
||
| 98 |