Passed
Push — master ( 95b550...462c63 )
by Igor
01:19 queued 12s
created

GeneralOptions::getTransportConfiguration()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
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_REQUEST_TIMEOUT = 1000000;
21
22
    /**
23
     * Request timeout in microseconds.
24
     *
25
     * @var int
26
     */
27
    private $requestTimeoutUs;
28
29
    /**
30
     * Connection timeout in microseconds.
31
     *
32
     * @var ConnectionOptions
33
     */
34
    private $connectionOptions;
35
36
    /**
37
     * @var array
38
     */
39
    private $transportConfiguration;
40
41
    /**
42
     * @throws InvalidConfigException
43
     */
44 24
    public function __construct(
45
        int $requestTimeoutUs = self::DEFAULT_REQUEST_TIMEOUT,
46
        ConnectionOptions $connectionOptions = null,
47
        array $transportConfiguration = []
48
    ) {
49 24
        if ($requestTimeoutUs <= 0) {
50 1
            throw new InvalidConfigException('Request timeout must be greater than 0.');
51
        }
52
53 23
        $this->requestTimeoutUs = $requestTimeoutUs;
54 23
        $this->connectionOptions = $connectionOptions ?? new ConnectionOptions();
55 23
        $this->transportConfiguration = $transportConfiguration;
56 23
    }
57
58 21
    public function getRequestTimeoutUs(): int
59
    {
60 21
        return $this->requestTimeoutUs;
61
    }
62
63 11
    public function getConnectionOptions(): ConnectionOptions
64
    {
65 11
        return $this->connectionOptions;
66
    }
67
68 12
    public function getTransportConfiguration(): array
69
    {
70 12
        return $this->transportConfiguration;
71
    }
72
73
    /**
74
     * @throws InvalidConfigException
75
     */
76 17
    public static function createFromArray(array $options): self
77
    {
78 17
        return new self(
79 17
            $options['request_timeout_us'] ?? self::DEFAULT_REQUEST_TIMEOUT,
80 17
            ConnectionOptions::createFromArray($options['connection'] ?? []),
81 17
            $options['transport_configuration'] ?? []
82
        );
83
    }
84
}
85