Passed
Push — master ( 401ce9...01f538 )
by Igor
01:14 queued 10s
created

GeneralOptions   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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

4 Methods

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