|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Builds a PartnersEndpoint with a dedicated bearer for given credentials. |
| 4 | + * |
| 5 | + * @package WooCommerce\PayPalCommerce\ApiClient\Factory |
| 6 | + */ |
| 7 | + |
| 8 | +declare( strict_types = 1 ); |
| 9 | + |
| 10 | +namespace WooCommerce\PayPalCommerce\ApiClient\Factory; |
| 11 | + |
| 12 | +use Psr\Log\LoggerInterface; |
| 13 | +use WooCommerce\PayPalCommerce\ApiClient\Endpoint\PartnersEndpoint; |
| 14 | +use WooCommerce\PayPalCommerce\ApiClient\Helper\Cache; |
| 15 | +use WooCommerce\PayPalCommerce\ApiClient\Helper\FailureRegistry; |
| 16 | +use WooCommerce\PayPalCommerce\WcGateway\Helper\EnvironmentConfig; |
| 17 | + |
| 18 | +/** |
| 19 | + * Builds a PartnersEndpoint for an explicit set of merchant credentials, backed |
| 20 | + * by its own freshly minted bearer. |
| 21 | + * |
| 22 | + * The container's shared PartnersEndpoint binds its bearer, host and merchant ID |
| 23 | + * to the connection state that existed when it was first resolved during the |
| 24 | + * request. This factory takes the credentials as arguments instead, so a caller |
| 25 | + * can obtain a working endpoint for any credentials, regardless of the active |
| 26 | + * connection or when in the request it is called. |
| 27 | + */ |
| 28 | +class PartnersEndpointFactory { |
| 29 | + |
| 30 | + /** |
| 31 | + * PayPal API host, per environment. |
| 32 | + * |
| 33 | + * @var EnvironmentConfig<string> |
| 34 | + */ |
| 35 | + private EnvironmentConfig $paypal_host; |
| 36 | + |
| 37 | + /** |
| 38 | + * Partner merchant ID, per environment. |
| 39 | + * |
| 40 | + * @var EnvironmentConfig<string> |
| 41 | + */ |
| 42 | + private EnvironmentConfig $partner_id; |
| 43 | + |
| 44 | + private SellerStatusFactory $seller_status_factory; |
| 45 | + |
| 46 | + private FailureRegistry $failure_registry; |
| 47 | + |
| 48 | + private Cache $cache; |
| 49 | + |
| 50 | + private PayPalBearerFactory $bearer_factory; |
| 51 | + |
| 52 | + private LoggerInterface $logger; |
| 53 | + |
| 54 | + /** |
| 55 | + * @param EnvironmentConfig<string> $paypal_host |
| 56 | + * @param EnvironmentConfig<string> $partner_id |
| 57 | + * @param SellerStatusFactory $seller_status_factory |
| 58 | + * @param FailureRegistry $failure_registry |
| 59 | + * @param Cache $cache |
| 60 | + * @param PayPalBearerFactory $bearer_factory |
| 61 | + * @param LoggerInterface $logger |
| 62 | + * |
| 63 | + * phpcs:disable Squiz.Commenting.FunctionComment.IncorrectTypeHint |
| 64 | + */ |
| 65 | + public function __construct( |
| 66 | + EnvironmentConfig $paypal_host, |
| 67 | + EnvironmentConfig $partner_id, |
| 68 | + SellerStatusFactory $seller_status_factory, |
| 69 | + FailureRegistry $failure_registry, |
| 70 | + Cache $cache, |
| 71 | + PayPalBearerFactory $bearer_factory, |
| 72 | + LoggerInterface $logger |
| 73 | + ) { |
| 74 | + $this->paypal_host = $paypal_host; |
| 75 | + $this->partner_id = $partner_id; |
| 76 | + $this->seller_status_factory = $seller_status_factory; |
| 77 | + $this->failure_registry = $failure_registry; |
| 78 | + $this->cache = $cache; |
| 79 | + $this->bearer_factory = $bearer_factory; |
| 80 | + $this->logger = $logger; |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Builds a PartnersEndpoint for the given merchant credentials. |
| 85 | + * |
| 86 | + * @param bool $is_sandbox Whether the credentials are for the sandbox. |
| 87 | + * @param string $client_id The merchant client ID. |
| 88 | + * @param string $client_secret The merchant client secret. |
| 89 | + * @param string $merchant_id The merchant ID. |
| 90 | + */ |
| 91 | + public function create( |
| 92 | + bool $is_sandbox, |
| 93 | + string $client_id, |
| 94 | + string $client_secret, |
| 95 | + string $merchant_id |
| 96 | + ): PartnersEndpoint { |
| 97 | + $host = (string) $this->paypal_host->get_value( $is_sandbox ); |
| 98 | + |
| 99 | + $bearer = $this->bearer_factory->create( $host, $client_id, $client_secret ); |
| 100 | + |
| 101 | + return new PartnersEndpoint( |
| 102 | + $host, |
| 103 | + $bearer, |
| 104 | + $this->logger, |
| 105 | + $this->seller_status_factory, |
| 106 | + (string) $this->partner_id->get_value( $is_sandbox ), |
| 107 | + $merchant_id, |
| 108 | + $this->failure_registry, |
| 109 | + $this->cache |
| 110 | + ); |
| 111 | + } |
| 112 | +} |
0 commit comments