-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'develop' into refactor/change-php-docs-imports
- Loading branch information
Showing
54 changed files
with
2,663 additions
and
251 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,12 +3,13 @@ name: Build | |
on: | ||
pull_request: | ||
branches: | ||
- '**' | ||
- "**" | ||
push: | ||
branches: | ||
- master | ||
- develop | ||
tags: | ||
- '*' | ||
- "*" | ||
|
||
jobs: | ||
build: | ||
|
@@ -17,21 +18,20 @@ jobs: | |
matrix: | ||
php-versions: [8.2] | ||
steps: | ||
- name: Checkout repository | ||
uses: actions/[email protected] | ||
- name: Set up PHP | ||
uses: shivammathur/setup-php@v2 | ||
with: | ||
php-version: 8.2 | ||
extensions: mbstring, json, curl, pcov | ||
coverage: pcov | ||
- name: Install dependencies | ||
run: composer install --no-interaction --no-progress --prefer-dist | ||
- name: Run PHPUnit tests | ||
run: vendor/bin/phpunit --coverage-clover build/logs/clover.xml | ||
- name: SonarCloud Scan | ||
uses: sonarsource/sonarcloud-github-action@master | ||
env: | ||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | ||
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} | ||
|
||
- name: Checkout repository | ||
uses: actions/[email protected] | ||
- name: Set up PHP | ||
uses: shivammathur/setup-php@v2 | ||
with: | ||
php-version: 8.2 | ||
extensions: mbstring, json, curl, pcov | ||
coverage: pcov | ||
- name: Install dependencies | ||
run: composer install --no-interaction --no-progress --prefer-dist | ||
- name: Run PHPUnit tests | ||
run: vendor/bin/phpunit --coverage-clover build/logs/clover.xml | ||
- name: SonarCloud Scan | ||
uses: sonarsource/sonarcloud-github-action@master | ||
env: | ||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | ||
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,3 +6,5 @@ composer.lock | |
.phpunit.result.cache | ||
/build | ||
.vscode | ||
|
||
/coverage |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
<?php | ||
|
||
namespace Transbank\Utils\Curl; | ||
|
||
use Psr\Http\Client\ClientInterface; | ||
use Psr\Http\Message\ResponseInterface; | ||
use Psr\Http\Message\RequestInterface; | ||
use Transbank\Utils\Curl\Exceptions\CurlRequestException; | ||
|
||
|
||
class Client implements ClientInterface | ||
{ | ||
private int $timeout; | ||
public function __construct(int $timeout) | ||
{ | ||
$this->timeout = $timeout; | ||
} | ||
public function sendRequest(RequestInterface $request): ResponseInterface | ||
{ | ||
$curl = curl_init(); | ||
|
||
if (!$curl) { | ||
throw new CurlRequestException('Unable to initialize cURL session.'); | ||
} | ||
|
||
curl_setopt_array($curl, [ | ||
CURLOPT_URL => (string) $request->getUri(), | ||
CURLOPT_CUSTOMREQUEST => $request->getMethod(), | ||
CURLOPT_RETURNTRANSFER => true, | ||
CURLOPT_HEADER => true, | ||
CURLOPT_FOLLOWLOCATION => true, | ||
CURLOPT_HTTP_VERSION => $this->getCurlHttpVersion($request->getProtocolVersion()), | ||
CURLOPT_TIMEOUT => $this->timeout, | ||
CURLOPT_CONNECTTIMEOUT => $this->timeout, | ||
]); | ||
|
||
$headers = []; | ||
foreach ($request->getHeaders() as $name => $value) { | ||
$headers[] = "$name: $value"; | ||
} | ||
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); | ||
|
||
$body = (string) $request->getBody(); | ||
if (!empty($body)) { | ||
curl_setopt($curl, CURLOPT_POSTFIELDS, $body); | ||
} | ||
|
||
$response = curl_exec($curl); | ||
if ($response === false) { | ||
if (is_resource($curl)) { | ||
curl_close($curl); | ||
} | ||
throw new CurlRequestException(curl_error($curl), curl_errno($curl)); | ||
} | ||
|
||
$headerSize = curl_getinfo($curl, CURLINFO_HEADER_SIZE); | ||
$statusCode = curl_getinfo($curl, CURLINFO_HTTP_CODE); | ||
$rawHeaders = substr($response, 0, $headerSize); | ||
$body = substr($response, $headerSize); | ||
|
||
curl_close($curl); | ||
|
||
$headers = $this->parseHeaders($rawHeaders); | ||
|
||
return new Response($statusCode, $headers, $body); | ||
} | ||
|
||
private function parseHeaders(string $rawHeaders): array | ||
{ | ||
$headers = []; | ||
$lines = explode("\r\n", $rawHeaders); | ||
|
||
foreach ($lines as $line) { | ||
if (strpos($line, ':') !== false) { | ||
list($name, $value) = explode(': ', $line, 2); | ||
$headers[$name][] = $value; | ||
} | ||
} | ||
|
||
return $headers; | ||
} | ||
|
||
private function getCurlHttpVersion(string $protocol): int | ||
{ | ||
$protocol = trim($protocol); | ||
$curlHttpVersion = [ | ||
'1.0' => CURL_HTTP_VERSION_1_0, | ||
'1.1' => CURL_HTTP_VERSION_1_1, | ||
'2.0' => CURL_HTTP_VERSION_2_0, | ||
'3.0' => defined('CURL_HTTP_VERSION_3') ? CURL_HTTP_VERSION_3 : CURL_HTTP_VERSION_NONE | ||
]; | ||
|
||
return $curlHttpVersion[$protocol] ?? CURL_HTTP_VERSION_NONE; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
namespace Transbank\Utils\Curl\Exceptions; | ||
|
||
class CurlRequestException extends \Exception | ||
{ | ||
const DEFAULT_MESSAGE = 'An error happened on the request'; | ||
|
||
/** | ||
* RequestException constructor. | ||
* | ||
* @param string $message | ||
* @param \Throwable|null $previous | ||
*/ | ||
public function __construct(string $message = self::DEFAULT_MESSAGE, int $errorCode = 0, \Throwable|null $previous = null) | ||
{ | ||
parent::__construct($message, $errorCode, $previous); | ||
} | ||
|
||
public function __toString(): string | ||
{ | ||
return __CLASS__ . ": [error code {$this->code}]: {$this->message} in {$this->file} on line {$this->line}\n"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
namespace Transbank\Utils\Curl\Exceptions; | ||
|
||
class StreamException extends \Exception | ||
{ | ||
const DEFAULT_MESSAGE = 'An error happened on the stream'; | ||
|
||
/** | ||
* StreamException constructor. | ||
* | ||
* @param string $message | ||
* @param \Throwable|null $previous | ||
*/ | ||
public function __construct(string $message = self::DEFAULT_MESSAGE, \Throwable|null $previous = null) | ||
{ | ||
parent::__construct($message, 0, $previous); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
|
||
namespace Transbank\Utils\Curl; | ||
|
||
use Composer\InstalledVersions; | ||
use Transbank\Contracts\HttpClientInterface; | ||
use Psr\Http\Message\ResponseInterface; | ||
|
||
|
||
class HttpCurlClient implements HttpClientInterface | ||
{ | ||
public function request( | ||
string $method, | ||
string $url, | ||
array|null $payload = [], | ||
array|null $options = null | ||
): ResponseInterface { | ||
$installedVersion = 'unknown'; | ||
|
||
if (class_exists('\Composer\InstalledVersions') && InstalledVersions::isInstalled('transbank/transbank-sdk')) { | ||
$installedVersion = InstalledVersions::getVersion('transbank/transbank-sdk') ?? 'unknown'; | ||
} | ||
|
||
$baseHeaders = [ | ||
'Content-Type' => 'application/json', | ||
'User-Agent' => 'SDK-PHP/' . $installedVersion, | ||
]; | ||
|
||
$givenHeaders = $options['headers'] ?? []; | ||
$headers = array_merge($baseHeaders, $givenHeaders); | ||
if (!$payload) { | ||
$payload = null; | ||
} | ||
if (is_array($payload)) { | ||
$payload = json_encode($payload); | ||
} | ||
|
||
$requestTimeout = $options['timeout'] ?? 0; | ||
|
||
$request = new Request($method, $url, $headers, $payload); | ||
$client = new Client($requestTimeout); | ||
return $client->sendRequest($request); | ||
} | ||
} |
Oops, something went wrong.