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 / Admin / Product / Operations / AbstractProductOperation.php

AbstractProductOperation.php in ووسلام – همگام سازی ووکامرس و باسلام 1.10.6, at includes/Admin/Product/Operations/AbstractProductOperation.php

161 lines 5.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\Admin\Product\Operations;
4
5 use SyncBasalam\Admin\Product\Operations\ProductOperationInterface;
6 use SyncBasalam\Admin\Product\Validators\ProductOperationValidator;
7 use SyncBasalam\Jobs\Exceptions\RetryableException;
8 use SyncBasalam\Jobs\Exceptions\NonRetryableException;
9 use SyncBasalam\Logger\Logger;
10 use SyncBasalam\Utilities\ProductMetaKey;
11
12 defined('ABSPATH') || exit;
13
14 abstract class AbstractProductOperation implements ProductOperationInterface
15 {
16 protected $validator;
17
18 public function __construct($validator = null)
19 {
20 $this->validator = $validator ?: syncBasalamContainer()->get(ProductOperationValidator::class);
21 }
22
23 final public function execute(int $product_id, array $args = []): array
24 {
25 $operationName = $this->getOperationNameFromClass();
26
27 do_action('sync_basalam_before_product_operation', $product_id, $args, $operationName);
28
29 do_action("sync_basalam_before_{$operationName}", $product_id, $args);
30
31 try {
32 $validation = $this->validate($product_id);
33
34 if (!$validation) {
35 $result = $this->buildValidationErrorResult($product_id);
36
37 $result = apply_filters('sync_basalam_product_operation_validation_error', $result, $product_id, $operationName);
38
39 return $result;
40 }
41
42 $result = $this->run($product_id, $args);
43
44 $this->logSuccess($product_id, $result);
45
46 $result = apply_filters('sync_basalam_product_operation_result', $result, $product_id, $args, $operationName);
47
48 $result = apply_filters("sync_basalam_{$operationName}_result", $result, $product_id, $args);
49
50 do_action('sync_basalam_after_product_operation', $result, $product_id, $args, $operationName);
51
52 do_action("sync_basalam_after_{$operationName}", $result, $product_id, $args);
53
54 return $result;
55 } catch (RetryableException $e) {
56 throw $e;
57 } catch (NonRetryableException $e) {
58 throw $e;
59 } catch (\Exception $e) {
60 $result = $this->handleException($e, $product_id);
61
62 return apply_filters('sync_basalam_product_operation_exception', $result, $product_id, $operationName);
63 }
64 }
65
66 abstract protected function run(int $product_id, array $args = []): array;
67
68 public function validate(int $product_id): bool
69 {
70 $result = $this->validator->validate($product_id);
71
72 if (!$result['valid']) $this->validator->logValidationResult($product_id, $result, $this->getPersianOperationName());
73
74 return $result['valid'];
75 }
76
77 protected function handleException(\Throwable $exception, int $product_id): array
78 {
79 update_post_meta($product_id, ProductMetaKey::basalamProductSyncStatus(), 'no');
80
81 Logger::error(sprintf("خطا در %s: %s", $this->getPersianOperationName(), $exception->getMessage()), [
82 'product_id' => $product_id,
83 'ع�
84 لیات' => $this->getPersianOperationName(),
85 ]);
86
87 return $this->buildErrorResult($exception, $product_id);
88 }
89
90 protected function buildErrorResult(\Throwable $exception, int $product_id): array
91 {
92 return [
93 'success' => false,
94 'message' => sprintf('فرایند %s �
95 حصول نا�
96 وفق بود: %s', $this->getPersianOperationName(), $exception->getMessage()),
97 'error' => $exception->getMessage(),
98 'status_code' => $this->getStatusCodeFromException($exception),
99 'product_id' => $product_id
100 ];
101 }
102
103 protected function buildValidationErrorResult(int $product_id): array
104 {
105 $validation = $this->validator->validate($product_id);
106
107 return [
108 'success' => false,
109 'message' => $validation['message'],
110 'error' => 'validation_failed',
111 'status_code' => 422,
112 'product_id' => $product_id,
113 'validation_error' => true
114 ];
115 }
116
117 protected function logSuccess(int $product_id, array $result): void
118 {
119 if ($result['success']) {
120 Logger::info(sprintf("%s با �
121 وفقیت انجا�
122 شد.", $this->getPersianOperationName()), [
123 'product_id' => $product_id,
124 'ع�
125 لیات' => $this->getPersianOperationName(),
126 'نتیجه' => $result
127 ]);
128 }
129 }
130
131 protected function getStatusCodeFromException(\Throwable $exception): int
132 {
133 if (method_exists($exception, 'getCode') && is_int($exception->getCode())) return $exception->getCode();
134 return 400;
135 }
136
137 protected function getOperationNameFromClass(): string
138 {
139 $className = static::class;
140 $className = substr($className, strrpos($className, '\\') + 1);
141 return lcfirst($className);
142 }
143
144 protected function getPersianOperationName(): string
145 {
146 $operationNames = [
147 'updateProduct' => 'بروزرسانی �
148 حصول',
149 'createProduct' => 'ایجاد �
150 حصول',
151 'restoreProduct' => 'بازگردانی �
152 حصول',
153 'archiveProduct' => 'بایگانی �
154 حصول',
155 ];
156
157 $operationName = $this->getOperationNameFromClass();
158 return $operationNames[$operationName] ?? $operationName;
159 }
160 }
161