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\Transport\Http\HttpTransportTypeInterface; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @author Igor Lazarev <[email protected]> |
18
|
|
|
*/ |
19
|
|
|
class GeneralOptions |
20
|
|
|
{ |
21
|
|
|
public const DEFAULT_REQUEST_TIMEOUT = 1000000; |
22
|
|
|
|
23
|
|
|
private const SUPPORTED_HTTP_CLIENTS = [ |
24
|
|
|
HttpTransportTypeInterface::AUTODETECT, |
25
|
|
|
HttpTransportTypeInterface::GUZZLE, |
26
|
|
|
HttpTransportTypeInterface::SYMFONY, |
27
|
|
|
]; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Request timeout in microseconds. |
31
|
|
|
* |
32
|
|
|
* @var int |
33
|
|
|
*/ |
34
|
|
|
private $requestTimeoutUs; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Connection configuration. |
38
|
|
|
* |
39
|
|
|
* @var ConnectionOptions |
40
|
|
|
*/ |
41
|
|
|
private $connectionOptions; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Serialization configuration. |
45
|
|
|
* |
46
|
|
|
* @var SerializationOptions |
47
|
|
|
*/ |
48
|
|
|
private $serializationOptions; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Preferred HTTP client. |
52
|
|
|
* |
53
|
|
|
* @see HttpTransportTypeInterface for available options. |
54
|
|
|
* |
55
|
|
|
* @var string |
56
|
|
|
*/ |
57
|
|
|
private $httpClient; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @var array |
61
|
|
|
*/ |
62
|
|
|
private $transportConfiguration; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @throws InvalidConfigException |
66
|
|
|
*/ |
67
|
41 |
|
public function __construct( |
68
|
|
|
int $requestTimeoutUs = self::DEFAULT_REQUEST_TIMEOUT, |
69
|
|
|
ConnectionOptions $connectionOptions = null, |
70
|
|
|
SerializationOptions $serializationOptions = null, |
71
|
|
|
array $transportConfiguration = [], |
72
|
|
|
string $httpClient = HttpTransportTypeInterface::AUTODETECT |
73
|
|
|
) { |
74
|
41 |
|
if ($requestTimeoutUs <= 0) { |
75
|
1 |
|
throw new InvalidConfigException('Request timeout must be greater than 0.'); |
76
|
|
|
} |
77
|
40 |
|
$this->validateHttpClient($httpClient); |
78
|
|
|
|
79
|
39 |
|
$this->requestTimeoutUs = $requestTimeoutUs; |
80
|
39 |
|
$this->connectionOptions = $connectionOptions ?? new ConnectionOptions(); |
81
|
39 |
|
$this->transportConfiguration = $transportConfiguration; |
82
|
39 |
|
$this->serializationOptions = $serializationOptions ?? new SerializationOptions(); |
83
|
39 |
|
$this->httpClient = $httpClient; |
84
|
39 |
|
} |
85
|
|
|
|
86
|
37 |
|
public function getRequestTimeoutUs(): int |
87
|
|
|
{ |
88
|
37 |
|
return $this->requestTimeoutUs; |
89
|
|
|
} |
90
|
|
|
|
91
|
15 |
|
public function getConnectionOptions(): ConnectionOptions |
92
|
|
|
{ |
93
|
15 |
|
return $this->connectionOptions; |
94
|
|
|
} |
95
|
|
|
|
96
|
20 |
|
public function getTransportConfiguration(): array |
97
|
|
|
{ |
98
|
20 |
|
return $this->transportConfiguration; |
99
|
|
|
} |
100
|
|
|
|
101
|
24 |
|
public function getSerializationOptions(): SerializationOptions |
102
|
|
|
{ |
103
|
24 |
|
return $this->serializationOptions; |
104
|
|
|
} |
105
|
|
|
|
106
|
20 |
|
public function getHttpClient(): string |
107
|
|
|
{ |
108
|
20 |
|
return $this->httpClient; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @throws InvalidConfigException |
113
|
|
|
*/ |
114
|
27 |
|
public static function createFromArray(array $options): self |
115
|
|
|
{ |
116
|
27 |
|
return new self( |
117
|
27 |
|
$options['request_timeout_us'] ?? self::DEFAULT_REQUEST_TIMEOUT, |
118
|
27 |
|
ConnectionOptions::createFromArray($options['connection'] ?? []), |
119
|
27 |
|
SerializationOptions::createFromArray($options['serialization'] ?? []), |
120
|
27 |
|
$options['transport_configuration'] ?? [], |
121
|
27 |
|
$options['http_client'] ?? HttpTransportTypeInterface::AUTODETECT |
122
|
|
|
); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @throws InvalidConfigException |
127
|
|
|
*/ |
128
|
40 |
|
private function validateHttpClient(string $httpClient): void |
129
|
|
|
{ |
130
|
40 |
|
if (!in_array($httpClient, self::SUPPORTED_HTTP_CLIENTS, true)) { |
131
|
1 |
|
throw new InvalidConfigException( |
132
|
1 |
|
sprintf( |
133
|
1 |
|
'Invalid value "%s" for http client option. Must be one of: %s.', |
134
|
1 |
|
$httpClient, |
135
|
1 |
|
implode( |
136
|
1 |
|
', ', |
137
|
1 |
|
array_map( |
138
|
|
|
static function (string $s): string { |
139
|
1 |
|
return '"'.$s.'"'; |
140
|
1 |
|
}, |
141
|
1 |
|
self::SUPPORTED_HTTP_CLIENTS |
142
|
|
|
) |
143
|
|
|
) |
144
|
|
|
) |
145
|
|
|
); |
146
|
|
|
} |
147
|
39 |
|
} |
148
|
|
|
} |
149
|
|
|
|