1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of JSON RPC Client. |
4
|
|
|
* |
5
|
|
|
* (c) Igor Lazarev <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Strider2038\JsonRpcClient\Configuration; |
12
|
|
|
|
13
|
|
|
use Strider2038\JsonRpcClient\Exception\InvalidConfigException; |
14
|
|
|
use Strider2038\JsonRpcClient\Response\ResponseObjectInterface; |
15
|
|
|
use Strider2038\JsonRpcClient\Transport\Http\HttpTransportTypeInterface; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @author Igor Lazarev <[email protected]> |
19
|
|
|
*/ |
20
|
|
|
class GeneralOptions |
21
|
|
|
{ |
22
|
|
|
public const DEFAULT_REQUEST_TIMEOUT = 1000000; |
23
|
|
|
|
24
|
|
|
private const SUPPORTED_HTTP_CLIENTS = [ |
25
|
|
|
HttpTransportTypeInterface::AUTODETECT, |
26
|
|
|
HttpTransportTypeInterface::GUZZLE, |
27
|
|
|
HttpTransportTypeInterface::SYMFONY, |
28
|
|
|
]; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Request timeout in microseconds. |
32
|
|
|
*/ |
33
|
|
|
private int $requestTimeoutUs; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Connection configuration. |
37
|
|
|
*/ |
38
|
|
|
private ConnectionOptions $connectionOptions; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* If enabled then all responses will be processed and client will return response payload. |
42
|
|
|
* All responses for a batch request will be sorted accordingly to request order. |
43
|
|
|
* If server returns error response, then instance of @see ErrorResponseException will be thrown. |
44
|
|
|
* |
45
|
|
|
* If disabled then client will return @see ResponseObjectInterface for each request |
46
|
|
|
* or an array of @see ResponseObjectInterface for each batch request. |
47
|
|
|
*/ |
48
|
|
|
private bool $enableResponseProcessing; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Serialization configuration. |
52
|
|
|
*/ |
53
|
|
|
private SerializationOptions $serializationOptions; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Preferred HTTP client. |
57
|
|
|
* |
58
|
|
|
* @see HttpTransportTypeInterface for available options. |
59
|
|
|
*/ |
60
|
|
|
private string $httpClientType; |
61
|
|
|
|
62
|
|
|
private array $transportConfiguration; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @throws InvalidConfigException |
66
|
|
|
*/ |
67
|
|
|
public function __construct( |
68
|
|
|
int $requestTimeoutUs = self::DEFAULT_REQUEST_TIMEOUT, |
69
|
|
|
ConnectionOptions $connectionOptions = null, |
70
|
|
|
bool $enableResponseProcessing = true, |
71
|
|
|
SerializationOptions $serializationOptions = null, |
72
|
|
|
array $transportConfiguration = [], |
73
|
|
|
string $httpClient = HttpTransportTypeInterface::AUTODETECT |
74
|
|
|
) { |
75
|
|
|
if ($requestTimeoutUs <= 0) { |
76
|
|
|
throw new InvalidConfigException('Request timeout must be greater than 0.'); |
77
|
|
|
} |
78
|
|
|
$this->validateHttpClient($httpClient); |
79
|
|
|
|
80
|
47 |
|
$this->requestTimeoutUs = $requestTimeoutUs; |
81
|
|
|
$this->connectionOptions = $connectionOptions ?? new ConnectionOptions(); |
82
|
|
|
$this->enableResponseProcessing = $enableResponseProcessing; |
83
|
|
|
$this->transportConfiguration = $transportConfiguration; |
84
|
|
|
$this->serializationOptions = $serializationOptions ?? new SerializationOptions(); |
85
|
|
|
$this->httpClientType = $httpClient; |
86
|
|
|
} |
87
|
|
|
|
88
|
47 |
|
public function getRequestTimeoutUs(): int |
89
|
1 |
|
{ |
90
|
|
|
return $this->requestTimeoutUs; |
91
|
46 |
|
} |
92
|
|
|
|
93
|
45 |
|
public function getConnectionOptions(): ConnectionOptions |
94
|
45 |
|
{ |
95
|
45 |
|
return $this->connectionOptions; |
96
|
45 |
|
} |
97
|
45 |
|
|
98
|
45 |
|
public function isResponseProcessingEnabled(): bool |
99
|
45 |
|
{ |
100
|
|
|
return $this->enableResponseProcessing; |
101
|
43 |
|
} |
102
|
|
|
|
103
|
43 |
|
public function getTransportConfiguration(): array |
104
|
|
|
{ |
105
|
|
|
return $this->transportConfiguration; |
106
|
16 |
|
} |
107
|
|
|
|
108
|
16 |
|
public function getSerializationOptions(): SerializationOptions |
109
|
|
|
{ |
110
|
|
|
return $this->serializationOptions; |
111
|
30 |
|
} |
112
|
|
|
|
113
|
30 |
|
public function getHttpClientType(): string |
114
|
|
|
{ |
115
|
|
|
return $this->httpClientType; |
116
|
25 |
|
} |
117
|
|
|
|
118
|
25 |
|
/** |
119
|
|
|
* @throws InvalidConfigException |
120
|
|
|
*/ |
121
|
30 |
|
public static function createFromArray(array $options): self |
122
|
|
|
{ |
123
|
30 |
|
return new self( |
124
|
|
|
$options['request_timeout_us'] ?? self::DEFAULT_REQUEST_TIMEOUT, |
125
|
|
|
ConnectionOptions::createFromArray($options['connection'] ?? []), |
126
|
25 |
|
$options['enable_response_processing'] ?? true, |
127
|
|
|
SerializationOptions::createFromArray($options['serialization'] ?? []), |
128
|
25 |
|
$options['transport_configuration'] ?? [], |
129
|
|
|
$options['http_client_type'] ?? HttpTransportTypeInterface::AUTODETECT |
130
|
|
|
); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
33 |
|
* @throws InvalidConfigException |
135
|
|
|
*/ |
136
|
33 |
|
private function validateHttpClient(string $httpClient): void |
137
|
33 |
|
{ |
138
|
33 |
|
if (!in_array($httpClient, self::SUPPORTED_HTTP_CLIENTS, true)) { |
139
|
33 |
|
throw new InvalidConfigException( |
140
|
33 |
|
sprintf( |
141
|
33 |
|
'Invalid value "%s" for http client option. Must be one of: %s.', |
142
|
33 |
|
$httpClient, |
143
|
|
|
implode( |
144
|
|
|
', ', |
145
|
|
|
array_map( |
146
|
|
|
static function (string $s): string { |
147
|
|
|
return '"'.$s.'"'; |
148
|
|
|
}, |
149
|
46 |
|
self::SUPPORTED_HTTP_CLIENTS |
150
|
|
|
) |
151
|
46 |
|
) |
152
|
1 |
|
) |
153
|
1 |
|
); |
154
|
1 |
|
} |
155
|
1 |
|
} |
156
|
|
|
} |
157
|
|
|
|