Conditions | 9 |
Paths | 6 |
Total Lines | 54 |
Code Lines | 34 |
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 |
||
29 | private function requireAccessToken(string $accountId, string $clientId, string $clientSecret) |
||
30 | { |
||
31 | $options = [ |
||
32 | CURLOPT_CUSTOMREQUEST => 'POST', |
||
33 | CURLOPT_ENCODING => '', |
||
34 | CURLOPT_HTTPHEADER => [ |
||
35 | 'Authorization: Basic '.base64_encode("$clientId:$clientSecret"), |
||
36 | 'Content-Type: application/x-www-form-urlencoded', |
||
37 | 'Host: zoom.us', |
||
38 | ], |
||
39 | CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, |
||
40 | CURLOPT_MAXREDIRS => 10, |
||
41 | CURLOPT_RETURNTRANSFER => true, |
||
42 | CURLOPT_TIMEOUT => 30, |
||
43 | CURLOPT_POST => true, |
||
44 | CURLOPT_POSTFIELDS => http_build_query([ |
||
45 | 'grant_type' => 'account_credentials', |
||
46 | 'account_id' => $accountId, |
||
47 | ]), |
||
48 | ]; |
||
49 | |||
50 | $url = 'https://zoom.us/oauth/token'; |
||
51 | |||
52 | $curl = curl_init($url); |
||
53 | |||
54 | if (false === $curl) { |
||
55 | throw new Exception("curl_init returned false"); |
||
56 | } |
||
57 | |||
58 | curl_setopt_array($curl, $options); |
||
59 | $responseBody = curl_exec($curl); |
||
60 | $responseCode = curl_getinfo($curl, CURLINFO_RESPONSE_CODE); |
||
61 | $curlError = curl_error($curl); |
||
62 | curl_close($curl); |
||
63 | |||
64 | if ($curlError) { |
||
65 | throw new Exception("cURL Error: $curlError"); |
||
66 | } |
||
67 | |||
68 | if (false === $responseBody || !is_string($responseBody)) { |
||
69 | throw new Exception('cURL Error'); |
||
70 | } |
||
71 | |||
72 | if (empty($responseCode) || $responseCode < 200 || $responseCode >= 300) { |
||
73 | throw new Exception($responseBody, $responseCode); |
||
74 | } |
||
75 | |||
76 | $jsonResponseBody = json_decode($responseBody, true); |
||
77 | |||
78 | if (false === $jsonResponseBody) { |
||
79 | throw new Exception('Could not generate JSON responso body'); |
||
80 | } |
||
81 | |||
82 | return $jsonResponseBody['access_token']; |
||
83 | } |
||
85 |