♻️ Replace builder pattern with function arguments

Instead of using `with_products()` to generate a new object, we now simply pass the products array to the data() function. This adds a lot more transparency to the process and makes the code easier to read and maintain.

The builder pattern is only implemented in 2 files, and both are rarely used, which makes it an overly complex solution for a simple task (in this project).
This commit is contained in:
Philipp Stracker 2025-03-05 14:59:55 +01:00
parent 112ab6485d
commit 1c17e7199f
No known key found for this signature in database
3 changed files with 14 additions and 37 deletions

View file

@ -22,39 +22,13 @@ class PartnerReferralsData {
*/
private DccApplies $dcc_applies;
/**
* The list of products ('PPCP', 'EXPRESS_CHECKOUT').
*
* @var string[]
*/
private $products;
/**
* PartnerReferralsData constructor.
*
* @param DccApplies $dcc_applies The DCC Applies helper.
*/
public function __construct(
DccApplies $dcc_applies
) {
public function __construct( DccApplies $dcc_applies ) {
$this->dcc_applies = $dcc_applies;
$this->products = array(
$this->dcc_applies->for_country_currency() ? 'PPCP' : 'EXPRESS_CHECKOUT',
);
}
/**
* Returns a new copy of this object with the given value set.
*
* @param string[] $products The list of products ('PPCP', 'EXPRESS_CHECKOUT').
* @return static
*/
public function with_products( array $products ): self {
$obj = clone $this;
$obj->products = $products;
return $obj;
}
/**
@ -69,9 +43,17 @@ class PartnerReferralsData {
/**
* Returns the data.
*
* @param string[] $products The list of products to use ('PPCP', 'EXPRESS_CHECKOUT').
* Default is based on DCC availability.
* @return array
*/
public function data(): array {
public function data( array $products = array() ) : array {
if ( ! $products ) {
$products = array(
$this->dcc_applies->for_country_currency() ? 'PPCP' : 'EXPRESS_CHECKOUT',
);
}
/**
* Returns the partners referrals data.
*/
@ -95,7 +77,7 @@ class PartnerReferralsData {
),
'show_add_credit_card' => true,
),
'products' => $this->products,
'products' => $products,
'legal_consents' => array(
array(
'type' => 'SHARE_DATA_CONSENT',

View file

@ -103,12 +103,8 @@ class OnboardingRenderer {
'displayMode' => 'minibrowser',
);
$data = $this->partner_referrals_data
->with_products( $products )
->data();
$environment = $is_production ? 'production' : 'sandbox';
$product = 'PPCP' === $data['products'][0] ? 'ppcp' : 'express_checkout';
$product = strtolower( $products[0] ?? 'express_checkout' );
$cache_key = $environment . '-' . $product;
$onboarding_url = new OnboardingUrl( $this->cache, $cache_key, get_current_user_id() );
@ -122,6 +118,7 @@ class OnboardingRenderer {
$onboarding_url->init();
$data = $this->partner_referrals_data->data( $products );
$data = $this->partner_referrals_data
->append_onboarding_token( $data, $onboarding_url->token() ?: '' );

View file

@ -202,9 +202,7 @@ class ConnectionUrlGenerator {
* @return array The prepared referral data.
*/
protected function prepare_referral_data( array $products, string $onboarding_token ) : array {
$data = $this->referrals_data
->with_products( $products )
->data();
$data = $this->referrals_data->data( $products );
return $this->referrals_data->append_onboarding_token( $data, $onboarding_token );
}