diff --git a/spec/Api/GenericApiSpec.php b/spec/Api/GenericApiSpec.php index e4002914..4ae74e5c 100644 --- a/spec/Api/GenericApiSpec.php +++ b/spec/Api/GenericApiSpec.php @@ -13,17 +13,19 @@ namespace spec\Sylius\PayPalPlugin\Api; -use GuzzleHttp\ClientInterface; use PhpSpec\ObjectBehavior; +use Psr\Http\Client\ClientInterface; +use Psr\Http\Message\RequestFactoryInterface; +use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\StreamInterface; use Sylius\PayPalPlugin\Api\GenericApiInterface; final class GenericApiSpec extends ObjectBehavior { - function let(ClientInterface $client): void + function let(ClientInterface $client, RequestFactoryInterface $requestFactory): void { - $this->beConstructedWith($client); + $this->beConstructedWith($client, $requestFactory); } function it_implements_generic_api_interface(): void @@ -33,17 +35,19 @@ function it_implements_generic_api_interface(): void function it_calls_api_by_url( ClientInterface $client, + RequestFactoryInterface $requestFactory, + RequestInterface $request, ResponseInterface $response, StreamInterface $body ): void { - $client->request('GET', 'http://url.com/', [ - 'headers' => [ - 'Authorization' => 'Bearer TOKEN', - 'Content-Type' => 'application/json', - 'Accept' => 'application/json', - ], - ])->willReturn($response); + $requestFactory->createRequest('GET', 'http://url.com/')->willReturn($request); + + $request->withHeader('Authorization', 'Bearer TOKEN')->willReturn($request); + $request->withHeader('Content-Type', 'application/json')->willReturn($request); + $request->withHeader('Accept', 'application/json')->willReturn($request); + + $client->sendRequest($request)->willReturn($response); $response->getBody()->willReturn($body); $body->getContents()->willReturn('{ "parameter": "VALUE" }'); diff --git a/src/Api/GenericApi.php b/src/Api/GenericApi.php index d8fc5788..a9a78e20 100644 --- a/src/Api/GenericApi.php +++ b/src/Api/GenericApi.php @@ -4,27 +4,26 @@ namespace Sylius\PayPalPlugin\Api; -use GuzzleHttp\ClientInterface; + +use Psr\Http\Client\ClientInterface; +use Psr\Http\Message\RequestFactoryInterface; final class GenericApi implements GenericApiInterface { - private ClientInterface $client; - public function __construct(ClientInterface $client) - { - $this->client = $client; + public function __construct( + private ClientInterface $client, + private RequestFactoryInterface $requestFactory + ){ } public function get(string $token, string $url): array { - $response = $this->client->request('GET', $url, [ - 'headers' => [ - 'Authorization' => 'Bearer ' . $token, - 'Content-Type' => 'application/json', - 'Accept' => 'application/json', - ], - ]); - - return (array) json_decode($response->getBody()->getContents(), true); + $request = $this->requestFactory->createRequest('GET', $url) + ->withHeader('Authorization', 'Bearer ' . $token) + ->withHeader('Content-Type', 'application/json') + ->withHeader('Accept', 'application/json'); + + return (array) json_decode($this->client->sendRequest($request)->getBody()->getContents(), true); } } diff --git a/src/Client/PayPalClient.php b/src/Client/PayPalClient.php index 61a06fe7..e196b1bf 100644 --- a/src/Client/PayPalClient.php +++ b/src/Client/PayPalClient.php @@ -20,7 +20,6 @@ use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\StreamFactoryInterface; use Psr\Log\LoggerInterface; -use Symfony\Component\HttpClient\Psr18Client; use Sylius\Component\Channel\Context\ChannelContextInterface; use Sylius\Component\Core\Model\ChannelInterface; use Sylius\PayPalPlugin\Exception\PayPalApiTimeoutException; diff --git a/src/Resources/config/services/api.xml b/src/Resources/config/services/api.xml index 5a262ec6..f4517932 100644 --- a/src/Resources/config/services/api.xml +++ b/src/Resources/config/services/api.xml @@ -59,7 +59,8 @@ id="Sylius\PayPalPlugin\Api\GenericApiInterface" class="Sylius\PayPalPlugin\Api\GenericApi" > - + +