Conditions | 8 |
Paths | 11 |
Total Lines | 68 |
Code Lines | 42 |
Lines | 0 |
Ratio | 0 % |
Changes | 19 | ||
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 |
||
33 | public function auth(Itau $credentials) |
||
34 | { |
||
35 | $endpoint = $credentials->getEnvironment()->getApiUrlAuth(); |
||
36 | $headers = [ |
||
37 | 'Content-Type: application/x-www-form-urlencoded', |
||
38 | 'x-itau-correlationID: 2', |
||
39 | 'x-itau-flowID: 1' |
||
40 | ]; |
||
41 | |||
42 | $request = [ |
||
43 | 'grant_type' => 'client_credentials', |
||
44 | 'client_id' => $credentials->getClientId(), |
||
45 | 'client_secret' => $credentials->getClientSecret() |
||
46 | ]; |
||
47 | |||
48 | $curl = curl_init(); |
||
49 | |||
50 | curl_setopt_array($curl, [ |
||
51 | CURLOPT_URL => $endpoint, |
||
52 | CURLOPT_PORT => 443, |
||
53 | CURLOPT_VERBOSE => 0, |
||
54 | CURLOPT_HTTPHEADER => $headers, |
||
55 | CURLOPT_RETURNTRANSFER => true, |
||
56 | CURLOPT_CUSTOMREQUEST => 'POST', |
||
57 | CURLOPT_POSTFIELDS => http_build_query($request), |
||
58 | CURLOPT_SSLCERT => $credentials->getCertificate(), |
||
59 | CURLOPT_SSLKEY => $credentials->getCertificateKey(), |
||
60 | CURLOPT_CAINFO => $credentials->getCertificate(), |
||
61 | CURLOPT_SSL_VERIFYPEER => 0 |
||
62 | ]); |
||
63 | |||
64 | try { |
||
65 | $response = curl_exec($curl); |
||
66 | } catch (Exception $e) { |
||
67 | throw new ItauException($e->getMessage(), 100); |
||
68 | } |
||
69 | // Verify error |
||
70 | if ($response === false) { |
||
71 | $errorMessage = curl_error($curl); |
||
72 | } |
||
73 | |||
74 | $statusCode = (int) curl_getinfo($curl, CURLINFO_HTTP_CODE); |
||
75 | curl_close($curl); |
||
76 | |||
77 | if ($statusCode >= 400) { |
||
78 | // TODO see what it means code 100 |
||
79 | throw new ItauException($response, 100); |
||
|
|||
80 | } |
||
81 | // Status code 204 don't have content. That means $response will be always false |
||
82 | // Provides a custom content for $response to avoid error in the next if logic |
||
83 | if ($statusCode === 204) { |
||
84 | return [ |
||
85 | 'status_code' => 204 |
||
86 | ]; |
||
87 | } |
||
88 | |||
89 | if (! $response) { |
||
90 | throw new ItauException("Empty response, curl_error: $errorMessage", $statusCode); |
||
91 | } |
||
92 | |||
93 | $responseDecode = json_decode($response, true); |
||
94 | |||
95 | if (is_array($responseDecode) && isset($responseDecode['error'])) { |
||
96 | throw new ItauException($responseDecode['error_description'], 100); |
||
97 | } |
||
98 | $credentials->setAuthorizationToken($responseDecode["access_token"]); |
||
99 | |||
100 | return $credentials; |
||
101 | } |
||
191 |