Skip to content

Commit 60187ba

Browse files
committed
♻️ Remove redundant ReferenceTransactionStatus cache and rely on cached seller status
1 parent 07babdb commit 60187ba

4 files changed

Lines changed: 75 additions & 28 deletions

File tree

modules/ppcp-api-client/services.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -300,8 +300,7 @@
300300
);
301301
},
302302
'api.reference-transaction-status' => static fn ( ContainerInterface $container ): ReferenceTransactionStatus => new ReferenceTransactionStatus(
303-
$container->get( 'api.endpoint.partners' ),
304-
$container->get( 'api.reference-transaction-status-cache' )
303+
$container->get( 'api.endpoint.partners' )
305304
),
306305
'api.endpoint.catalog-products' => static function ( ContainerInterface $container ): CatalogProducts {
307306
return new CatalogProducts(
@@ -866,9 +865,6 @@ static function ( ContainerInterface $container ): PurchaseUnitSanitizer {
866865
'api.user-id-token-cache' => static function ( ContainerInterface $container ): Cache {
867866
return new Cache( 'ppcp-id-token-cache' );
868867
},
869-
'api.reference-transaction-status-cache' => static function ( ContainerInterface $container ): Cache {
870-
return new Cache( 'ppcp-reference-transaction-status-cache' );
871-
},
872868
'api.user-id-token' => static function ( ContainerInterface $container ): UserIdToken {
873869
return new UserIdToken(
874870
$container->get( 'api.host' ),

modules/ppcp-api-client/src/Helper/ReferenceTransactionStatus.php

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,10 @@
1919
*/
2020
class ReferenceTransactionStatus {
2121

22-
public const CACHE_KEY = 'ppcp_reference_transaction_enabled';
23-
24-
2522
protected PartnersEndpoint $partners_endpoint;
26-
protected Cache $cache;
2723

28-
public function __construct( PartnersEndpoint $partners_endpoint, Cache $cache ) {
24+
public function __construct( PartnersEndpoint $partners_endpoint ) {
2925
$this->partners_endpoint = $partners_endpoint;
30-
$this->cache = $cache;
3126
}
3227

3328
/**
@@ -39,29 +34,19 @@ public function __construct( PartnersEndpoint $partners_endpoint, Cache $cache )
3934
* @return bool True if reference transactions are enabled, false otherwise.
4035
*/
4136
public function reference_transaction_enabled(): bool {
42-
if ( $this->cache->has( self::CACHE_KEY ) ) {
43-
$cached = $this->cache->get( self::CACHE_KEY );
44-
if ( is_string( $cached ) || is_bool( $cached ) ) {
45-
return wc_string_to_bool( $cached );
46-
}
47-
}
48-
4937
try {
5038
foreach ( $this->partners_endpoint->seller_status()->capabilities() as $capability ) {
5139
if (
5240
$capability->name() === 'PAYPAL_WALLET_VAULTING_ADVANCED' &&
5341
$capability->status() === 'ACTIVE'
5442
) {
55-
$this->cache->set( self::CACHE_KEY, wc_bool_to_string( true ), MONTH_IN_SECONDS );
5643
return true;
5744
}
5845
}
5946
} catch ( RuntimeException $exception ) {
60-
$this->cache->set( self::CACHE_KEY, wc_bool_to_string( false ), HOUR_IN_SECONDS );
6147
return false;
6248
}
6349

64-
$this->cache->set( self::CACHE_KEY, wc_bool_to_string( false ), HOUR_IN_SECONDS );
6550
return false;
6651
}
6752
}

modules/ppcp-wc-gateway/src/WCGatewayModule.php

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
use WooCommerce\PayPalCommerce\ApiClient\Entity\Capture;
2020
use WooCommerce\PayPalCommerce\ApiClient\Entity\OrderStatus;
2121
use WooCommerce\PayPalCommerce\ApiClient\Helper\ReferenceTransactionStatus;
22-
use WooCommerce\PayPalCommerce\ApiClient\Helper\Cache;
2322
use WooCommerce\PayPalCommerce\ApiClient\Helper\DccApplies;
2423
use WooCommerce\PayPalCommerce\LocalAlternativePaymentMethods\LocalApmProductStatus;
2524
use WooCommerce\PayPalCommerce\Settings\Data\Definition\FeaturesDefinition;
@@ -450,12 +449,6 @@ static function () use ( $c ): void {
450449
$pwc_product_status->clear();
451450
}
452451

453-
$reference_transaction_status_cache = $c->get( 'api.reference-transaction-status-cache' );
454-
assert( $reference_transaction_status_cache instanceof Cache );
455-
// Clear Reference Transaction status.
456-
if ( $reference_transaction_status_cache->has( ReferenceTransactionStatus::CACHE_KEY ) ) {
457-
$reference_transaction_status_cache->delete( ReferenceTransactionStatus::CACHE_KEY );
458-
}
459452
}
460453
);
461454

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace WooCommerce\PayPalCommerce\ApiClient\Helper;
5+
6+
use Mockery;
7+
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\PartnersEndpoint;
8+
use WooCommerce\PayPalCommerce\ApiClient\Entity\SellerStatus;
9+
use WooCommerce\PayPalCommerce\ApiClient\Exception\RuntimeException;
10+
use WooCommerce\PayPalCommerce\TestCase;
11+
12+
class ReferenceTransactionStatusTest extends TestCase
13+
{
14+
public function testReferenceTransactionEnabledReturnsTrueWhenCapabilityIsActive(): void
15+
{
16+
$active_capability = new class() {
17+
public function name(): string
18+
{
19+
return 'PAYPAL_WALLET_VAULTING_ADVANCED';
20+
}
21+
22+
public function status(): string
23+
{
24+
return 'ACTIVE';
25+
}
26+
};
27+
28+
$seller_status = Mockery::mock(SellerStatus::class);
29+
$seller_status->shouldReceive('capabilities')->once()->andReturn([$active_capability]);
30+
31+
$partners_endpoint = Mockery::mock(PartnersEndpoint::class);
32+
$partners_endpoint->shouldReceive('seller_status')->once()->andReturn($seller_status);
33+
34+
$testee = new ReferenceTransactionStatus($partners_endpoint);
35+
36+
$this->assertTrue($testee->reference_transaction_enabled());
37+
}
38+
39+
public function testReferenceTransactionEnabledReturnsFalseWhenCapabilityIsMissing(): void
40+
{
41+
$inactive_capability = new class() {
42+
public function name(): string
43+
{
44+
return 'PAYPAL_WALLET_VAULTING_ADVANCED';
45+
}
46+
47+
public function status(): string
48+
{
49+
return 'INACTIVE';
50+
}
51+
};
52+
53+
$seller_status = Mockery::mock(SellerStatus::class);
54+
$seller_status->shouldReceive('capabilities')->once()->andReturn([$inactive_capability]);
55+
56+
$partners_endpoint = Mockery::mock(PartnersEndpoint::class);
57+
$partners_endpoint->shouldReceive('seller_status')->once()->andReturn($seller_status);
58+
59+
$testee = new ReferenceTransactionStatus($partners_endpoint);
60+
61+
$this->assertFalse($testee->reference_transaction_enabled());
62+
}
63+
64+
public function testReferenceTransactionEnabledReturnsFalseWhenSellerStatusFails(): void
65+
{
66+
$partners_endpoint = Mockery::mock(PartnersEndpoint::class);
67+
$partners_endpoint->shouldReceive('seller_status')->once()->andThrow(new RuntimeException('failed'));
68+
69+
$testee = new ReferenceTransactionStatus($partners_endpoint);
70+
71+
$this->assertFalse($testee->reference_transaction_enabled());
72+
}
73+
}

0 commit comments

Comments
 (0)