Passed
Pull Request — master (#5)
by Igor
03:05
created

GeneralOptions   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 17
c 1
b 1
f 0
dl 0
loc 64
ccs 18
cts 18
cp 1
rs 10
wmc 6

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getConnectionOptions() 0 3 1
A getTransportConfiguration() 0 3 1
A createFromArray() 0 6 1
A __construct() 0 12 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
     * @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