♻️ Refactor to use the EnvironmentConfig

This commit is contained in:
Philipp Stracker 2025-01-03 12:22:06 +01:00
parent ed13064a16
commit a1f80f1d3d
No known key found for this signature in database
3 changed files with 42 additions and 59 deletions

View file

@ -79,6 +79,7 @@ use WooCommerce\PayPalCommerce\ApiClient\Repository\PartnerReferralsData;
use WooCommerce\PayPalCommerce\ApiClient\Repository\PayeeRepository; use WooCommerce\PayPalCommerce\ApiClient\Repository\PayeeRepository;
use WooCommerce\PayPalCommerce\WcGateway\Settings\Settings; use WooCommerce\PayPalCommerce\WcGateway\Settings\Settings;
use WooCommerce\PayPalCommerce\ApiClient\Authentication\ConnectBearer; use WooCommerce\PayPalCommerce\ApiClient\Authentication\ConnectBearer;
use WooCommerce\PayPalCommerce\WcGateway\Helper\EnvironmentConfig;
return array( return array(
'api.host' => function( ContainerInterface $container ) : string { 'api.host' => function( ContainerInterface $container ) : string {
@ -879,4 +880,20 @@ return array(
'api.partner_merchant_id-sandbox' => static function( ContainerInterface $container ) : string { 'api.partner_merchant_id-sandbox' => static function( ContainerInterface $container ) : string {
return CONNECT_WOO_SANDBOX_MERCHANT_ID; return CONNECT_WOO_SANDBOX_MERCHANT_ID;
}, },
'api.env.paypal-host' => static function ( ContainerInterface $container ) : EnvironmentConfig {
/** @type EnvironmentConfig<string> Configuration object */
return EnvironmentConfig::create(
'string',
$container->get( 'api.paypal-host-production' ),
$container->get( 'api.paypal-host-sandbox' )
);
},
'api.env.endpoint.login-seller' => static function ( ContainerInterface $container ) : EnvironmentConfig {
/** @type EnvironmentConfig<LoginSeller> Configuration object */
return EnvironmentConfig::create(
LoginSeller::class,
$container->get( 'api.endpoint.login-seller-production' ),
$container->get( 'api.endpoint.login-seller-sandbox' )
);
},
); );

View file

@ -201,10 +201,8 @@ return array(
'settings.service.connection_manager' => static function ( ContainerInterface $container ) : ConnectionManager { 'settings.service.connection_manager' => static function ( ContainerInterface $container ) : ConnectionManager {
return new ConnectionManager( return new ConnectionManager(
$container->get( 'settings.data.common' ), $container->get( 'settings.data.common' ),
$container->get( 'api.paypal-host-production' ), $container->get( 'api.env.paypal-host' ),
$container->get( 'api.paypal-host-sandbox' ), $container->get( 'api.env.endpoint.login-seller' ),
$container->get( 'api.endpoint.login-seller-production' ),
$container->get( 'api.endpoint.login-seller-sandbox' ),
$container->get( 'api.repository.partner-referrals-data' ), $container->get( 'api.repository.partner-referrals-data' ),
$container->get( 'woocommerce.logger.woocommerce' ), $container->get( 'woocommerce.logger.woocommerce' ),
); );

View file

@ -9,17 +9,17 @@ declare( strict_types = 1 );
namespace WooCommerce\PayPalCommerce\Settings\Service; namespace WooCommerce\PayPalCommerce\Settings\Service;
use JsonException;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
use RuntimeException; use RuntimeException;
use JsonException;
use WooCommerce\PayPalCommerce\ApiClient\Authentication\PayPalBearer; use WooCommerce\PayPalCommerce\ApiClient\Authentication\PayPalBearer;
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\LoginSeller;
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\Orders; use WooCommerce\PayPalCommerce\ApiClient\Endpoint\Orders;
use WooCommerce\PayPalCommerce\ApiClient\Helper\InMemoryCache; use WooCommerce\PayPalCommerce\ApiClient\Helper\InMemoryCache;
use WooCommerce\PayPalCommerce\Settings\Data\CommonSettings;
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\LoginSeller;
use WooCommerce\WooCommerce\Logging\Logger\NullLogger;
use WooCommerce\PayPalCommerce\ApiClient\Repository\PartnerReferralsData; use WooCommerce\PayPalCommerce\ApiClient\Repository\PartnerReferralsData;
use Automattic\Jetpack\Partner; use WooCommerce\PayPalCommerce\Settings\Data\CommonSettings;
use WooCommerce\PayPalCommerce\WcGateway\Helper\EnvironmentConfig;
use WooCommerce\WooCommerce\Logging\Logger\NullLogger;
/** /**
* Class that manages the connection to PayPal. * Class that manages the connection to PayPal.
@ -42,16 +42,16 @@ class ConnectionManager {
/** /**
* Base URLs for the manual connection attempt, by environment. * Base URLs for the manual connection attempt, by environment.
* *
* @var array<string, string> * @var EnvironmentConfig<string>
*/ */
private array $connection_hosts; private EnvironmentConfig $connection_host;
/** /**
* Login API handler instances, by environment. * Login API handler instances, by environment.
* *
* @var array<string, LoginSeller> * @var EnvironmentConfig<LoginSeller>
*/ */
private array $login_endpoints; private EnvironmentConfig $login_endpoint;
/** /**
* Onboarding referrals data. * Onboarding referrals data.
@ -63,35 +63,24 @@ class ConnectionManager {
/** /**
* Constructor. * Constructor.
* *
* @param CommonSettings $common_settings Data model that stores the connection * @param CommonSettings $common_settings Data model that stores the connection details.
* details. * @param EnvironmentConfig $connection_host API host for direct authentication.
* @param string $live_host The API host for the live mode. * @param EnvironmentConfig $login_endpoint API handler to fetch merchant credentials.
* @param string $sandbox_host The API host for the sandbox mode. * @param PartnerReferralsData $referrals_data Partner referrals data.
* @param LoginSeller $live_login_endpoint API handler to fetch live-merchant * @param ?LoggerInterface $logger Logging instance.
* credentials.
* @param LoginSeller $sandbox_login_endpoint API handler to fetch sandbox-merchant
* credentials.
* @param PartnerReferralsData $referrals_data Partner referrals data.
* @param ?LoggerInterface $logger Logging instance.
*/ */
public function __construct( public function __construct(
CommonSettings $common_settings, string $live_host, string $sandbox_host, CommonSettings $common_settings,
LoginSeller $live_login_endpoint, LoginSeller $sandbox_login_endpoint, EnvironmentConfig $connection_host,
EnvironmentConfig $login_endpoint,
PartnerReferralsData $referrals_data, PartnerReferralsData $referrals_data,
?LoggerInterface $logger = null ?LoggerInterface $logger = null
) { ) {
$this->common_settings = $common_settings; $this->common_settings = $common_settings;
$this->connection_host = $connection_host;
$this->login_endpoint = $login_endpoint;
$this->referrals_data = $referrals_data;
$this->logger = $logger ?: new NullLogger(); $this->logger = $logger ?: new NullLogger();
$this->connection_hosts = array(
'live' => $live_host,
'sandbox' => $sandbox_host,
);
$this->login_endpoints = array(
'live' => $live_login_endpoint,
'sandbox' => $sandbox_login_endpoint,
);
$this->referrals_data = $referrals_data;
} }
/** /**
@ -226,7 +215,6 @@ class ConnectionManager {
$credentials = $this->get_credentials( $shared_id, $auth_code, $use_sandbox ); $credentials = $this->get_credentials( $shared_id, $auth_code, $use_sandbox );
// TODO. // TODO.
// $this->update_connection_details( $use_sandbox, $payee['merchant_id'], $payee['email_address'] );
} }
@ -234,26 +222,6 @@ class ConnectionManager {
// Internal helper methods // Internal helper methods
/**
* Returns the API host for the relevant environment.
*
* @param bool $for_sandbox Whether to return the sandbox API host.
* @return string
*/
private function get_host( bool $for_sandbox = false ) : string {
return $for_sandbox ? $this->connection_hosts['sandbox'] : $this->connection_hosts['live'];
}
/**
* Returns an API handler to fetch merchant credentials.
*
* @param bool $for_sandbox Whether to return the sandbox API handler.
* @return LoginSeller
*/
private function get_login_endpoint( bool $for_sandbox = false ) : LoginSeller {
return $for_sandbox ? $this->login_endpoints['sandbox'] : $this->login_endpoints['live'];
}
/** /**
* Retrieves the payee object with the merchant data by creating a minimal PayPal order. * Retrieves the payee object with the merchant data by creating a minimal PayPal order.
* *
@ -271,7 +239,7 @@ class ConnectionManager {
string $client_secret, string $client_secret,
bool $use_sandbox bool $use_sandbox
) : array { ) : array {
$host = $this->get_host( $use_sandbox ); $host = $this->connection_host->get_value( $use_sandbox );
$bearer = new PayPalBearer( $bearer = new PayPalBearer(
new InMemoryCache(), new InMemoryCache(),
@ -339,10 +307,10 @@ class ConnectionManager {
* @return array * @return array
*/ */
private function get_credentials( string $shared_id, string $auth_code, bool $use_sandbox ) : array { private function get_credentials( string $shared_id, string $auth_code, bool $use_sandbox ) : array {
$login_handler = $this->get_login_endpoint( $use_sandbox ); $login_handler = $this->login_endpoint->get_value( $use_sandbox );
$nonce = $this->referrals_data->nonce(); $nonce = $this->referrals_data->nonce();
// TODO. Always throws the exception "No token found." // TODO. Always throws the exception "No token found.".
$response = $login_handler->credentials_for( $shared_id, $auth_code, $nonce ); $response = $login_handler->credentials_for( $shared_id, $auth_code, $nonce );
// TODO. // TODO.