| 1 |
<?php |
| 2 |
|
| 3 |
namespace SyncBasalam\Services\Api; |
| 4 |
|
| 5 |
use SyncBasalam\Admin\Settings\SettingsConfig; |
| 6 |
use SyncBasalam\Admin\Settings\SettingsManager; |
| 7 |
use SyncBasalam\Jobs\Exceptions\RetryableException; |
| 8 |
use SyncBasalam\Jobs\Exceptions\NonRetryableException; |
| 9 |
|
| 10 |
defined('ABSPATH') || exit; |
| 11 |
|
| 12 |
class ApiResponseHandler |
| 13 |
{ |
| 14 |
public function handle($response, $url = ''): array |
| 15 |
{ |
| 16 |
if (is_wp_error($response)) { |
| 17 |
return $this->handleWpError($response, $url); |
| 18 |
} |
| 19 |
|
| 20 |
$body = wp_remote_retrieve_body($response); |
| 21 |
|
| 22 |
$statusCodeRaw = wp_remote_retrieve_response_code($response); |
| 23 |
$statusCode = is_numeric($statusCodeRaw) ? (int) $statusCodeRaw : 0; |
| 24 |
|
| 25 |
return $this->handleHttpStatusCode($statusCode, $body, $url); |
| 26 |
} |
| 27 |
|
| 28 |
|
| 29 |
private function handleWpError(\WP_Error $response, string $url = ''): array |
| 30 |
{ |
| 31 |
$errorMessage = $response->get_error_message(); |
| 32 |
$category = RequestStatusTracker::recordWpError($response, $url); |
| 33 |
|
| 34 |
if ($category === 'blocked_http') { |
| 35 |
throw new BlockedHttpRequestException($errorMessage); |
| 36 |
} |
| 37 |
|
| 38 |
if ($category === 'timeout') { |
| 39 |
throw RetryableException::apiTimeout('درخواست با تای� |
| 40 |
اوت � |
| 41 |
واجه شد: ' . $errorMessage); |
| 42 |
} |
| 43 |
|
| 44 |
if (in_array($category, ['dns_error', 'ssl_error', 'connection_error', 'network_error'], true)) { |
| 45 |
throw RetryableException::networkError('خطای شبکه: ' . $errorMessage); |
| 46 |
} |
| 47 |
|
| 48 |
throw RetryableException::temporary('خطای � |
| 49 |
وقت در درخواست: ' . $errorMessage); |
| 50 |
} |
| 51 |
|
| 52 |
private function handleHttpStatusCode(int $statusCode, $body, $url): array |
| 53 |
{ |
| 54 |
RequestStatusTracker::recordHttpStatus($statusCode, $url); |
| 55 |
|
| 56 |
if ($statusCode === 0) { |
| 57 |
throw new BlockedHttpRequestException('پاسخ نا� |
| 58 |
عتبر از درخواست HTTP (کد وضعیت نا� |
| 59 |
شخص)'); |
| 60 |
} |
| 61 |
|
| 62 |
if (in_array($statusCode, [200, 201, 202], true)) { |
| 63 |
return $this->successResponse($body, $statusCode); |
| 64 |
} |
| 65 |
|
| 66 |
if (in_array($statusCode, [408, 504], true)) { |
| 67 |
throw RetryableException::apiTimeout('درخواست با تای� |
| 68 |
اوت � |
| 69 |
واجه شد'); |
| 70 |
} |
| 71 |
|
| 72 |
if ($statusCode === 429) { |
| 73 |
throw RetryableException::rateLimit('� |
| 74 |
حدودیت تعداد درخواستها - لطفا ک� |
| 75 |
ی صبر کنید'); |
| 76 |
} |
| 77 |
|
| 78 |
if (in_array($statusCode, [500, 502, 503], true)) { |
| 79 |
throw RetryableException::serverError('خطای س� |
| 80 |
ت سرور (کد ' . $statusCode . ')'); |
| 81 |
} |
| 82 |
|
| 83 |
if ($statusCode === 401) { |
| 84 |
if ($this->isBasalamDomain($url)) { |
| 85 |
SettingsManager::updateSettings([ |
| 86 |
SettingsConfig::TOKEN => null, |
| 87 |
SettingsConfig::REFRESH_TOKEN => null, |
| 88 |
]); |
| 89 |
} |
| 90 |
throw NonRetryableException::unauthorized('دسترسی غیر� |
| 91 |
جاز - لطفا دوباره وارد شوید'); |
| 92 |
} |
| 93 |
|
| 94 |
$clientErrors = [ |
| 95 |
400 => 'درخواست نا� |
| 96 |
عتبر', |
| 97 |
403 => 'دسترسی غیر� |
| 98 |
جاز', |
| 99 |
404 => '� |
| 100 |
نبع � |
| 101 |
ورد نظر یافت نشد', |
| 102 |
422 => 'خطا در پردازش دادهها', |
| 103 |
]; |
| 104 |
|
| 105 |
if (isset($clientErrors[$statusCode])) { |
| 106 |
$errorMessage = $this->extractErrorMessageFromBody($body) ?: $clientErrors[$statusCode]; |
| 107 |
throw NonRetryableException::permanent($errorMessage . ' (کد ' . $statusCode . ')'); |
| 108 |
} |
| 109 |
|
| 110 |
// Unknown error - treat as retryable |
| 111 |
throw RetryableException::temporary('خطای غیر� |
| 112 |
نتظره (کد ' . $statusCode . ')'); |
| 113 |
} |
| 114 |
|
| 115 |
private function extractErrorMessageFromBody($body): ?string |
| 116 |
{ |
| 117 |
if (empty($body)) return null; |
| 118 |
|
| 119 |
if (is_string($body)) { |
| 120 |
$decoded = json_decode($body, true); |
| 121 |
if (is_array($decoded)) { |
| 122 |
$body = $decoded; |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
if (is_array($body)) { |
| 127 |
if (!empty($body['errors'][0]['message'])) return $body['errors'][0]['message']; |
| 128 |
if (isset($body['messages'][0]['message'])) return $body['messages'][0]['message']; |
| 129 |
if (isset($body['message'])) return $body['message']; |
| 130 |
if (isset($body['error'])) return $body['error']; |
| 131 |
} |
| 132 |
|
| 133 |
return null; |
| 134 |
} |
| 135 |
|
| 136 |
private function isBasalamDomain(string $url): bool |
| 137 |
{ |
| 138 |
$host = parse_url($url, PHP_URL_HOST); |
| 139 |
|
| 140 |
if (!is_string($host) || $host === '') return false; |
| 141 |
|
| 142 |
|
| 143 |
return $host === 'basalam.com' || substr(strtolower($host), -strlen('.basalam.com')) === '.basalam.com'; |
| 144 |
} |
| 145 |
|
| 146 |
private function successResponse($body, int $statusCode): array |
| 147 |
{ |
| 148 |
return [ |
| 149 |
'body' => $body, |
| 150 |
'status_code' => $statusCode, |
| 151 |
'success' => true |
| 152 |
]; |
| 153 |
} |
| 154 |
|
| 155 |
public function handleTimeout(string $url): array |
| 156 |
{ |
| 157 |
RequestStatusTracker::recordCategory('timeout', ['url' => $url]); |
| 158 |
throw RetryableException::apiTimeout('درخواست تای� |
| 159 |
اوت شد'); |
| 160 |
} |
| 161 |
} |
| 162 |
|