| @@ -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 | +} | |