| 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 |
|