| 1 |
<?php |
| 2 |
|
| 3 |
namespace SyncBasalam\Admin\Product\Operations; |
| 4 |
|
| 5 |
use SyncBasalam\Admin\Product\Operations\AbstractProductOperation; |
| 6 |
use SyncBasalam\Services\Products\UpdateSingleProductService; |
| 7 |
use SyncBasalam\Admin\Product\Data\ProductDataFacade; |
| 8 |
use SyncBasalam\Logger\Logger; |
| 9 |
use SyncBasalam\Services\Products\Discount\DiscountTaskProcessor; |
| 10 |
use SyncBasalam\Jobs\Exceptions\NonRetryableException; |
| 11 |
use SyncBasalam\Services\VendorSyncPolicy; |
| 12 |
|
| 13 |
defined('ABSPATH') || exit; |
| 14 |
|
| 15 |
class UpdateProduct extends AbstractProductOperation |
| 16 |
{ |
| 17 |
private $updateProductService; |
| 18 |
private $discountProcessor; |
| 19 |
|
| 20 |
public function __construct( |
| 21 |
$updateProductService = null, |
| 22 |
$discountProcessor = null |
| 23 |
) |
| 24 |
{ |
| 25 |
parent::__construct(); |
| 26 |
$this->updateProductService = $updateProductService ?: syncBasalamContainer()->get(UpdateSingleProductService::class); |
| 27 |
$this->discountProcessor = $discountProcessor ?: syncBasalamContainer()->get(DiscountTaskProcessor::class); |
| 28 |
} |
| 29 |
|
| 30 |
|
| 31 |
protected function run(int $productId, array $args = []): array |
| 32 |
{ |
| 33 |
$vendorSyncPolicy = syncBasalamContainer()->get(VendorSyncPolicy::class); |
| 34 |
if (!$vendorSyncPolicy->canUpdate()) { |
| 35 |
throw NonRetryableException::invalidData($vendorSyncPolicy->getRestrictionMessage(false)); |
| 36 |
} |
| 37 |
|
| 38 |
$categoryIds = $args['category_ids'] ?? null; |
| 39 |
|
| 40 |
$productData = ProductDataFacade::get($productId, $categoryIds); |
| 41 |
|
| 42 |
$updateResult = $this->updateProductService->updateProductInBasalam($productData, $productId); |
| 43 |
|
| 44 |
if ($updateResult['success']) $this->discountProcessor->handleProductDiscount($productId); |
| 45 |
|
| 46 |
return $updateResult; |
| 47 |
} |
| 48 |
|
| 49 |
public function validate(int $productId): bool |
| 50 |
{ |
| 51 |
if (!parent::validate($productId)) return false; |
| 52 |
|
| 53 |
$validation = $this->validator->validateBasalamConnection($productId); |
| 54 |
if (!$validation['valid']) { |
| 55 |
$this->validator->logValidationResult($productId, $validation, $this->getOperationNameFromClass()); |
| 56 |
return false; |
| 57 |
} |
| 58 |
|
| 59 |
return true; |
| 60 |
} |
| 61 |
} |
| 62 |
|