♻️ Simplify manual connect logic

This commit is contained in:
Philipp Stracker 2024-11-21 16:40:06 +01:00
parent 290aed8ad8
commit b0b354e266
No known key found for this signature in database
2 changed files with 46 additions and 22 deletions

View file

@ -28,7 +28,7 @@ class PayPalBearer implements Bearer {
/** /**
* The settings. * The settings.
* *
* @var ContainerInterface * @var ?ContainerInterface
*/ */
protected $settings; protected $settings;
@ -75,6 +75,7 @@ class PayPalBearer implements Bearer {
* @param string $key The key. * @param string $key The key.
* @param string $secret The secret. * @param string $secret The secret.
* @param LoggerInterface $logger The logger. * @param LoggerInterface $logger The logger.
* @param ?ContainerInterface $settings The settings.
*/ */
public function __construct( public function __construct(
Cache $cache, Cache $cache,
@ -82,7 +83,7 @@ class PayPalBearer implements Bearer {
string $key, string $key,
string $secret, string $secret,
LoggerInterface $logger, LoggerInterface $logger,
ContainerInterface $settings ?ContainerInterface $settings
) { ) {
$this->cache = $cache; $this->cache = $cache;
@ -109,6 +110,40 @@ 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. * Creates a new bearer token.
* *
@ -116,8 +151,8 @@ class PayPalBearer implements Bearer {
* @return Token * @return Token
*/ */
private function newBearer() : Token { private function newBearer() : Token {
$key = $this->settings->has( 'client_id' ) && $this->settings->get( 'client_id' ) ? $this->settings->get( 'client_id' ) : $this->key; $key = $this->get_key();
$secret = $this->settings->has( 'client_secret' ) && $this->settings->get( 'client_secret' ) ? $this->settings->get( 'client_secret' ) : $this->secret; $secret = $this->get_secret();
$url = trailingslashit( $this->host ) . 'v1/oauth2/token?grant_type=client_credentials'; $url = trailingslashit( $this->host ) . 'v1/oauth2/token?grant_type=client_credentials';
$args = array( $args = array(

View file

@ -178,24 +178,13 @@ class ConnectManualRestEndpoint extends RestEndpoint {
$host = $use_sandbox ? $this->sandbox_host : $this->live_host; $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( $bearer = new PayPalBearer(
new InMemoryCache(), new InMemoryCache(),
$host, $host,
$client_id, $client_id,
$client_secret, $client_secret,
$this->logger, $this->logger,
$empty_settings null
); );
$orders = new Orders( $orders = new Orders(