← All changes
|
includes/Services/Products/CreateSingleProductService.php
+63
-27
1.10.5
→
1.10.20
View file →
| @@ -1,4 +1,4 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | namespace SyncBasalam\Services\Products; |
| 4 | 4 | |
| @@ -7,9 +7,10 @@ | ||
| 7 | 7 | use SyncBasalam\Config\Endpoints; |
| 8 | 8 | use SyncBasalam\Services\ApiServiceManager; |
| 9 | 9 | use SyncBasalam\Jobs\Exceptions\RetryableException; |
| 10 | 10 | use SyncBasalam\Jobs\Exceptions\NonRetryableException; |
| 11 | -use SyncBasalam\Utilities\ProductMetaKey; | |
| 11 | +use SyncBasalam\Utilities\ProductMetaKey; | |
| 12 | +use SyncBasalam\Services\VendorSyncPolicy; | |
| 12 | 13 | |
| 13 | 14 | defined('ABSPATH') || exit; |
| 14 | 15 | |
| 15 | 16 | class CreateSingleProductService |
| @@ -22,11 +23,16 @@ | ||
| 22 | 23 | $this->apiservice = syncBasalamContainer()->get(ApiServiceManager::class); |
| 23 | 24 | $this->validator = $validator ?: new CreateProductDataValidator(); |
| 24 | 25 | } |
| 25 | 26 | |
| 26 | - public function createProductInBasalam($productData, $productId) | |
| 27 | - { | |
| 28 | - $productData = apply_filters('sync_basalam_product_data_before_create', $productData, $productId); | |
| 27 | + public function createProductInBasalam($productData, $productId) | |
| 28 | + { | |
| 29 | + $vendorSyncPolicy = syncBasalamContainer()->get(VendorSyncPolicy::class); | |
| 30 | + if (!$vendorSyncPolicy->canCreate()) { | |
| 31 | + throw NonRetryableException::invalidData($vendorSyncPolicy->getRestrictionMessage(false)); | |
| 32 | + } | |
| 33 | + | |
| 34 | + $productData = apply_filters('sync_basalam_product_data_before_create', $productData, $productId); | |
| 29 | 35 | $this->ValidCreateProductData($productData, $productId); |
| 30 | 36 | |
| 31 | 37 | do_action('sync_basalam_before_create_product_api', $productId, $productData); |
| 32 | 38 | |
| @@ -33,28 +39,47 @@ | ||
| 33 | 39 | $vendorId = syncBasalamSettings()->getSettings(SettingsConfig::VENDOR_ID); |
| 34 | 40 | |
| 35 | 41 | $url = sprintf(Endpoints::PRODUCT_CREATE, $vendorId); |
| 36 | 42 | |
| 37 | - $maxDescriptionRetries = 3; | |
| 38 | - $descriptionRetry = 0; | |
| 43 | + $maxDescriptionRetries = 3; | |
| 44 | + $descriptionRetry = 0; | |
| 45 | + $skuRetry = false; | |
| 46 | + | |
| 47 | + while (true) { | |
| 48 | + try { | |
| 49 | + $request = $this->apiservice->post($url, $productData); | |
| 50 | + } catch (RetryableException $e) { | |
| 51 | + if ($this->retryWithoutDuplicateSku($productData, $e, $skuRetry)) { | |
| 52 | + continue; | |
| 53 | + } | |
| 54 | + | |
| 55 | + throw $e; | |
| 56 | + } catch (NonRetryableException $e) { | |
| 57 | + if ($this->retryWithoutDuplicateSku($productData, $e, $skuRetry)) { | |
| 58 | + continue; | |
| 59 | + } | |
| 60 | + | |
| 61 | + if ($descriptionRetry < $maxDescriptionRetries && $this->stripForbiddenDescription($e, $productData, $productId, $descriptionRetry)) { | |
| 62 | + $descriptionRetry++; | |
| 63 | + continue; | |
| 64 | + } | |
| 65 | + throw $e; | |
| 66 | + } catch (\Exception $e) { | |
| 67 | + if ($this->retryWithoutDuplicateSku($productData, $e, $skuRetry)) { | |
| 68 | + continue; | |
| 69 | + } | |
| 70 | + | |
| 71 | + throw new \Exception(esc_html($e->getMessage())); | |
| 72 | + } | |
| 73 | + | |
| 74 | + // Some API adapters return a non-2xx response instead of throwing it. | |
| 75 | + if ($this->retryWithoutDuplicateSku($productData, $request, $skuRetry)) { | |
| 76 | + continue; | |
| 77 | + } | |
| 78 | + | |
| 79 | + break; | |
| 80 | + } | |
| 39 | 81 | |
| 40 | - while (true) { | |
| 41 | - try { | |
| 42 | - $request = $this->apiservice->post($url, $productData); | |
| 43 | - break; | |
| 44 | - } catch (RetryableException $e) { | |
| 45 | - throw $e; | |
| 46 | - } catch (NonRetryableException $e) { | |
| 47 | - if ($descriptionRetry < $maxDescriptionRetries && $this->stripForbiddenDescription($e, $productData, $productId, $descriptionRetry)) { | |
| 48 | - $descriptionRetry++; | |
| 49 | - continue; | |
| 50 | - } | |
| 51 | - throw $e; | |
| 52 | - } catch (\Exception $e) { | |
| 53 | - throw new \Exception(esc_html($e->getMessage())); | |
| 54 | - } | |
| 55 | - } | |
| 56 | - | |
| 57 | 82 | if ($request['status_code'] != 201 && isset($request['status_code'])) { |
| 58 | 83 | |
| 59 | 84 | $body = $request['body'] ?? ''; |
| 60 | 85 | |
| @@ -188,12 +213,23 @@ | ||
| 188 | 213 | if ($cleaned === $productData['description']) return false; |
| 189 | 214 | |
| 190 | 215 | $productData['description'] = $cleaned; |
| 191 | 216 | |
| 192 | - return true; | |
| 193 | - } | |
| 194 | - | |
| 195 | - private function ValidCreateProductData(array $productData, int $productId): void | |
| 217 | + return true; | |
| 218 | + } | |
| 219 | + | |
| 220 | + private function retryWithoutDuplicateSku(array &$productData, $error, bool &$retried): bool | |
| 221 | + { | |
| 222 | + if ($retried || !ProductSkuRetry::hasSku($productData)) return false; | |
| 223 | + if (!ProductSkuRetry::isDuplicateSkuError($error)) return false; | |
| 224 | + | |
| 225 | + $productData = ProductSkuRetry::withoutSkus($productData); | |
| 226 | + $retried = true; | |
| 227 | + | |
| 228 | + return true; | |
| 229 | + } | |
| 230 | + | |
| 231 | + private function ValidCreateProductData(array $productData, int $productId): void | |
| 196 | 232 | { |
| 197 | 233 | $validation = $this->validator->validate($productData, $productId); |
| 198 | 234 | |
| 199 | 235 | if (!$validation['valid']) { |