PluginProbe
ووسلام – همگام سازی ووکامرس و باسلام / 1.10.20
ووسلام – همگام سازی ووکامرس و باسلام v1.10.20
1.10.19 1.10.20 1.10.18 1.10.17 1.10.15 1.10.14 1.10.13 1.10.12 1.10.10 1.10.9 1.10.8 1.10.7 1.10.6 1.10.5 1.10.4 1.10.3 1.10.2 1.10.1 1.10.0 1.9.2 1.9.1 1.9.0 1.8.8 1.8.5 1.8.6 All 53 releases
sync-basalam / includes / Admin / Product / Data / ProductDataFacade.php

ProductDataFacade.php in ووسلام – همگام سازی ووکامرس و باسلام 1.10.20, at includes/Admin/Product/Data/ProductDataFacade.php

83 lines 3.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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