PluginProbe
ووسلام – همگام سازی ووکامرس و باسلام / 1.10.12
ووسلام – همگام سازی ووکامرس و باسلام v1.10.12
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 / Services / Api / AbstractApiService.php

AbstractApiService.php in ووسلام – همگام سازی ووکامرس و باسلام 1.10.12, at includes/Services/Api/AbstractApiService.php

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