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

105 lines 3.3 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\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