| Conditions | 11 |
| Paths | 21 |
| Total Lines | 52 |
| Code Lines | 35 |
| 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 |
||
| 41 | public function send($httpMethod, $relativePath, $parameters = [], $requestBody = null) |
||
| 42 | { |
||
| 43 | $options = [ |
||
| 44 | CURLOPT_CUSTOMREQUEST => $httpMethod, |
||
| 45 | CURLOPT_ENCODING => '', |
||
| 46 | CURLOPT_HTTPHEADER => [ |
||
| 47 | 'authorization: Bearer '.$this->token, |
||
| 48 | 'content-type: application/json', |
||
| 49 | ], |
||
| 50 | CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, |
||
| 51 | CURLOPT_MAXREDIRS => 10, |
||
| 52 | CURLOPT_RETURNTRANSFER => true, |
||
| 53 | CURLOPT_TIMEOUT => 30, |
||
| 54 | ]; |
||
| 55 | if (!is_null($requestBody)) { |
||
| 56 | $jsonRequestBody = json_encode($requestBody); |
||
| 57 | if (false === $jsonRequestBody) { |
||
| 58 | throw new Exception('Could not generate JSON request body'); |
||
| 59 | } |
||
| 60 | $options[CURLOPT_POSTFIELDS] = $jsonRequestBody; |
||
| 61 | } |
||
| 62 | |||
| 63 | $url = "https://api.zoom.us/v2/$relativePath"; |
||
| 64 | if (!empty($parameters)) { |
||
| 65 | $url .= '?'.http_build_query($parameters); |
||
| 66 | } |
||
| 67 | $curl = curl_init($url); |
||
| 68 | if (false === $curl) { |
||
| 69 | throw new Exception("curl_init returned false"); |
||
| 70 | } |
||
| 71 | curl_setopt_array($curl, $options); |
||
| 72 | $responseBody = curl_exec($curl); |
||
| 73 | $responseCode = curl_getinfo($curl, CURLINFO_RESPONSE_CODE); |
||
| 74 | $curlError = curl_error($curl); |
||
| 75 | curl_close($curl); |
||
| 76 | |||
| 77 | if ($curlError) { |
||
| 78 | throw new Exception("cURL Error: $curlError"); |
||
| 79 | } |
||
| 80 | |||
| 81 | if (false === $responseBody || !is_string($responseBody)) { |
||
| 82 | throw new Exception('cURL Error'); |
||
| 83 | } |
||
| 84 | |||
| 85 | if (empty($responseCode) |
||
| 86 | || $responseCode < 200 |
||
| 87 | || $responseCode >= 300 |
||
| 88 | ) { |
||
| 89 | throw new Exception($responseBody, $responseCode); |
||
| 90 | } |
||
| 91 | |||
| 92 | return $responseBody; |
||
| 93 | } |
||
| 95 |