Skip to content

Commit e9d8506

Browse files
committed
♻️ Apply factory-method pattern to ValidationIssue
1 parent e1bc02d commit e9d8506

30 files changed

Lines changed: 229 additions & 335 deletions

modules/ppcp-store-sync/src/CartValidation/CouponValidator/CouponValidator.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
use WooCommerce\PayPalCommerce\StoreSync\Helper\CartHelper;
2020
use WooCommerce\PayPalCommerce\StoreSync\Schema\Coupon;
2121
use WooCommerce\PayPalCommerce\StoreSync\Schema\PayPalCart;
22-
use WooCommerce\PayPalCommerce\StoreSync\Validation\CouponInvalid;
22+
use WooCommerce\PayPalCommerce\StoreSync\Validation\ValidationIssue;
2323

2424
/**
2525
* Validates coupons for Agentic Commerce using WooCommerce's native validation.
@@ -146,7 +146,7 @@ public function __construct(
146146
* Validates coupons in the cart.
147147
*
148148
* @param PayPalCart $cart The cart to validate.
149-
* @return CouponInvalid[]|null Array of validation issues or null if valid.
149+
* @return ValidationIssue[]|null Array of validation issues or null if valid.
150150
*/
151151
public function validate( PayPalCart $cart ): ?array {
152152
$coupons_to_apply = $this->get_coupons_to_apply( $cart );
@@ -222,9 +222,9 @@ private function is_wc_available(): bool {
222222
*
223223
* @param Coupon[] $coupons The coupons to check.
224224
* @param PayPalCart $cart The cart context.
225-
* @return CouponInvalid|null Validation issue or null if no conflicts.
225+
* @return ValidationIssue|null Validation issue or null if no conflicts.
226226
*/
227-
private function check_stacking_conflicts( array $coupons, PayPalCart $cart ): ?CouponInvalid {
227+
private function check_stacking_conflicts( array $coupons, PayPalCart $cart ): ?ValidationIssue {
228228
if ( count( $coupons ) < 2 ) {
229229
return null;
230230
}
@@ -279,9 +279,9 @@ private function check_stacking_conflicts( array $coupons, PayPalCart $cart ): ?
279279
* @param PayPalCart $cart The cart context.
280280
* @param int $index The coupon index.
281281
* @param WC_Discounts $discounts The WC discounts instance.
282-
* @return CouponInvalid|null Validation issue or null if valid.
282+
* @return ValidationIssue|null Validation issue or null if valid.
283283
*/
284-
private function validate_single_coupon( Coupon $coupon, PayPalCart $cart, int $index, WC_Discounts $discounts ): ?CouponInvalid {
284+
private function validate_single_coupon( Coupon $coupon, PayPalCart $cart, int $index, WC_Discounts $discounts ): ?ValidationIssue {
285285
$code = $coupon->code() ?? '';
286286
$field = $index > 0 ? "coupons[$index]" : 'coupons';
287287

@@ -358,7 +358,7 @@ private function map_error_code_to_issue_type( int $error_code ): string {
358358
* @param PayPalCart $cart The cart context.
359359
* @param WC_Coupon|null $wc_coupon The WC coupon object.
360360
* @param array $extra_context Additional context data.
361-
* @return CouponInvalid The validation issue.
361+
* @return ValidationIssue The validation issue.
362362
*/
363363
private function create_issue(
364364
string $issue_type,
@@ -367,7 +367,7 @@ private function create_issue(
367367
PayPalCart $cart,
368368
?WC_Coupon $wc_coupon,
369369
array $extra_context = array()
370-
): CouponInvalid {
370+
): ValidationIssue {
371371
$config = self::ISSUE_CONFIG[ $issue_type ] ?? self::ISSUE_CONFIG['COUPON_INVALID'];
372372

373373
$context = $this->context_builder->build_coupon_context(
@@ -418,7 +418,7 @@ private function create_issue(
418418
$context
419419
);
420420

421-
$issue = CouponInvalid::create( $config['message'] )
421+
$issue = ValidationIssue::create_coupon_invalid( $config['message'] )
422422
->user_message( $user_message )
423423
->for_field( $field )
424424
->add_resolution( $resolutions );

modules/ppcp-store-sync/src/CartValidation/CurrencyValidator.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
use WooCommerce\PayPalCommerce\StoreSync\Enums\Priority;
1313
use WooCommerce\PayPalCommerce\StoreSync\Schema\PayPalCart;
1414
use WooCommerce\PayPalCommerce\StoreSync\Validation\Resolution\ResolutionOption;
15-
use WooCommerce\PayPalCommerce\StoreSync\Validation\CurrencyMismatch;
15+
use WooCommerce\PayPalCommerce\StoreSync\Validation\ValidationIssue;
1616

1717
class CurrencyValidator implements ValidatorInterface {
1818

@@ -68,7 +68,7 @@ private function extract_currency_at_index( PayPalCart $cart, int $index ): ?arr
6868
);
6969
}
7070

71-
private function validate_consistent_currency( array $currencies, string $store_currency ): ?CurrencyMismatch {
71+
private function validate_consistent_currency( array $currencies, string $store_currency ): ?ValidationIssue {
7272
$unique_currencies = array_unique( array_column( $currencies, 'currency' ) );
7373

7474
if ( count( $unique_currencies ) === 1 ) {
@@ -83,7 +83,7 @@ private function validate_consistent_currency( array $currencies, string $store_
8383
)
8484
);
8585

86-
return CurrencyMismatch::create(
86+
return ValidationIssue::create_currency_mismatch(
8787
sprintf(
8888
'Mixed currencies detected: item %d has currency %s, expected %s',
8989
$mismatch['index'],
@@ -107,9 +107,9 @@ private function validate_consistent_currency( array $currencies, string $store_
107107
);
108108
}
109109

110-
private function validate_store_currency( string $cart_currency, int $item_index, string $store_currency ): ?CurrencyMismatch {
110+
private function validate_store_currency( string $cart_currency, int $item_index, string $store_currency ): ?ValidationIssue {
111111
if ( $cart_currency !== $store_currency ) {
112-
return CurrencyMismatch::create(
112+
return ValidationIssue::create_currency_mismatch(
113113
sprintf( 'Cart currency %s does not match store currency %s', $cart_currency, $store_currency )
114114
)
115115
->user_message( sprintf( 'This store only accepts payments in %s.', $store_currency ) )

modules/ppcp-store-sync/src/CartValidation/InventoryValidator.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515
use WooCommerce\PayPalCommerce\StoreSync\Schema\PayPalCart;
1616
use WooCommerce\PayPalCommerce\StoreSync\Schema\CartItem;
1717
use WooCommerce\PayPalCommerce\StoreSync\Validation\Resolution\ResolutionOption;
18-
use WooCommerce\PayPalCommerce\StoreSync\Validation\InsufficientQuantity;
19-
use WooCommerce\PayPalCommerce\StoreSync\Validation\ItemOutOfStock;
2018
use WooCommerce\PayPalCommerce\StoreSync\Validation\ValidationIssue;
2119

2220
class InventoryValidator implements ValidatorInterface {
@@ -55,7 +53,7 @@ private function validate_product( int $key, CartItem $item ): ?ValidationIssue
5553
}
5654

5755
if ( ! $this->product_manager->is_in_stock( $product ) ) {
58-
return ItemOutOfStock::create( 'Product is no longer available' )
56+
return ValidationIssue::create_item_out_of_stock( 'Product is no longer available' )
5957
->user_message( sprintf( '%s is currently out of stock.', $product->get_name() ) )
6058
->for_field( $field )
6159
->add_resolution(
@@ -69,7 +67,7 @@ private function validate_product( int $key, CartItem $item ): ?ValidationIssue
6967
if ( ! $this->product_manager->is_in_stock( $product, $item->quantity() ) ) {
7068
$stock_quantity = $product->get_stock_quantity() ?? 0;
7169

72-
return InsufficientQuantity::create( 'Insufficient inventory' )
70+
return ValidationIssue::create_insufficient_quantity( 'Insufficient inventory' )
7371
->user_message(
7472
sprintf(
7573
'Only %d of %s available, but %d requested.',

modules/ppcp-store-sync/src/CartValidation/PriceValidator.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
use WooCommerce\PayPalCommerce\StoreSync\Validation\Resolution\ResolutionOption;
2020
use WooCommerce\PayPalCommerce\StoreSync\Schema\Money;
2121
use WooCommerce\PayPalCommerce\StoreSync\Validation\Context\PricingErrorContext;
22-
use WooCommerce\PayPalCommerce\StoreSync\Validation\PriceMismatch;
2322
use WooCommerce\PayPalCommerce\StoreSync\Validation\ValidationIssue;
2423

2524
class PriceValidator implements ValidatorInterface {
@@ -72,11 +71,11 @@ private function validate_price_matches_store( int $key, CartItem $item, PayPalC
7271
return null;
7372
}
7473

75-
private function create_price_mismatch_issue( WC_Product $product, Money $cart_price, float $store_price, string $field, PayPalCart $cart ): PriceMismatch {
74+
private function create_price_mismatch_issue( WC_Product $product, Money $cart_price, float $store_price, string $field, PayPalCart $cart ): ValidationIssue {
7675
$price_difference = $store_price - $cart_price->value();
7776
$is_increase = $price_difference > 0;
7877

79-
return PriceMismatch::create(
78+
return ValidationIssue::create_price_mismatch(
8079
sprintf(
8180
"Price mismatch for '%s': cart price is %s but store price is %s",
8281
$product->get_name(),

modules/ppcp-store-sync/src/CartValidation/ProductValidator.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
use WooCommerce\PayPalCommerce\StoreSync\Schema\PayPalCart;
1616
use WooCommerce\PayPalCommerce\StoreSync\Schema\CartItem;
1717
use WooCommerce\PayPalCommerce\StoreSync\Validation\Resolution\ResolutionOption;
18-
use WooCommerce\PayPalCommerce\StoreSync\Validation\InvalidProduct;
1918
use WooCommerce\PayPalCommerce\StoreSync\Validation\ValidationIssue;
2019
use WooCommerce\PayPalCommerce\StoreSync\Config\IngestionConfiguration;
2120

@@ -56,7 +55,7 @@ private function validate_product( int $key, CartItem $item ): ?ValidationIssue
5655
$product = $this->product_manager->find_product( $item );
5756

5857
if ( ! $product ) {
59-
return InvalidProduct::create( "Product '{$identifier}' not found in WooCommerce catalog" )
58+
return ValidationIssue::create_invalid_product( "Product '{$identifier}' not found in WooCommerce catalog" )
6059
->user_message( "'{$item->name()}' not found in WooCommerce catalog" )
6160
->for_field( $field )
6261
->add_resolution(
@@ -67,7 +66,7 @@ private function validate_product( int $key, CartItem $item ): ?ValidationIssue
6766
}
6867

6968
if ( ! $product->is_purchasable() ) {
70-
return InvalidProduct::create( "Product '{$identifier}' is not available for purchase" )
69+
return ValidationIssue::create_invalid_product( "Product '{$identifier}' is not available for purchase" )
7170
->user_message( "'{$item->name()}' cannot be purchased at this time" )
7271
->for_field( $field )
7372
->add_resolution(
@@ -85,17 +84,17 @@ private function validate_product( int $key, CartItem $item ): ?ValidationIssue
8584
$valid_types = (array) ( $filter_args['type'] ?? array() );
8685

8786
if ( ! $support_downloads && $product->is_downloadable() ) {
88-
return InvalidProduct::create( "Downloadable product '{$identifier}' is not supported" )
87+
return ValidationIssue::create_invalid_product( "Downloadable product '{$identifier}' is not supported" )
8988
->user_message( "'{$item->name()}' cannot be purchased at this time" )
9089
->for_field( $field );
9190
}
9291
if ( ! $product->is_type( $valid_types ) ) {
93-
return InvalidProduct::create( "Product '{$identifier}' is not supported (unsupported product type)" )
92+
return ValidationIssue::create_invalid_product( "Product '{$identifier}' is not supported (unsupported product type)" )
9493
->user_message( "'{$item->name()}' cannot be purchased at this time" )
9594
->for_field( $field );
9695
}
9796
if ( ! in_array( $product->get_status(), $valid_status, true ) ) {
98-
return InvalidProduct::create( "Product '{$identifier}' is not supported (product has an unsupported status)" )
97+
return ValidationIssue::create_invalid_product( "Product '{$identifier}' is not supported (product has an unsupported status)" )
9998
->user_message( "'{$item->name()}' cannot be purchased at this time" )
10099
->for_field( $field );
101100
}

modules/ppcp-store-sync/src/CartValidation/ShippingValidator.php

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,7 @@
2525
use WooCommerce\PayPalCommerce\StoreSync\Schema\PayPalCart;
2626
use WooCommerce\PayPalCommerce\StoreSync\Validation\Context\ShippingErrorContext;
2727
use WooCommerce\PayPalCommerce\StoreSync\Validation\Resolution\ResolutionOption;
28-
use WooCommerce\PayPalCommerce\StoreSync\Validation\InvalidAddress;
29-
use WooCommerce\PayPalCommerce\StoreSync\Validation\ShippingUnavailable;
30-
use WooCommerce\PayPalCommerce\StoreSync\Validation\MissingField;
28+
use WooCommerce\PayPalCommerce\StoreSync\Validation\ValidationIssue;
3129
use WooCommerce\PayPalCommerce\StoreSync\Schema\Address;
3230
use WooCommerce\PayPalCommerce\StoreSync\Schema\CartItem;
3331

@@ -45,7 +43,7 @@ public function validate( PayPalCart $cart ) {
4543
if ( ! $shipping_address ) {
4644
if ( $this->cart_needs_shipping( $cart ) ) {
4745
return array(
48-
MissingField::create( 'Shipping address is required' )
46+
ValidationIssue::create_missing_field( 'Shipping address is required' )
4947
->user_message( 'Please provide a shipping address to continue.' )
5048
->for_field( 'shipping_address' )
5149
->add_context( ShippingErrorContext::create_missing_shipping_address() )
@@ -105,13 +103,13 @@ private function cart_needs_shipping( PayPalCart $cart ): bool {
105103
* Scenario 1: Validates that the address has all required fields and proper formats.
106104
*
107105
* @param Address $address The address to validate.
108-
* @return InvalidAddress[] Array of validation issues.
106+
* @return ValidationIssue[] Array of validation issues.
109107
*/
110108
private function validate_address_completeness( Address $address ): array {
111109
$issues = array();
112110

113111
if ( ! $address->address_line_1() ) {
114-
$issues[] = InvalidAddress::create( 'Shipping address is missing street address' )
112+
$issues[] = ValidationIssue::create_invalid_address( 'Shipping address is missing street address' )
115113
->user_message( 'Please provide a complete street address.' )
116114
->for_field( 'shipping_address.address_line_1' )
117115
->add_resolution(
@@ -127,7 +125,7 @@ private function validate_address_completeness( Address $address ): array {
127125
}
128126

129127
if ( ! $address->admin_area_2() ) {
130-
$issues[] = InvalidAddress::create( 'Shipping address is missing city' )
128+
$issues[] = ValidationIssue::create_invalid_address( 'Shipping address is missing city' )
131129
->user_message( 'Please provide a city.' )
132130
->for_field( 'shipping_address.admin_area_2' )
133131
->add_resolution(
@@ -144,7 +142,7 @@ private function validate_address_completeness( Address $address ): array {
144142

145143
$postal_code = $address->postal_code();
146144
if ( ! $postal_code ) {
147-
$issues[] = InvalidAddress::create( 'Shipping address is missing postal code' )
145+
$issues[] = ValidationIssue::create_invalid_address( 'Shipping address is missing postal code' )
148146
->user_message( 'Please provide a postal code.' )
149147
->for_field( 'shipping_address.postal_code' )
150148
->add_resolution(
@@ -173,9 +171,9 @@ private function validate_address_completeness( Address $address ): array {
173171
*
174172
* @param string $postal_code The postal code to validate.
175173
* @param string|null $country_code The country code.
176-
* @return InvalidAddress|null Validation issue if format is invalid.
174+
* @return ValidationIssue|null Validation issue if format is invalid.
177175
*/
178-
private function validate_postal_code_format( string $postal_code, ?string $country_code ): ?InvalidAddress {
176+
private function validate_postal_code_format( string $postal_code, ?string $country_code ): ?ValidationIssue {
179177
if ( ! $country_code ) {
180178
return null;
181179
}
@@ -188,7 +186,7 @@ private function validate_postal_code_format( string $postal_code, ?string $coun
188186
$is_valid = WC_Validation::is_postcode( $postal_code, $country_code );
189187

190188
if ( ! $is_valid ) {
191-
return InvalidAddress::create(
189+
return ValidationIssue::create_invalid_address(
192190
sprintf( 'Invalid postal code format for %s: %s', $country_code, $postal_code )
193191
)
194192
->user_message( 'Please provide a valid postal code.' )
@@ -209,9 +207,9 @@ private function validate_postal_code_format( string $postal_code, ?string $coun
209207
*
210208
* @param PayPalCart $cart The cart to validate.
211209
* @param Address $address The shipping address.
212-
* @return ShippingUnavailable|null Validation issue if PO Box restrictions apply.
210+
* @return ValidationIssue|null Validation issue if PO Box restrictions apply.
213211
*/
214-
private function validate_po_box_restrictions( PayPalCart $cart, Address $address ): ?ShippingUnavailable {
212+
private function validate_po_box_restrictions( PayPalCart $cart, Address $address ): ?ValidationIssue {
215213
$address_line = $address->address_line_1();
216214

217215
if ( ! $address_line || ! $this->is_po_box( $address_line ) ) {
@@ -226,7 +224,7 @@ private function validate_po_box_restrictions( PayPalCart $cart, Address $addres
226224
$signature_required_items
227225
);
228226

229-
return ShippingUnavailable::create( 'PO Box delivery not available for this order' )
227+
return ValidationIssue::create_shipping_unavailable( 'PO Box delivery not available for this order' )
230228
->user_message( 'This order contains items requiring signature confirmation and cannot be delivered to a PO Box.' )
231229
->for_field( 'shipping_address' )
232230
->add_context(
@@ -320,15 +318,15 @@ private function is_po_box( string $address_line ): bool {
320318
* Scenario 3: Validates that the country code is allowed for shipping.
321319
*
322320
* @param string|null $country_code The country code to validate.
323-
* @return ShippingUnavailable|null Validation issue if country is not allowed.
321+
* @return ValidationIssue|null Validation issue if country is not allowed.
324322
*/
325-
private function validate_country( ?string $country_code ): ?ShippingUnavailable {
323+
private function validate_country( ?string $country_code ): ?ValidationIssue {
326324
if ( ! $country_code ) {
327325
return null;
328326
}
329327

330328
if ( ! $this->is_country_allowed( $country_code ) ) {
331-
return ShippingUnavailable::create( sprintf( 'Shipping to %s is not available', $country_code ) )
329+
return ValidationIssue::create_shipping_unavailable( sprintf( 'Shipping to %s is not available', $country_code ) )
332330
->user_message( sprintf( 'We do not ship to %s.', $this->get_country_name( $country_code ) ) )
333331
->for_field( 'shipping_address.country_code' )
334332
->add_resolution(

modules/ppcp-store-sync/src/Schema/Address.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
namespace WooCommerce\PayPalCommerce\StoreSync\Schema;
1111

12-
use WooCommerce\PayPalCommerce\StoreSync\Validation\InvalidData;
12+
use WooCommerce\PayPalCommerce\StoreSync\Validation\ValidationIssue;
1313

1414
/**
1515
* @see AddressTest - Unit tests for this class.
@@ -44,14 +44,14 @@ protected function parse_fields( array $input, callable $add_issue ): void {
4444
$this->country_code = $country_code;
4545
} else {
4646
$add_issue(
47-
InvalidData::create( 'Unexpected country_code' )
47+
ValidationIssue::create_invalid_data( 'Unexpected country_code' )
4848
->user_message( 'Please provide a valid 2-letter country code.' )
4949
->for_field( 'country_code' )
5050
);
5151
}
5252
} else {
5353
$add_issue(
54-
InvalidData::create( 'Missing required field' )
54+
ValidationIssue::create_invalid_data( 'Missing required field' )
5555
->user_message( 'Please provide a country code.' )
5656
->for_field( 'country_code' )
5757
);
@@ -65,7 +65,7 @@ protected function parse_fields( array $input, callable $add_issue ): void {
6565
$this->address_line_1 = $address_line_1;
6666
} else {
6767
$add_issue(
68-
InvalidData::create( 'Field address_line_1 is too long' )
68+
ValidationIssue::create_invalid_data( 'Field address_line_1 is too long' )
6969
->user_message( 'Please provide a valid address line 1.' )
7070
->for_field( 'address_line_1' )
7171
);
@@ -79,7 +79,7 @@ protected function parse_fields( array $input, callable $add_issue ): void {
7979
$this->address_line_2 = $address_line_2;
8080
} else {
8181
$add_issue(
82-
InvalidData::create( 'Field address_line_2 is too long' )
82+
ValidationIssue::create_invalid_data( 'Field address_line_2 is too long' )
8383
->user_message( 'Please provide a valid address line 2.' )
8484
->for_field( 'address_line_2' )
8585
);
@@ -93,7 +93,7 @@ protected function parse_fields( array $input, callable $add_issue ): void {
9393
$this->admin_area_2 = $admin_area_2;
9494
} else {
9595
$add_issue(
96-
InvalidData::create( 'Field admin_area_2 is too long' )
96+
ValidationIssue::create_invalid_data( 'Field admin_area_2 is too long' )
9797
->user_message( 'Please provide a valid city.' )
9898
->for_field( 'admin_area_2' )
9999
);
@@ -107,7 +107,7 @@ protected function parse_fields( array $input, callable $add_issue ): void {
107107
$this->admin_area_1 = $admin_area_1;
108108
} else {
109109
$add_issue(
110-
InvalidData::create( 'Field admin_area_1 is too long' )
110+
ValidationIssue::create_invalid_data( 'Field admin_area_1 is too long' )
111111
->user_message( 'Please provide a valid region or state.' )
112112
->for_field( 'admin_area_1' )
113113
);
@@ -121,7 +121,7 @@ protected function parse_fields( array $input, callable $add_issue ): void {
121121
$this->postal_code = $postal_code;
122122
} else {
123123
$add_issue(
124-
InvalidData::create( 'Field postal_code is too long' )
124+
ValidationIssue::create_invalid_data( 'Field postal_code is too long' )
125125
->user_message( 'Please provide a valid postal code.' )
126126
->for_field( 'postal_code' )
127127
);

0 commit comments

Comments
 (0)