| 1 |
<?php |
| 2 |
|
| 3 |
namespace SyncBasalam\Services\Api; |
| 4 |
|
| 5 |
use SyncBasalam\Logger\Logger; |
| 6 |
use SyncBasalam\Admin\Settings\SettingsConfig; |
| 7 |
use SyncBasalam\Jobs\Exceptions\RetryableException; |
| 8 |
|
| 9 |
defined('ABSPATH') || exit; |
| 10 |
|
| 11 |
abstract class AbstractApiService |
| 12 |
{ |
| 13 |
protected array $defaultHeaders; |
| 14 |
protected $validator; |
| 15 |
protected $responseHandler; |
| 16 |
protected $circuitBreaker; |
| 17 |
|
| 18 |
public function __construct() |
| 19 |
{ |
| 20 |
$token = syncBasalamSettings()->getSettings(SettingsConfig::TOKEN); |
| 21 |
|
| 22 |
$this->defaultHeaders = [ |
| 23 |
'Content-Type' => 'application/json', |
| 24 |
'Accept' => 'application/json', |
| 25 |
'user-agent' => 'Wp-Basalam', |
| 26 |
'referer' => get_site_url(), |
| 27 |
]; |
| 28 |
|
| 29 |
if (!empty($token)) $this->defaultHeaders['Authorization'] = 'Bearer ' . $token; |
| 30 |
|
| 31 |
|
| 32 |
$this->validator = new ApiRequestValidator(); |
| 33 |
$this->responseHandler = new ApiResponseHandler(); |
| 34 |
$this->circuitBreaker = new CircuitBreaker(); |
| 35 |
} |
| 36 |
|
| 37 |
public function run(string $url, $data, array $headers = []): array |
| 38 |
{ |
| 39 |
$validationResult = $this->validator->validate($url, $data, $headers); |
| 40 |
|
| 41 |
if (!$validationResult['valid']) { |
| 42 |
Logger::error("خطا در اعتبارسنجی ریکوئست: " . $validationResult['message']); |
| 43 |
return [ |
| 44 |
'body' => null, |
| 45 |
'status_code' => 400, |
| 46 |
'error' => $validationResult['message'] |
| 47 |
]; |
| 48 |
} |
| 49 |
|
| 50 |
// Check circuit breaker — throws CircuitBreakerOpenException when OPEN. |
| 51 |
$this->circuitBreaker->isAllowed(); |
| 52 |
|
| 53 |
$preparedRequest = $this->prepareRequest($url, $data, $headers); |
| 54 |
|
| 55 |
if (isset($preparedRequest['error'])) { |
| 56 |
Logger::error("خطای ا� |
| 57 |
نیتی: " . $preparedRequest['error']); |
| 58 |
return ['body' => null, 'status_code' => 401, 'error' => $preparedRequest['error']]; |
| 59 |
} |
| 60 |
|
| 61 |
try { |
| 62 |
$response = $this->executeRequest($preparedRequest); |
| 63 |
$result = $this->responseHandler->handle($response, $url); |
| 64 |
$this->circuitBreaker->recordSuccess(); |
| 65 |
return $result; |
| 66 |
} catch (\Throwable $e) { |
| 67 |
if ($this->shouldRecordFailure($e, $url)) { |
| 68 |
$this->circuitBreaker->recordFailure(); |
| 69 |
} |
| 70 |
throw $e; |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
protected function prepareRequest(string $url, $data, array $headers): array |
| 75 |
{ |
| 76 |
$headers = array_merge($this->defaultHeaders, $headers); |
| 77 |
|
| 78 |
return [ |
| 79 |
'url' => $url, |
| 80 |
'data' => json_encode($data), |
| 81 |
'headers' => $headers, |
| 82 |
]; |
| 83 |
} |
| 84 |
|
| 85 |
abstract protected function executeRequest(array $request); |
| 86 |
|
| 87 |
protected function shouldRecordFailure(\Throwable $exception, string $url): bool |
| 88 |
{ |
| 89 |
if (!$this->isBasalamDomain($url)) return false; |
| 90 |
if ($exception instanceof BlockedHttpRequestException) return false; |
| 91 |
return $exception instanceof RetryableException; |
| 92 |
} |
| 93 |
|
| 94 |
protected function isBasalamDomain(string $url): bool |
| 95 |
{ |
| 96 |
$host = parse_url($url, PHP_URL_HOST); |
| 97 |
|
| 98 |
if (!is_string($host) || $host === '') return false; |
| 99 |
|
| 100 |
$host = strtolower($host); |
| 101 |
|
| 102 |
return $host === 'basalam.com' || substr($host, -strlen('.basalam.com')) === '.basalam.com'; |
| 103 |
} |
| 104 |
} |
| 105 |
|