PluginProbe
ووسلام – همگام سازی ووکامرس و باسلام / 1.10.6
ووسلام – همگام سازی ووکامرس و باسلام v1.10.6
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 / Services / Products / PreparationDaysGuard.php

PreparationDaysGuard.php in ووسلام – همگام سازی ووکامرس و باسلام 1.10.6, at includes/Services/Products/PreparationDaysGuard.php

76 lines 2.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace SyncBasalam\Services\Products;
4
5 use SyncBasalam\Admin\Product\Data\Services\CategoryService;
6 use SyncBasalam\Admin\Settings\SettingsConfig;
7 use SyncBasalam\Jobs\Exceptions\NonRetryableException;
8 use SyncBasalam\Logger\Logger;
9
10 defined('ABSPATH') || exit;
11
12 /**
13 * Enforces the Basalam per-category maximum preparation days while product data is collected.
14 *
15 * When the requested preparation days exceed the category limit:
16 * - setting "yes" (default): the value is capped to the limit and returned.
17 * - setting "no": the product is skipped (a log is written and a NonRetryableException thrown).
18 */
19 class PreparationDaysGuard
20 {
21 private $categoryPreparationService;
22 private $categoryService;
23
24 public function __construct($categoryPreparationService = null, $categoryService = null)
25 {
26 $this->categoryPreparationService = $categoryPreparationService
27 ?: syncBasalamContainer()->get(CategoryPreparationService::class);
28 $this->categoryService = $categoryService ?: new CategoryService();
29 }
30
31 /**
32 * @return int The (possibly capped) preparation days.
33 * @throws NonRetryableException When the product must be skipped.
34 */
35 public function enforce(int $preparationDays, $product): int
36 {
37 $categoryId = $this->resolveCategoryId($product);
38 if (empty($categoryId)) return $preparationDays;
39
40 $maxPreparation = $this->categoryPreparationService->getMaxPreparationDays($categoryId);
41 if ($maxPreparation === null) return $preparationDays;
42
43 if ($preparationDays <= $maxPreparation) return $preparationDays;
44
45 $shouldCap = syncBasalamSettings()->getSettings(SettingsConfig::CAP_PREPARATION_TO_CATEGORY_MAX) !== 'no';
46
47 if ($shouldCap) {
48 return $maxPreparation;
49 }
50
51 $message = sprintf(
52 'ز�
53 ان آ�
54 اده‌سازی �
55 حصول (%d روز) بیشتر از حداکثر �
56 جاز دسته‌بندی باسلا�
57 (%d روز) است و طبق تنظی�
58 ات، �
59 حصول ارسال نشد.',
60 $preparationDays,
61 $maxPreparation
62 );
63
64 throw NonRetryableException::invalidData(esc_html($message));
65 }
66
67 private function resolveCategoryId($product): ?int
68 {
69 if (!is_object($product)) return null;
70
71 $categoryId = $this->categoryService->getPrimaryCategoryId($product);
72
73 return $categoryId ?: null;
74 }
75 }
76