Merge branch 'trunk' into PCP-3317-implement-ap-ms-via-orders-api

This commit is contained in:
Emili Castells Guasch 2024-08-12 16:08:01 +02:00
commit 1628f86d4c
5 changed files with 28 additions and 46 deletions

View file

@ -1677,8 +1677,7 @@ return array(
return new UserIdToken(
$container->get( 'api.host' ),
$container->get( 'woocommerce.logger.woocommerce' ),
$container->get( 'api.client-credentials' ),
$container->get( 'api.client-credentials-cache' )
$container->get( 'api.client-credentials' )
);
},
'api.sdk-client-token' => static function( ContainerInterface $container ): SdkClientToken {

View file

@ -96,18 +96,6 @@ class ApiModule implements ModuleInterface {
10,
2
);
add_action(
'wp_logout',
function( int $user_id ) use ( $c ) {
$client_credentials_cache = $c->get( 'api.client-credentials-cache' );
assert( $client_credentials_cache instanceof Cache );
if ( $client_credentials_cache->has( UserIdToken::CACHE_KEY . '-' . (string) $user_id ) ) {
$client_credentials_cache->delete( UserIdToken::CACHE_KEY . '-' . (string) $user_id );
}
}
);
}
/**

View file

@ -11,7 +11,6 @@ use Psr\Log\LoggerInterface;
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\RequestTrait;
use WooCommerce\PayPalCommerce\ApiClient\Exception\PayPalApiException;
use WooCommerce\PayPalCommerce\ApiClient\Exception\RuntimeException;
use WooCommerce\PayPalCommerce\ApiClient\Helper\Cache;
use WP_Error;
/**
@ -21,8 +20,6 @@ class UserIdToken {
use RequestTrait;
const CACHE_KEY = 'user-id-token-key';
/**
* The host.
*
@ -44,31 +41,21 @@ class UserIdToken {
*/
private $client_credentials;
/**
* The cache.
*
* @var Cache
*/
private $cache;
/**
* UserIdToken constructor.
*
* @param string $host The host.
* @param LoggerInterface $logger The logger.
* @param ClientCredentials $client_credentials The client credentials.
* @param Cache $cache The cache.
*/
public function __construct(
string $host,
LoggerInterface $logger,
ClientCredentials $client_credentials,
Cache $cache
ClientCredentials $client_credentials
) {
$this->host = $host;
$this->logger = $logger;
$this->client_credentials = $client_credentials;
$this->cache = $cache;
}
/**
@ -82,10 +69,6 @@ class UserIdToken {
* @throws RuntimeException If something unexpected happens.
*/
public function id_token( string $target_customer_id = '' ): string {
if ( $this->cache->has( self::CACHE_KEY . '-' . (string) get_current_user_id() ) ) {
return $this->cache->get( self::CACHE_KEY . '-' . (string) get_current_user_id() );
}
$url = trailingslashit( $this->host ) . 'v1/oauth2/token?grant_type=client_credentials&response_type=id_token';
if ( $target_customer_id ) {
$url = add_query_arg(
@ -115,11 +98,6 @@ class UserIdToken {
throw new PayPalApiException( $json, $status_code );
}
$id_token = $json->id_token;
$expires_in = (int) $json->expires_in;
$this->cache->set( self::CACHE_KEY . '-' . (string) get_current_user_id(), $id_token, $expires_in );
return $id_token;
return $json->id_token;
}
}