From b0b354e2668880277712ddd07d8af3d474661e52 Mon Sep 17 00:00:00 2001 From: Philipp Stracker Date: Thu, 21 Nov 2024 16:40:06 +0100 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20Simplify=20manual=20connec?= =?UTF-8?q?t=20logic?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/Authentication/PayPalBearer.php | 55 +++++++++++++++---- .../Endpoint/ConnectManualRestEndpoint.php | 13 +---- 2 files changed, 46 insertions(+), 22 deletions(-) diff --git a/modules/ppcp-api-client/src/Authentication/PayPalBearer.php b/modules/ppcp-api-client/src/Authentication/PayPalBearer.php index 3d5773ea3..7f176c218 100644 --- a/modules/ppcp-api-client/src/Authentication/PayPalBearer.php +++ b/modules/ppcp-api-client/src/Authentication/PayPalBearer.php @@ -28,7 +28,7 @@ class PayPalBearer implements Bearer { /** * The settings. * - * @var ContainerInterface + * @var ?ContainerInterface */ protected $settings; @@ -70,11 +70,12 @@ class PayPalBearer implements Bearer { /** * PayPalBearer constructor. * - * @param Cache $cache The cache. - * @param string $host The host. - * @param string $key The key. - * @param string $secret The secret. - * @param LoggerInterface $logger The logger. + * @param Cache $cache The cache. + * @param string $host The host. + * @param string $key The key. + * @param string $secret The secret. + * @param LoggerInterface $logger The logger. + * @param ?ContainerInterface $settings The settings. */ public function __construct( Cache $cache, @@ -82,7 +83,7 @@ class PayPalBearer implements Bearer { string $key, string $secret, LoggerInterface $logger, - ContainerInterface $settings + ?ContainerInterface $settings ) { $this->cache = $cache; @@ -109,15 +110,49 @@ class PayPalBearer implements Bearer { } } + /** + * Retrieves the client key for authentication. + * + * @return string The client ID from settings, or the key defined via constructor. + */ + private function get_key() : string { + if ( + $this->settings + && $this->settings->has( 'client_id' ) + && $this->settings->get( 'client_id' ) + ) { + return $this->settings->get( 'client_id' ); + } + + return $this->key; + } + + /** + * Retrieves the client secret for authentication. + * + * @return string The client secret from settings, or the value defined via constructor. + */ + private function get_secret() : string { + if ( + $this->settings + && $this->settings->has( 'client_secret' ) + && $this->settings->get( 'client_secret' ) + ) { + return $this->settings->get( 'client_secret' ); + } + + return $this->secret; + } + /** * Creates a new bearer token. * * @throws RuntimeException When request fails. * @return Token */ - private function newBearer(): Token { - $key = $this->settings->has( 'client_id' ) && $this->settings->get( 'client_id' ) ? $this->settings->get( 'client_id' ) : $this->key; - $secret = $this->settings->has( 'client_secret' ) && $this->settings->get( 'client_secret' ) ? $this->settings->get( 'client_secret' ) : $this->secret; + private function newBearer() : Token { + $key = $this->get_key(); + $secret = $this->get_secret(); $url = trailingslashit( $this->host ) . 'v1/oauth2/token?grant_type=client_credentials'; $args = array( diff --git a/modules/ppcp-settings/src/Endpoint/ConnectManualRestEndpoint.php b/modules/ppcp-settings/src/Endpoint/ConnectManualRestEndpoint.php index f39b5431e..5225b4d13 100644 --- a/modules/ppcp-settings/src/Endpoint/ConnectManualRestEndpoint.php +++ b/modules/ppcp-settings/src/Endpoint/ConnectManualRestEndpoint.php @@ -178,24 +178,13 @@ class ConnectManualRestEndpoint extends RestEndpoint { $host = $use_sandbox ? $this->sandbox_host : $this->live_host; - $empty_settings = new class() implements ContainerInterface - { - public function get( string $id ) { - throw new NotFoundException(); - } - - public function has( string $id ) { - return false; - } - }; - $bearer = new PayPalBearer( new InMemoryCache(), $host, $client_id, $client_secret, $this->logger, - $empty_settings + null ); $orders = new Orders(