PluginProbe
ووسلام – همگام سازی ووکامرس و باسلام / 1.10.7
ووسلام – همگام سازی ووکامرس و باسلام v1.10.7
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.7, at includes/Admin/Product/Data/ProductDataFacade.php

69 lines 2.3 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\Utilities\ProductMetaKey;
12
13 defined('ABSPATH') || exit;
14
15 class ProductDataFacade
16 {
17 private static $builder = null;
18 private static $factory = null;
19 private static $validator = null;
20
21 private static function initialize(): void
22 {
23 if (self::$builder === null) {
24 self::$factory = new ProductDataFactory();
25 self::$validator = new ValidatorChain();
26
27 self::$validator->add(new ProductExistenceValidator())
28 ->add(new ProductStatusValidator())
29 ->add(new ImageValidator())
30 ->add(new WeightValidator());
31
32 self::$builder = new ProductDataBuilder(self::$validator, self::$factory);
33 }
34 }
35
36 public static function get(int $productId, ?array $categoryIds = null): array
37 {
38 self::initialize();
39
40 $product = wc_get_product($productId);
41 if (!$product) throw new \InvalidArgumentException(esc_html("Product with ID {$productId} not found"));
42
43 $basalamProductId = get_post_meta($productId, ProductMetaKey::basalamProductId(), true);
44 $isUpdate = !empty($basalamProductId);
45
46 $syncFields = syncBasalamSettings()->getSettings(\SyncBasalam\Admin\Settings\SettingsConfig::SYNC_PRODUCT_FIELDS);
47
48 $strategy = 'create';
49 if ($isUpdate) {
50 if ($syncFields === 'price_stock') $strategy = 'quick_update';
51 elseif ($syncFields === 'custom') $strategy = 'custom_update';
52 else $strategy = 'update';
53 }
54
55 return self::$builder
56 ->reset()
57 ->setStrategy(self::$factory->createStrategy($strategy))
58 ->fromWooProduct($productId)
59 ->withCategoryIds($categoryIds)
60 ->build();
61 }
62
63 public static function validateProduct($product): void
64 {
65 self::initialize();
66 self::$validator->validate($product);
67 }
68 }
69