PluginProbe
ووسلام – همگام سازی ووکامرس و باسلام / 1.10.20
ووسلام – همگام سازی ووکامرس و باسلام v1.10.20
1.10.21 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 All 54 releases
← All changes | includes/Services/Api/AbstractApiService.php +32 -19 1.8.51.10.20 View file →
@@ -1,11 +1,12 @@
1 1 <?php
2 2
3 3 namespace SyncBasalam\Services\Api;
4 4
5 -use SyncBasalam\Logger\Logger;
6 -use SyncBasalam\Admin\Settings\SettingsConfig;
7 -use SyncBasalam\Jobs\Exceptions\RetryableException;
5 +use SyncBasalam\Logger\Logger;
6 +use SyncBasalam\Admin\Settings\SettingsConfig;
7 +use SyncBasalam\Config\Endpoints;
8 +use SyncBasalam\Jobs\Exceptions\RetryableException;
8 9
9 10 defined('ABSPATH') || exit;
10 11
11 12 abstract class AbstractApiService
@@ -33,11 +34,12 @@
33 34 $this->responseHandler = new ApiResponseHandler();
34 35 $this->circuitBreaker = new CircuitBreaker();
35 36 }
36 37
37 - public function run(string $url, $data, array $headers = []): array
38 - {
39 - $validationResult = $this->validator->validate($url, $data, $headers);
38 + public function run(string $url, $data, array $headers = []): array
39 + {
40 + $url = Endpoints::resolve($url);
41 + $validationResult = $this->validator->validate($url, $data, $headers);
40 42
41 43 if (!$validationResult['valid']) {
42 44 Logger::error("خطا در اعتبارسنجی ریکوئست: " . $validationResult['message']);
43 45 return [
@@ -61,14 +63,14 @@
61 63 $response = $this->executeRequest($preparedRequest);
62 64 $result = $this->responseHandler->handle($response, $url);
63 65 $this->circuitBreaker->recordSuccess();
64 66 return $result;
65 - } catch (\Exception $e) {
66 - if ($this->shouldRecordFailure($e, $url)) {
67 - $this->circuitBreaker->recordFailure();
68 - }
69 - throw $e;
70 - }
67 + } catch (\Throwable $e) {
68 + if ($this->shouldRecordFailure($e, $url)) {
69 + $this->circuitBreaker->recordFailure();
70 + }
71 + throw $e;
72 + }
71 73 }
72 74
73 75 protected function prepareRequest(string $url, $data, array $headers): array
74 76 {
@@ -82,11 +84,22 @@
82 84 }
83 85
84 86 abstract protected function executeRequest(array $request);
85 87
86 - protected function shouldRecordFailure(\Exception $exception, string $url): bool
87 - {
88 - if (strpos($url, 'basalam.com') === false) return false;
89 - if ($exception instanceof BlockedHttpRequestException) return false;
90 - return $exception instanceof RetryableException;
91 - }
92 -}
88 + protected function shouldRecordFailure(\Throwable $exception, string $url): bool
89 + {
90 + if (!$this->isBasalamDomain($url)) return false;
91 + if ($exception instanceof BlockedHttpRequestException) return false;
92 + return $exception instanceof RetryableException;
93 + }
94 +
95 + protected function isBasalamDomain(string $url): bool
96 + {
97 + $host = parse_url($url, PHP_URL_HOST);
98 +
99 + if (!is_string($host) || $host === '') return false;
100 +
101 + $host = strtolower($host);
102 +
103 + return $host === 'basalam.com' || substr($host, -strlen('.basalam.com')) === '.basalam.com';
104 + }
105 +}