Passed
Push — master ( 01f538...95b550 )
by Igor
04:19 queued 59s
created

GeneralOptions::getConnectionOptions()   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
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
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
     * @throws InvalidConfigException
38
     */
39 15
    public function __construct(
40
        int $requestTimeoutUs = self::DEFAULT_REQUEST_TIMEOUT,
41
        ConnectionOptions $connectionOptions = null
42
    ) {
43 15
        if ($requestTimeoutUs <= 0) {
44 1
            throw new InvalidConfigException('Request timeout must be greater than 0.');
45
        }
46
47 14
        $this->requestTimeoutUs = $requestTimeoutUs;
48 14
        $this->connectionOptions = $connectionOptions ?? new ConnectionOptions();
49 14
    }
50
51 11
    public function getRequestTimeoutUs(): int
52
    {
53 11
        return $this->requestTimeoutUs;
54
    }
55
56 6
    public function getConnectionOptions(): ConnectionOptions
57
    {
58 6
        return $this->connectionOptions;
59
    }
60
61
    /**
62
     * @throws InvalidConfigException
63
     */
64 7
    public static function createFromArray(array $options): self
65
    {
66 7
        return new self(
67 7
            $options['request_timeout_us'] ?? self::DEFAULT_REQUEST_TIMEOUT,
68 7
            ConnectionOptions::createFromArray($options['connection'] ?? [])
69
        );
70
    }
71
}
72