leandroferreirama /
api-itau
| 1 | <?php |
||||
| 2 | namespace Itau\API; |
||||
| 3 | |||||
| 4 | use Exception; |
||||
| 5 | use Itau\API\Exception\ItauException; |
||||
| 6 | |||||
| 7 | /** |
||||
| 8 | * Class Request |
||||
| 9 | * |
||||
| 10 | * @package Itau\API |
||||
| 11 | */ |
||||
| 12 | class Request |
||||
| 13 | { |
||||
| 14 | |||||
| 15 | public const CURL_TYPE_POST = "POST"; |
||||
| 16 | public const CURL_TYPE_PUT = "PUT"; |
||||
| 17 | public const CURL_TYPE_GET = "GET"; |
||||
| 18 | public const CURL_TYPE_DELETE = "DELETE"; |
||||
| 19 | |||||
| 20 | /** |
||||
| 21 | * Request constructor. |
||||
| 22 | * |
||||
| 23 | * @param Itau $credentials |
||||
| 24 | * TODO create local variable to $credentials |
||||
| 25 | */ |
||||
| 26 | public function __construct(Itau $credentials) |
||||
| 27 | { |
||||
| 28 | if (! $credentials->getAuthorizationToken()) { |
||||
| 29 | $this->auth($credentials); |
||||
| 30 | } |
||||
| 31 | } |
||||
| 32 | |||||
| 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); |
||||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||
| 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); |
||||
|
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||||
| 91 | } |
||||
| 92 | |||||
| 93 | $responseDecode = json_decode($response, true); |
||||
|
0 ignored issues
–
show
It seems like
$response can also be of type true; however, parameter $json of json_decode() does only seem to accept string, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 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 | } |
||||
| 102 | |||||
| 103 | public function get(Itau $credentials, $fullUrl, $params = null) |
||||
| 104 | { |
||||
| 105 | return $this->send($credentials, $fullUrl, self::CURL_TYPE_GET, $params); |
||||
| 106 | } |
||||
| 107 | |||||
| 108 | public function post(Itau $credentials, $fullUrl, $params) |
||||
| 109 | { |
||||
| 110 | return $this->send($credentials, $fullUrl, self::CURL_TYPE_POST, $params); |
||||
| 111 | } |
||||
| 112 | |||||
| 113 | public function patch(Itau $credentials, $fullUrl, $params = null) |
||||
| 114 | { |
||||
| 115 | return $this->send($credentials, $fullUrl, 'PATCH', $params); |
||||
| 116 | } |
||||
| 117 | |||||
| 118 | private function send(Itau $credentials, $fullUrl, $method, $jsonBody = null) |
||||
| 119 | { |
||||
| 120 | $curl = curl_init($fullUrl); |
||||
| 121 | |||||
| 122 | $defaultCurlOptions = array( |
||||
| 123 | CURLOPT_CONNECTTIMEOUT => 60, |
||||
| 124 | CURLOPT_RETURNTRANSFER => true, |
||||
| 125 | CURLOPT_TIMEOUT => 60, |
||||
| 126 | CURLOPT_VERBOSE => 0, |
||||
| 127 | CURLOPT_HTTPHEADER => array( |
||||
| 128 | 'Content-Type: application/json; charset=utf-8' |
||||
| 129 | ), |
||||
| 130 | CURLOPT_SSLCERT => $credentials->getCertificate(), |
||||
| 131 | CURLOPT_SSLKEY => $credentials->getCertificateKey(), |
||||
| 132 | CURLOPT_CAINFO => $credentials->getCertificate(), |
||||
| 133 | CURLOPT_SSL_VERIFYHOST => 2, |
||||
| 134 | CURLOPT_SSL_VERIFYPEER => 0 |
||||
| 135 | ); |
||||
| 136 | |||||
| 137 | $defaultCurlOptions[CURLOPT_HTTPHEADER][] = 'Authorization: Bearer ' . $credentials->getAuthorizationToken(); |
||||
| 138 | $defaultCurlOptions[CURLOPT_HTTPHEADER][] = 'x-itau-apikey: ' . $credentials->getClientId(); |
||||
| 139 | $defaultCurlOptions[CURLOPT_HTTPHEADER][] = 'x-itau-correlationID: 2'; |
||||
| 140 | |||||
| 141 | // Add custom method |
||||
| 142 | if (in_array($method, [ |
||||
| 143 | self::CURL_TYPE_DELETE, |
||||
| 144 | self::CURL_TYPE_PUT, |
||||
| 145 | self::CURL_TYPE_GET, |
||||
| 146 | 'PATCH' |
||||
| 147 | ])) { |
||||
| 148 | curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method); |
||||
| 149 | } |
||||
| 150 | |||||
| 151 | // Add body params |
||||
| 152 | if (! empty($jsonBody)) { |
||||
| 153 | curl_setopt($curl, CURLOPT_POST, 1); |
||||
| 154 | curl_setopt($curl, CURLOPT_POSTFIELDS, is_string($jsonBody) ? $jsonBody : json_encode($jsonBody)); |
||||
| 155 | } |
||||
| 156 | |||||
| 157 | curl_setopt_array($curl, $defaultCurlOptions); |
||||
| 158 | |||||
| 159 | $response = null; |
||||
|
0 ignored issues
–
show
|
|||||
| 160 | $errorMessage = ''; |
||||
|
0 ignored issues
–
show
|
|||||
| 161 | |||||
| 162 | try { |
||||
| 163 | $response = curl_exec($curl); |
||||
| 164 | } catch (Exception $e) { |
||||
| 165 | throw new ItauException("Request Exception, error: {$e->getMessage()}", 100); |
||||
| 166 | } |
||||
| 167 | |||||
| 168 | // Verify error |
||||
| 169 | if ($response === false) { |
||||
| 170 | $errorMessage = curl_error($curl); |
||||
| 171 | } |
||||
| 172 | |||||
| 173 | $statusCode = (int) curl_getinfo($curl, CURLINFO_HTTP_CODE); |
||||
| 174 | curl_close($curl); |
||||
| 175 | |||||
| 176 | if ($statusCode >= 400) { |
||||
| 177 | // TODO see what it means code 100 |
||||
| 178 | throw new ItauException($response, 100); |
||||
|
0 ignored issues
–
show
It seems like
$response can also be of type true; however, parameter $message of Itau\API\Exception\ItauException::__construct() does only seem to accept string, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 179 | } |
||||
| 180 | |||||
| 181 | $responseDecode = json_decode($response, true); |
||||
|
0 ignored issues
–
show
It seems like
$response can also be of type true; however, parameter $json of json_decode() does only seem to accept string, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 182 | if(is_null($responseDecode)){ |
||||
| 183 | $responseDecode = ['status_code' => $statusCode]; |
||||
| 184 | } else { |
||||
| 185 | array_push($responseDecode, ['status_code' => $statusCode]); |
||||
| 186 | } |
||||
| 187 | |||||
| 188 | return $responseDecode; |
||||
| 189 | } |
||||
| 190 | } |
||||
| 191 |