Conditions | 11 |
Paths | 4 |
Total Lines | 36 |
Code Lines | 23 |
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 |
||
63 | public function send(IUser $user, string $identifier, string $message) { |
||
64 | $client = $this->clientService->newClient(); |
||
65 | // determine type of gateway |
||
66 | $response = $client->get($this->config->getUrl() . '/v1/about'); |
||
67 | if ($response->getStatusCode() === 200) { |
||
68 | // New style gateway https://gitlab.com/morph027/signal-cli-dbus-rest-api |
||
69 | $response = $client->post( |
||
70 | $this->config->getUrl() . '/v1/send/' . $identifier, |
||
71 | [ |
||
72 | 'json' => [ 'message' => $message ], |
||
73 | ] |
||
74 | ); |
||
75 | $body = $response->getBody(); |
||
76 | $json = json_decode($body, true); |
||
|
|||
77 | if ($response->getStatusCode() !== 201 || is_null($json) || !is_array($json) || !isset($json['timestamp'])) { |
||
78 | $status = $response->getStatusCode(); |
||
79 | throw new SmsTransmissionException("error reported by Signal gateway, status=$status, body=$body}"); |
||
80 | } |
||
81 | } else { |
||
82 | // Try old deprecated gateway https://gitlab.com/morph027/signal-web-gateway |
||
83 | $response = $client->post( |
||
84 | $this->config->getUrl() . '/v1/send/' . $identifier, |
||
85 | [ |
||
86 | 'body' => [ |
||
87 | 'to' => $identifier, |
||
88 | 'message' => $message, |
||
89 | ], |
||
90 | 'json' => [ 'message' => $message ], |
||
91 | ] |
||
92 | ); |
||
93 | $body = $response->getBody(); |
||
94 | $json = json_decode($body, true); |
||
95 | |||
96 | if ($response->getStatusCode() !== 200 || is_null($json) || !is_array($json) || !isset($json['success']) || $json['success'] !== true) { |
||
97 | $status = $response->getStatusCode(); |
||
98 | throw new SmsTransmissionException("error reported by Signal gateway, status=$status, body=$body}"); |
||
99 | } |
||
112 |