Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BP-3290 Fix support for guzzlehttp v5 #159

Merged
merged 2 commits into from
Dec 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/Transaction/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
use Buckaroo\Services\TransactionHeaders\DefaultHeader;
use Buckaroo\Services\TransactionHeaders\HmacHeader;
use Buckaroo\Services\TransactionHeaders\SoftwareHeader;
use Buckaroo\Transaction\Request\HttpClient\HttpClientGuzzle;
use Buckaroo\Transaction\Request\HttpClient\HttpClientFactory;
use Buckaroo\Transaction\Request\HttpClient\HttpClientInterface;
use Buckaroo\Transaction\Request\Request;
use Buckaroo\Transaction\Response\Response;
Expand All @@ -43,7 +43,7 @@ class Client
private const METHOD_POST = 'POST';

/**
* @var HttpClientInterface|HttpClientGuzzle
* @var HttpClientInterface
*/
protected HttpClientInterface $httpClient;
/**
Expand All @@ -61,7 +61,7 @@ class Client
public function __construct(?Config $config)
{
$this->config = $config;
$this->httpClient = new HttpClientGuzzle($config->getLogger());
$this->httpClient = HttpClientFactory::createClient($config->getLogger());
}

/**
Expand Down
70 changes: 70 additions & 0 deletions src/Transaction/Request/HttpClient/GuzzleHttpClientV5.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

namespace Buckaroo\Transaction\Request\HttpClient;

use Buckaroo\Exceptions\BuckarooException;
use Buckaroo\Exceptions\TransferException;
use Buckaroo\Handlers\Logging\Subject;
use GuzzleHttp\Client;
use GuzzleHttp\ClientInterface;
use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\Exception\RequestException;

class GuzzleHttpClientV5 extends HttpClientAbstract
{
/**
* @var Subject
*/
protected Subject $logger;
protected ClientInterface $httpClient;

public function __construct(Subject $logger)
{
parent::__construct($logger);
$this->logger = $logger;

$this->httpClient = new Client([
'timeout' => self::TIMEOUT,
'connect_timeout' => self::CONNECT_TIMEOUT,
]);
}

/**
* @param string $url
* @param array $headers
* @param string $method
* @param string|null $data
* @return array|mixed
* @throws TransferException
* @throws BuckarooException|GuzzleException
*/

public function call(string $url, array $headers, string $method, string $data = null)
{
$headers = $this->convertHeadersFormat($headers);

$request = $this->httpClient->createRequest($method, $url, [
'headers' => $headers,
'body' => $data,
]);

try
{
$response = $this->httpClient->send($request);

$result = (string) $response->getBody();

$this->logger->info('RESPONSE HEADERS: ' . json_encode($response->getHeaders()));
$this->logger->info('RESPONSE BODY: ' . $response->getBody());
} catch (RequestException $e) {
throw new TransferException($this->logger, "Transfer failed", 0, $e);
}

$result = $this->getDecodedResult($response, $result);

return [
$response,
$result,
];
}
}
Original file line number Diff line number Diff line change
@@ -1,27 +1,8 @@
<?php
/*
* NOTICE OF LICENSE
*
* This source file is subject to the MIT License
* It is available through the world-wide-web at this URL:
* https://tldrlegal.com/license/mit-license
* If you are unable to obtain it through the world-wide-web, please send an email
* to support@buckaroo.nl so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade this module to newer
* versions in the future. If you wish to customize this module for your
* needs please contact support@buckaroo.nl for more information.
*
* @copyright Copyright (c) Buckaroo B.V.
* @license https://tldrlegal.com/license/mit-license
*/

declare(strict_types=1);

namespace Buckaroo\Transaction\Request\HttpClient;

use Buckaroo\Exceptions\BuckarooException;
use Buckaroo\Exceptions\TransferException;
use Buckaroo\Handlers\Logging\Subject;
use GuzzleHttp\Client;
Expand All @@ -30,7 +11,7 @@
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\RequestOptions;

class HttpClientGuzzle extends HttpClientAbstract
class GuzzleHttpClientV7 extends HttpClientAbstract
{
/**
* @var Subject
Expand Down Expand Up @@ -61,7 +42,7 @@ public function __construct(Subject $logger)
* @param string|null $data
* @return array|mixed
* @throws TransferException
* @throws \Buckaroo\Exceptions\BuckarooException
* @throws BuckarooException
*/
public function call(string $url, array $headers, string $method, string $data = null)
{
Expand All @@ -77,8 +58,7 @@ public function call(string $url, array $headers, string $method, string $data =

$this->logger->info('RESPONSE HEADERS: ' . json_encode($response->getHeaders()));
$this->logger->info('RESPONSE BODY: ' . $response->getBody());
} catch (GuzzleException $e)
{
} catch (GuzzleException $e) {
throw new TransferException($this->logger, "Transfer failed", 0, $e);
}

Expand Down
23 changes: 23 additions & 0 deletions src/Transaction/Request/HttpClient/HttpClientFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Buckaroo\Transaction\Request\HttpClient;

use Buckaroo\Handlers\Logging\Subject;
use Composer\InstalledVersions;

class HttpClientFactory
{
public static function createClient(Subject $logger)
{
// Detect the installed GuzzleHttp version
$versionString = InstalledVersions::getVersion('guzzlehttp/guzzle');
// Extract the major version number
$majorVersion = (int) explode('.', $versionString)[0];

// Instantiate the appropriate client based on the major version
if ($majorVersion === 5) {
return new GuzzleHttpClientV5($logger);
}
return new GuzzleHttpClientV7($logger);
}
}