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
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @author Igor Lazarev <[email protected]> |
17
|
|
|
*/ |
18
|
|
|
class GeneralOptions |
19
|
|
|
{ |
20
|
|
|
public const DEFAULT_CONNECTION_TIMEOUT = 1000000; |
21
|
|
|
public const DEFAULT_REQUEST_TIMEOUT = 1000000; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Connection timeout in microseconds. |
25
|
|
|
* |
26
|
|
|
* @var int |
27
|
|
|
*/ |
28
|
|
|
private $connectionTimeoutUs; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Request timeout in microseconds. |
32
|
|
|
* |
33
|
|
|
* @var int |
34
|
|
|
*/ |
35
|
|
|
private $requestTimeoutUs; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @throws InvalidConfigException |
39
|
|
|
*/ |
40
|
15 |
|
public function __construct( |
41
|
|
|
int $connectionTimeoutUs = self::DEFAULT_CONNECTION_TIMEOUT, |
42
|
|
|
int $requestTimeoutUs = self::DEFAULT_REQUEST_TIMEOUT |
43
|
|
|
) { |
44
|
15 |
|
if ($connectionTimeoutUs <= 0 || $requestTimeoutUs <= 0) { |
45
|
1 |
|
throw new InvalidConfigException('Timeout must be greater than 0.'); |
46
|
|
|
} |
47
|
|
|
|
48
|
14 |
|
$this->connectionTimeoutUs = $connectionTimeoutUs; |
49
|
14 |
|
$this->requestTimeoutUs = $requestTimeoutUs; |
50
|
14 |
|
} |
51
|
|
|
|
52
|
7 |
|
public function getConnectionTimeoutUs(): int |
53
|
|
|
{ |
54
|
7 |
|
return $this->connectionTimeoutUs; |
55
|
|
|
} |
56
|
|
|
|
57
|
11 |
|
public function getRequestTimeoutUs(): int |
58
|
|
|
{ |
59
|
11 |
|
return $this->requestTimeoutUs; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @throws InvalidConfigException |
64
|
|
|
*/ |
65
|
7 |
|
public static function createFromArray(array $options): self |
66
|
|
|
{ |
67
|
7 |
|
return new self( |
68
|
7 |
|
$options['connection_timeout_us'] ?? self::DEFAULT_CONNECTION_TIMEOUT, |
69
|
7 |
|
$options['request_timeout_us'] ?? self::DEFAULT_REQUEST_TIMEOUT |
70
|
|
|
); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|