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

GeneralOptions   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 13
dl 0
loc 51
ccs 14
cts 14
cp 1
rs 10
c 1
b 1
f 0
wmc 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getConnectionOptions() 0 3 1
A createFromArray() 0 5 1
A __construct() 0 10 2
A getRequestTimeoutUs() 0 3 1
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