| 1 |
<?php |
| 2 |
|
| 3 |
namespace SyncBasalam\Admin\Product; |
| 4 |
|
| 5 |
use SyncBasalam\Admin\Product\Operations\UpdateProduct; |
| 6 |
use SyncBasalam\Admin\Product\Operations\CreateProduct; |
| 7 |
use SyncBasalam\Admin\Product\Operations\ArchiveProduct; |
| 8 |
use SyncBasalam\Admin\Product\Operations\RestoreProduct; |
| 9 |
use SyncBasalam\Jobs\Exceptions\RetryableException; |
| 10 |
use SyncBasalam\Jobs\Exceptions\NonRetryableException; |
| 11 |
use SyncBasalam\Services\Products\ProductConnection; |
| 12 |
|
| 13 |
defined('ABSPATH') || exit; |
| 14 |
|
| 15 |
class ProductOperations |
| 16 |
{ |
| 17 |
private $updateOperation; |
| 18 |
private $createOperation; |
| 19 |
private $archiveOperation; |
| 20 |
private $restoreOperation; |
| 21 |
|
| 22 |
public function __construct( |
| 23 |
$updateOperation = null, |
| 24 |
$createOperation = null, |
| 25 |
$archiveOperation = null, |
| 26 |
$restoreOperation = null |
| 27 |
) { |
| 28 |
$this->updateOperation = $updateOperation ?: syncBasalamContainer()->get(UpdateProduct::class); |
| 29 |
$this->createOperation = $createOperation ?: syncBasalamContainer()->get(CreateProduct::class); |
| 30 |
$this->archiveOperation = $archiveOperation ?: syncBasalamContainer()->get(ArchiveProduct::class); |
| 31 |
$this->restoreOperation = $restoreOperation ?: syncBasalamContainer()->get(RestoreProduct::class); |
| 32 |
} |
| 33 |
|
| 34 |
public function updateExistProduct($product_id, $category_ids = null) |
| 35 |
{ |
| 36 |
try { |
| 37 |
return $this->updateOperation->execute($product_id, ['category_ids' => $category_ids]); |
| 38 |
} catch (RetryableException $e) { |
| 39 |
throw $e; |
| 40 |
} catch (NonRetryableException $e) { |
| 41 |
throw $e; |
| 42 |
} catch (\Exception $e) { |
| 43 |
throw new \Exception(esc_html($e->getMessage())); |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
public function createNewProduct($product_id, $category_ids) |
| 48 |
{ |
| 49 |
try { |
| 50 |
return $this->createOperation->execute($product_id, ['category_ids' => $category_ids]); |
| 51 |
} catch (RetryableException $e) { |
| 52 |
throw $e; |
| 53 |
} catch (NonRetryableException $e) { |
| 54 |
throw $e; |
| 55 |
} catch (\Exception $e) { |
| 56 |
throw new \Exception(esc_html($e->getMessage())); |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
public function restoreExistProduct($product_id) |
| 61 |
{ |
| 62 |
try { |
| 63 |
return $this->restoreOperation->execute($product_id); |
| 64 |
} catch (RetryableException $e) { |
| 65 |
throw $e; |
| 66 |
} catch (NonRetryableException $e) { |
| 67 |
throw $e; |
| 68 |
} catch (\Exception $e) { |
| 69 |
throw new \Exception(esc_html($e->getMessage())); |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
public function archiveExistProduct($product_id) |
| 74 |
{ |
| 75 |
try { |
| 76 |
return $this->archiveOperation->execute($product_id); |
| 77 |
} catch (RetryableException $e) { |
| 78 |
throw $e; |
| 79 |
} catch (NonRetryableException $e) { |
| 80 |
throw $e; |
| 81 |
} catch (\Exception $e) { |
| 82 |
throw new \Exception(esc_html($e->getMessage())); |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
public static function disconnectProduct($product_id) |
| 88 |
{ |
| 89 |
do_action('sync_basalam_before_disconnect_product', $product_id); |
| 90 |
|
| 91 |
ProductConnection::purge($product_id); |
| 92 |
|
| 93 |
$result = [ |
| 94 |
'success' => true, |
| 95 |
'message' => 'اتصال � |
| 96 |
حصولات با � |
| 97 |
وفقیت حذف شد.', |
| 98 |
'status_code' => 200, |
| 99 |
]; |
| 100 |
|
| 101 |
$result = apply_filters('sync_basalam_disconnect_product_result', $result, $product_id); |
| 102 |
|
| 103 |
do_action('sync_basalam_after_disconnect_product', $result, $product_id); |
| 104 |
|
| 105 |
return $result; |
| 106 |
} |
| 107 |
} |
| 108 |
|