Skip to content

Commit

Permalink
Merge branch 'develop' into refactor/change-php-docs-imports
Browse files Browse the repository at this point in the history
  • Loading branch information
Matiasnickolas committed Oct 30, 2024
2 parents ed9ceb0 + 0e022ef commit 5a659ba
Show file tree
Hide file tree
Showing 54 changed files with 2,663 additions and 251 deletions.
40 changes: 20 additions & 20 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ name: Build
on:
pull_request:
branches:
- '**'
- "**"
push:
branches:
- master
- develop
tags:
- '*'
- "*"

jobs:
build:
Expand All @@ -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 }}
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ composer.lock
.phpunit.result.cache
/build
.vscode

/coverage
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"ext-curl": "*",
"ext-json": "*",
"ext-mbstring": "*",
"guzzlehttp/guzzle":"^7"
"psr/http-client": "^1.0",
"psr/http-message": "^2.0"
},
"require-dev": {
"phpunit/phpunit": "^9",
Expand Down
32 changes: 16 additions & 16 deletions src/Contracts/HttpClientInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,20 @@

interface HttpClientInterface
{
/**
* @param string $method
* @param string $url
* @param array|null $payload
* @param array|null $options
*
* @throws \GuzzleHttp\Exception\GuzzleException
*
* @return ResponseInterface
*/
public function request(
string $method,
string $url,
array|null $payload = [],
array|null $options = null
): ResponseInterface;
/**
* @param string $method
* @param string $url
* @param array|null $payload
* @param array|null $options
*
* @throws \Transbank\Utils\Curl\Exceptions\CurlRequestException
*
* @return ResponseInterface
*/
public function request(
string $method,
string $url,
array|null $payload = [],
array|null $options = null
): ResponseInterface;
}
2 changes: 1 addition & 1 deletion src/Contracts/RequestService.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use Transbank\Webpay\Options;
use Psr\Http\Message\ResponseInterface;
use Transbank\Webpay\Exceptions\TransbankApiRequest;
use Transbank\Utils\TransbankApiRequest;

interface RequestService
{
Expand Down
12 changes: 6 additions & 6 deletions src/PatpassComercio/Inscription.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public function __construct(
* @param string $city
*
* @throws InscriptionStartException
* @throws \GuzzleHttp\Exception\GuzzleException
* @throws \Transbank\Utils\Curl\Exceptions\CurlRequestException
*
* @return InscriptionStartResponse
*/
Expand Down Expand Up @@ -122,7 +122,7 @@ public function start(
* @param string $token
*
* @throws InscriptionStatusException
* @throws \GuzzleHttp\Exception\GuzzleException
* @throws \Transbank\Utils\Curl\Exceptions\CurlRequestException
*
* @return InscriptionStatusResponse
*/
Expand Down Expand Up @@ -176,23 +176,23 @@ protected function getBaseUrl(): string
}

/**
* @param string $commerceCode
* @param string $apiKey
* @param string $commerceCode
*
* @return self
*/
public static function buildForIntegration(string $commerceCode, string $apiKey): self
public static function buildForIntegration(string $apiKey, string $commerceCode): self
{
return new self(new Options($apiKey, $commerceCode, Options::ENVIRONMENT_INTEGRATION));
}

/**
* @param string $commerceCode
* @param string $apiKey
* @param string $commerceCode
*
* @return self
*/
public static function buildForProduction(string $commerceCode, string $apiKey): self
public static function buildForProduction(string $apiKey, string $commerceCode): self
{
return new self(new Options($apiKey, $commerceCode, Options::ENVIRONMENT_PRODUCTION));
}
Expand Down
95 changes: 95 additions & 0 deletions src/Utils/Curl/Client.php
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;
}
}
24 changes: 24 additions & 0 deletions src/Utils/Curl/Exceptions/CurlRequestException.php
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";
}
}
19 changes: 19 additions & 0 deletions src/Utils/Curl/Exceptions/StreamException.php
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);
}
}
44 changes: 44 additions & 0 deletions src/Utils/Curl/HttpCurlClient.php
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);
}
}
Loading

0 comments on commit 5a659ba

Please sign in to comment.