From 2edf89010912e681186b3d43971caff537a68878 Mon Sep 17 00:00:00 2001 From: vegim carkaxhija Date: Thu, 23 Nov 2023 10:35:34 +0100 Subject: [PATCH 1/2] created a Factory to determine which guzzlehttp version is used --- src/Transaction/Client.php | 6 +- .../Request/HttpClient/GuzzleHttpClientV5.php | 70 +++++++++++++++++++ ...lientGuzzle.php => GuzzleHttpClientV7.php} | 28 ++------ .../Request/HttpClient/HttpClientFactory.php | 24 +++++++ 4 files changed, 101 insertions(+), 27 deletions(-) create mode 100644 src/Transaction/Request/HttpClient/GuzzleHttpClientV5.php rename src/Transaction/Request/HttpClient/{HttpClientGuzzle.php => GuzzleHttpClientV7.php} (67%) create mode 100644 src/Transaction/Request/HttpClient/HttpClientFactory.php diff --git a/src/Transaction/Client.php b/src/Transaction/Client.php index b3116fac..a1c4d878 100644 --- a/src/Transaction/Client.php +++ b/src/Transaction/Client.php @@ -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; @@ -43,7 +43,7 @@ class Client private const METHOD_POST = 'POST'; /** - * @var HttpClientInterface|HttpClientGuzzle + * @var HttpClientInterface */ protected HttpClientInterface $httpClient; /** @@ -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()); } /** diff --git a/src/Transaction/Request/HttpClient/GuzzleHttpClientV5.php b/src/Transaction/Request/HttpClient/GuzzleHttpClientV5.php new file mode 100644 index 00000000..acb11dc5 --- /dev/null +++ b/src/Transaction/Request/HttpClient/GuzzleHttpClientV5.php @@ -0,0 +1,70 @@ +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, + ]; + } +} diff --git a/src/Transaction/Request/HttpClient/HttpClientGuzzle.php b/src/Transaction/Request/HttpClient/GuzzleHttpClientV7.php similarity index 67% rename from src/Transaction/Request/HttpClient/HttpClientGuzzle.php rename to src/Transaction/Request/HttpClient/GuzzleHttpClientV7.php index d15a2fd7..015b4e09 100644 --- a/src/Transaction/Request/HttpClient/HttpClientGuzzle.php +++ b/src/Transaction/Request/HttpClient/GuzzleHttpClientV7.php @@ -1,27 +1,8 @@ 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); } diff --git a/src/Transaction/Request/HttpClient/HttpClientFactory.php b/src/Transaction/Request/HttpClient/HttpClientFactory.php new file mode 100644 index 00000000..1fbc0e2e --- /dev/null +++ b/src/Transaction/Request/HttpClient/HttpClientFactory.php @@ -0,0 +1,24 @@ + Date: Thu, 23 Nov 2023 12:47:49 +0100 Subject: [PATCH 2/2] fix --- src/Transaction/Request/HttpClient/HttpClientFactory.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Transaction/Request/HttpClient/HttpClientFactory.php b/src/Transaction/Request/HttpClient/HttpClientFactory.php index 1fbc0e2e..1c7a1220 100644 --- a/src/Transaction/Request/HttpClient/HttpClientFactory.php +++ b/src/Transaction/Request/HttpClient/HttpClientFactory.php @@ -17,8 +17,7 @@ public static function createClient(Subject $logger) // Instantiate the appropriate client based on the major version if ($majorVersion === 5) { return new GuzzleHttpClientV5($logger); - } else { - return new GuzzleHttpClientV7($logger); } + return new GuzzleHttpClientV7($logger); } }