Skip to content

Commit

Permalink
Fix BC changes
Browse files Browse the repository at this point in the history
  • Loading branch information
mpysiak committed Mar 27, 2024
1 parent d03ea1d commit 47f7eaa
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 16 deletions.
2 changes: 1 addition & 1 deletion spec/Enabler/PayPalPaymentMethodEnablerSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ function let(
SellerWebhookRegistrarInterface $sellerWebhookRegistrar
): void {
$this->beConstructedWith(
$client, $requestFactory, 'http://base-url.com', $paymentMethodManager, $sellerWebhookRegistrar
$client, 'http://base-url.com', $paymentMethodManager, $sellerWebhookRegistrar, $requestFactory
);
}

Expand Down
42 changes: 37 additions & 5 deletions src/Api/GenericApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,58 @@

namespace Sylius\PayPalPlugin\Api;


use GuzzleHttp\ClientInterface as GuzzleClientInterface;
use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\RequestFactoryInterface;

final class GenericApi implements GenericApiInterface
{

public function __construct(
private readonly ClientInterface $client,
private readonly RequestFactoryInterface $requestFactory
){
private readonly GuzzleClientInterface|ClientInterface $client,
private readonly ?RequestFactoryInterface $requestFactory = null,
) {
if ($this->client instanceof GuzzleClientInterface) {
trigger_deprecation(
'sylius/paypal-plugin',
'1.6',
'Passing GuzzleHttp\ClientInterface as a first argument in the constructor is deprecated and will be removed. Use Psr\Http\Client\ClientInterface instead.',
);
}

if (null === $this->requestFactory) {
trigger_deprecation(
'sylius/paypal-plugin',
'1.6',
'Not passing $requestFactory to %s constructor is deprecated and will be removed',
self::class,
);
}
}

public function get(string $token, string $url): array
{
if ($this->client instanceof GuzzleClientInterface && null === $this->requestFactory) {
return $this->legacyGet($token, $url);
}

$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);
}

private function legacyGet(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);
}
}
42 changes: 33 additions & 9 deletions src/Enabler/PayPalPaymentMethodEnabler.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace Sylius\PayPalPlugin\Enabler;

use Doctrine\Persistence\ObjectManager;
use GuzzleHttp\Client;
use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\RequestFactoryInterface;
use Sylius\Bundle\PayumBundle\Model\GatewayConfigInterface;
Expand All @@ -24,12 +25,28 @@
final class PayPalPaymentMethodEnabler implements PaymentMethodEnablerInterface
{
public function __construct(
private readonly ClientInterface $client,
private readonly RequestFactoryInterface $requestFactory,
private readonly Client|ClientInterface $client,
private readonly string $baseUrl,
private readonly ObjectManager $paymentMethodManager,
private readonly SellerWebhookRegistrarInterface $sellerWebhookRegistrar
private readonly SellerWebhookRegistrarInterface $sellerWebhookRegistrar,
private readonly ?RequestFactoryInterface $requestFactory = null,
) {
if ($this->client instanceof Client) {
trigger_deprecation(
'sylius/paypal-plugin',
'1.6',
'Passing GuzzleHttp\Client as a first argument in the constructor is deprecated and will be removed. Use Psr\Http\Client\ClientInterface instead.',
);
}

if (null === $this->requestFactory) {
trigger_deprecation(
'sylius/paypal-plugin',
'1.6',
'Not passing $requestFactory to %s constructor is deprecated and will be removed',
self::class,
);
}
}

public function enable(PaymentMethodInterface $paymentMethod): void
Expand All @@ -38,12 +55,19 @@ public function enable(PaymentMethodInterface $paymentMethod): void
$gatewayConfig = $paymentMethod->getGatewayConfig();
$config = $gatewayConfig->getConfig();

$response = $this->client->sendRequest(
$this->requestFactory->createRequest(
'GET',
sprintf('%s/seller-permissions/check/%s', $this->baseUrl, (string) $config['merchant_id'])
)
);
if ($this->client instanceof Client && null === $this->requestFactory) {
$response = $this->client->request(
'GET',
sprintf('%s/seller-permissions/check/%s', $this->baseUrl, (string) $config['merchant_id']),
);
} else {
$response = $this->client->sendRequest(
$this->requestFactory->createRequest(
'GET',
sprintf('%s/seller-permissions/check/%s', $this->baseUrl, (string) $config['merchant_id']),
),
);
}

$content = (array) json_decode($response->getBody()->getContents(), true);
if (!((bool) $content['permissionsGranted'])) {
Expand Down
2 changes: 1 addition & 1 deletion src/Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -232,10 +232,10 @@
class="Sylius\PayPalPlugin\Enabler\PayPalPaymentMethodEnabler"
>
<argument type="service" id="Http\Discovery\Psr18Client" />
<argument type="service" id="Psr\Http\Message\RequestFactoryInterface" />
<argument>%sylius.pay_pal.facilitator_url%</argument>
<argument type="service" id="sylius.manager.payment_method" />
<argument type="service" id="Sylius\PayPalPlugin\Registrar\SellerWebhookRegistrarInterface" />
<argument type="service" id="Psr\Http\Message\RequestFactoryInterface" />
</service>

<service
Expand Down

0 comments on commit 47f7eaa

Please sign in to comment.