Skip to content

Commit e1d3112

Browse files
committed
refactor: 👔 Extract agentic auto-register logic
1 parent 934cc7f commit e1d3112

3 files changed

Lines changed: 94 additions & 33 deletions

File tree

‎modules/ppcp-store-sync/services.php‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
use WooCommerce\PayPalCommerce\StoreSync\Merchant\MerchantMetadataProvider;
3838
use WooCommerce\PayPalCommerce\StoreSync\Registration\RegistrationService;
3939
use WooCommerce\PayPalCommerce\StoreSync\Registration\RegistrationEligibility;
40+
use WooCommerce\PayPalCommerce\StoreSync\Registration\ReconciliationService;
4041
use WooCommerce\PayPalCommerce\StoreSync\Helper\AgenticCheckoutProcessor;
4142
use WooCommerce\PayPalCommerce\StoreSync\Helper\ShippingOptionsBuilder;
4243
use WooCommerce\PayPalCommerce\StoreSync\Helper\PayPalOrderManager;
@@ -109,6 +110,13 @@
109110
$c->get( 'agentic.logger.default' )
110111
);
111112
},
113+
'agentic.registration.reconciler' => static function ( ContainerInterface $c ): ReconciliationService {
114+
return new ReconciliationService(
115+
$c->get( 'agentic.settings.model' ),
116+
$c->get( 'agentic.registration.handler' ),
117+
$c->get( 'agentic.logger.default' )
118+
);
119+
},
112120

113121
// Authentication services.
114122
'agentic.auth.key_provider' => static function (): PayPalJwkProvider {
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<?php
2+
3+
declare( strict_types = 1 );
4+
5+
namespace WooCommerce\PayPalCommerce\StoreSync\Registration;
6+
7+
use Psr\Log\LoggerInterface;
8+
use WooCommerce\PayPalCommerce\StoreSync\Setting\AgenticSettingsDataModel;
9+
10+
/**
11+
* Aligns the store's registration with the current settings: registers when the
12+
* feature is enabled but the store is not registered, and deregisters when it is
13+
* no longer enabled. Reconciliation is idempotent, so calling it when the desired
14+
* and actual states already match does nothing.
15+
*/
16+
class ReconciliationService {
17+
18+
private AgenticSettingsDataModel $settings;
19+
private RegistrationService $registration;
20+
private LoggerInterface $logger;
21+
22+
public function __construct(
23+
AgenticSettingsDataModel $settings,
24+
RegistrationService $registration,
25+
LoggerInterface $logger
26+
) {
27+
28+
$this->settings = $settings;
29+
$this->registration = $registration;
30+
$this->logger = $logger;
31+
}
32+
33+
/**
34+
* Aligns the registration state with the current settings.
35+
*
36+
* This is convergent: a failed registration disables the feature, so a
37+
* subsequent run sees the desired and actual states already matching and does
38+
* nothing.
39+
*/
40+
public function reconcile(): void {
41+
$desired = $this->settings->should_initialize_features();
42+
$actual = $this->registration->is_registered();
43+
44+
if ( $desired && ! $actual ) {
45+
if ( $this->should_auto_register() ) {
46+
$this->register();
47+
}
48+
} elseif ( ! $desired && $actual ) {
49+
$this->registration->deregister();
50+
}
51+
}
52+
53+
/**
54+
* Registers the store, disabling the feature when registration fails.
55+
*
56+
* A failure is never retried automatically: re-enabling the toggle is the
57+
* merchant's explicit retry mechanism.
58+
*/
59+
private function register(): void {
60+
$result = $this->registration->register();
61+
62+
if ( ! is_wp_error( $result ) ) {
63+
return;
64+
}
65+
66+
$this->settings->set_active( false );
67+
$this->settings->save();
68+
69+
$this->logger->warning(
70+
'Store sync auto-disabled after registration failure',
71+
array( 'error' => $result->get_error_message() )
72+
);
73+
}
74+
75+
/**
76+
* Whether auto-registration is enabled for this site.
77+
*
78+
* Disable for testing or troubleshooting by adding the following constant to
79+
* wp-config.php:
80+
*
81+
* define( 'PPCP_AGENTIC_AUTO_REGISTER', false );
82+
*/
83+
private function should_auto_register(): bool {
84+
return ! defined( 'PPCP_AGENTIC_AUTO_REGISTER' ) || PPCP_AGENTIC_AUTO_REGISTER;
85+
}
86+
}

‎modules/ppcp-store-sync/src/StoreSyncModule.php‎

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -91,17 +91,11 @@ public function run( ContainerInterface $container ): bool {
9191

9292
// Early exit if features should not be initialized.
9393
if ( ! $agentic_settings->should_initialize_features() ) {
94-
$this->ensure_deregistered( $registration_handler );
95-
9694
return true;
9795
}
9896

9997
// Feature is active and merchant is eligible: Initialize everything.
10098

101-
if ( $this->should_auto_register() ) {
102-
$this->ensure_registered( $registration_handler );
103-
}
104-
10599
// Public REST endpoints.
106100
add_action(
107101
'rest_api_init',
@@ -173,31 +167,4 @@ static function () use ( $settings, $eligibility_check ) {
173167
}
174168
);
175169
}
176-
177-
private function ensure_registered( RegistrationService $registration_service ): void {
178-
if ( $registration_service->is_registered() ) {
179-
return;
180-
}
181-
add_action( 'init', static fn() => $registration_service->register() );
182-
}
183-
184-
private function ensure_deregistered( RegistrationService $registration_service ): void {
185-
if ( ! $registration_service->is_registered() ) {
186-
return;
187-
}
188-
add_action( 'init', static fn() => $registration_service->deregister() );
189-
}
190-
191-
/**
192-
* Whether the auto-registration is enabled for this site.
193-
*
194-
* By default, the plugin automatically registers when the merchant is eligible and the feature
195-
* is enabled. For testing or troubleshooting, this behavior can be disabled by adding the
196-
* following constant to wp-config.php:
197-
*
198-
* define( 'PPCP_AGENTIC_AUTO_REGISTER', false );
199-
*/
200-
private function should_auto_register(): bool {
201-
return ! defined( 'PPCP_AGENTIC_AUTO_REGISTER' ) || PPCP_AGENTIC_AUTO_REGISTER;
202-
}
203170
}

0 commit comments

Comments
 (0)