Skip to content

Commit ee0c84c

Browse files
committed
refac: Clearer logic around enforcing a valid and default onboarding flow source
1 parent 9fd5606 commit ee0c84c

File tree

1 file changed

+28
-25
lines changed

1 file changed

+28
-25
lines changed

plugins/woocommerce/src/Internal/Admin/Settings/PaymentsProviders/WooPayments/WooPaymentsService.php

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -145,10 +145,7 @@ public function get_onboarding_details( string $location, string $rest_path, ?st
145145
// Since getting the onboarding details is not idempotent, we will check it as an action.
146146
$this->check_if_onboarding_action_is_acceptable();
147147

148-
if ( empty( $source ) ) {
149-
// If no source is provided, we duse the default.
150-
$source = self::SESSION_ENTRY_DEFAULT;
151-
}
148+
$source = $this->validate_onboarding_source( $source );
152149

153150
return array(
154151
// This state is high-level data, independent of the type of onboarding flow.
@@ -376,9 +373,7 @@ public function mark_onboarding_step_started( string $step_id, string $location,
376373
$result = $this->save_nox_profile_onboarding_step_entry( $step_id, $location, 'statuses', $statuses );
377374

378375
if ( $result ) {
379-
if ( empty( $source ) ) {
380-
$source = self::SESSION_ENTRY_DEFAULT;
381-
}
376+
$source = $this->validate_onboarding_source( $source );
382377

383378
// Record an event for the step being started.
384379
$this->record_event(
@@ -460,9 +455,7 @@ public function mark_onboarding_step_completed( string $step_id, string $locatio
460455
$result = $this->save_nox_profile_onboarding_step_entry( $step_id, $location, 'statuses', $statuses );
461456

462457
if ( $result ) {
463-
if ( empty( $source ) ) {
464-
$source = self::SESSION_ENTRY_DEFAULT;
465-
}
458+
$source = $this->validate_onboarding_source( $source );
466459

467460
// Record an event for the step being completed.
468461
$this->record_event(
@@ -937,9 +930,7 @@ public function onboarding_test_account_init( string $location, ?string $source
937930
// Lock the onboarding to prevent concurrent actions.
938931
$this->set_onboarding_lock();
939932

940-
if ( empty( $source ) ) {
941-
$source = self::SESSION_ENTRY_DEFAULT;
942-
}
933+
$source = $this->validate_onboarding_source( $source );
943934

944935
try {
945936
// Call the WooPayments API to initialize the test account.
@@ -1078,9 +1069,7 @@ public function get_onboarding_kyc_session( string $location, array $self_assess
10781069
// Lock the onboarding to prevent concurrent actions.
10791070
$this->set_onboarding_lock();
10801071

1081-
if ( empty( $source ) ) {
1082-
$source = self::SESSION_ENTRY_DEFAULT;
1083-
}
1072+
$source = $this->validate_onboarding_source( $source );
10841073

10851074
try {
10861075
// Call the WooPayments API to get the KYC session.
@@ -1202,9 +1191,7 @@ public function finish_onboarding_kyc_session( string $location, ?string $source
12021191
// Lock the onboarding to prevent concurrent actions.
12031192
$this->set_onboarding_lock();
12041193

1205-
if ( empty( $source ) ) {
1206-
$source = self::SESSION_ENTRY_DEFAULT;
1207-
}
1194+
$source = $this->validate_onboarding_source( $source );
12081195

12091196
try {
12101197
// Call the WooPayments API to finalize the KYC session.
@@ -1372,9 +1359,7 @@ public function reset_onboarding( string $location, string $from = '', ?string $
13721359
// Lock the onboarding to prevent concurrent actions.
13731360
$this->set_onboarding_lock();
13741361

1375-
if ( empty( $source ) ) {
1376-
$source = self::SESSION_ENTRY_DEFAULT;
1377-
}
1362+
$source = $this->validate_onboarding_source( $source );
13781363

13791364
// Before resetting the account, record its details for tracking purposes.
13801365
$event_props = array(
@@ -1462,9 +1447,7 @@ public function disable_test_account( string $location, string $from = '', ?stri
14621447
// Lock the onboarding to prevent concurrent actions.
14631448
$this->set_onboarding_lock();
14641449

1465-
if ( empty( $source ) ) {
1466-
$source = self::SESSION_ENTRY_DEFAULT;
1467-
}
1450+
$source = $this->validate_onboarding_source( $source );
14681451

14691452
try {
14701453
// Call the WooPayments API to disable the test account and prepare for the switch to live.
@@ -2469,4 +2452,24 @@ private function get_overview_page_url(): string {
24692452
admin_url( 'admin.php' )
24702453
);
24712454
}
2455+
2456+
/**
2457+
* Check the onboarding source and ensure it is a valid value.
2458+
*
2459+
* @param string|null $source The source of the onboarding request.
2460+
*
2461+
* @return string The validated onboarding source.
2462+
*/
2463+
private function validate_onboarding_source( ?string $source ): string {
2464+
if ( empty( $source ) ) {
2465+
return self::SESSION_ENTRY_DEFAULT;
2466+
}
2467+
2468+
$valid_sources = array(
2469+
self::SESSION_ENTRY_DEFAULT,
2470+
self::SESSION_ENTRY_LYS,
2471+
);
2472+
2473+
return in_array( $source, $valid_sources, true ) ? $source : self::SESSION_ENTRY_DEFAULT;
2474+
}
24722475
}

0 commit comments

Comments
 (0)