| 1 |
<?php |
| 2 |
|
| 3 |
namespace SyncBasalam\Admin\Product\Data; |
| 4 |
|
| 5 |
use SyncBasalam\Admin\Product\Data\Validators\ValidatorChain; |
| 6 |
use SyncBasalam\Admin\Product\Data\Validators\ImageValidator; |
| 7 |
use SyncBasalam\Admin\Product\Data\Validators\ProductExistenceValidator; |
| 8 |
use SyncBasalam\Admin\Product\Data\Validators\ProductStatusValidator; |
| 9 |
use SyncBasalam\Admin\Product\Data\Validators\WeightValidator; |
| 10 |
use SyncBasalam\Admin\Product\ProductDataFactory; |
| 11 |
use SyncBasalam\Admin\Settings\SettingsConfig; |
| 12 |
use SyncBasalam\Admin\Settings\SettingsManager; |
| 13 |
use SyncBasalam\Jobs\Exceptions\NonRetryableException; |
| 14 |
use SyncBasalam\Services\VendorSyncPolicy; |
| 15 |
use SyncBasalam\Utilities\ProductMetaKey; |
| 16 |
|
| 17 |
defined('ABSPATH') || exit; |
| 18 |
|
| 19 |
class ProductDataFacade |
| 20 |
{ |
| 21 |
private static $builder = null; |
| 22 |
private static $factory = null; |
| 23 |
private static $validator = null; |
| 24 |
|
| 25 |
private static function initialize(): void |
| 26 |
{ |
| 27 |
if (self::$builder === null) { |
| 28 |
self::$factory = new ProductDataFactory(); |
| 29 |
self::$validator = new ValidatorChain(); |
| 30 |
|
| 31 |
self::$validator->add(new ProductExistenceValidator()) |
| 32 |
->add(new ProductStatusValidator()) |
| 33 |
->add(new ImageValidator()) |
| 34 |
->add(new WeightValidator()); |
| 35 |
|
| 36 |
self::$builder = new ProductDataBuilder(self::$validator, self::$factory); |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
public static function get(int $productId, ?array $categoryIds = null): array |
| 41 |
{ |
| 42 |
self::initialize(); |
| 43 |
|
| 44 |
$product = wc_get_product($productId); |
| 45 |
if (!$product) throw new \InvalidArgumentException(esc_html("Product with ID {$productId} not found")); |
| 46 |
|
| 47 |
$basalamProductId = get_post_meta($productId, ProductMetaKey::basalamProductId(), true); |
| 48 |
$isUpdate = !empty($basalamProductId); |
| 49 |
|
| 50 |
$settings = SettingsManager::getSettings(); |
| 51 |
$syncFields = $settings[SettingsConfig::SYNC_PRODUCT_FIELDS] ?? 'all'; |
| 52 |
$vendorSyncPolicy = syncBasalamContainer()->get(VendorSyncPolicy::class); |
| 53 |
|
| 54 |
if ( |
| 55 |
$isUpdate |
| 56 |
&& !$vendorSyncPolicy->shouldRestrictUpdateFields(false) |
| 57 |
&& !SettingsManager::isProductUpdateSelectionValid($settings) |
| 58 |
) { |
| 59 |
throw NonRetryableException::invalidData(SettingsConfig::CUSTOM_PRODUCT_UPDATE_REQUIRED_MESSAGE); |
| 60 |
} |
| 61 |
|
| 62 |
$strategy = 'create'; |
| 63 |
if ($isUpdate) { |
| 64 |
if ($vendorSyncPolicy->shouldRestrictUpdateFields() || $syncFields === 'price_stock') $strategy = 'quick_update'; |
| 65 |
elseif ($syncFields === 'custom') $strategy = 'custom_update'; |
| 66 |
else $strategy = 'update'; |
| 67 |
} |
| 68 |
|
| 69 |
return self::$builder |
| 70 |
->reset() |
| 71 |
->setStrategy(self::$factory->createStrategy($strategy)) |
| 72 |
->fromWooProduct($productId) |
| 73 |
->withCategoryIds($categoryIds) |
| 74 |
->build(); |
| 75 |
} |
| 76 |
|
| 77 |
public static function validateProduct($product): void |
| 78 |
{ |
| 79 |
self::initialize(); |
| 80 |
self::$validator->validate($product); |
| 81 |
} |
| 82 |
} |
| 83 |
|