Passed
Push — master ( 462c63...daf9a5 )
by Igor
01:08 queued 19s
created

GeneralOptions   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 24
dl 0
loc 81
ccs 24
cts 24
cp 1
rs 10
c 1
b 1
f 0
wmc 8

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getConnectionOptions() 0 3 1
A getTransportConfiguration() 0 3 1
A getSerializer() 0 3 1
A createFromArray() 0 7 1
A __construct() 0 17 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_REQUEST_TIMEOUT = 1000000;
21
    public const DEFAULT_SERIALIZER = 'object';
22
23
    /**
24
     * Request timeout in microseconds.
25
     *
26
     * @var int
27
     */
28
    private $requestTimeoutUs;
29
30
    /**
31
     * Connection timeout in microseconds.
32
     *
33
     * @var ConnectionOptions
34
     */
35
    private $connectionOptions;
36
37
    /**
38
     * @var array
39
     */
40
    private $transportConfiguration;
41
42
    /**
43
     * @var string
44
     */
45
    private $serializer;
46
47
    /**
48
     * @throws InvalidConfigException
49
     */
50 26
    public function __construct(
51
        int $requestTimeoutUs = self::DEFAULT_REQUEST_TIMEOUT,
52
        ConnectionOptions $connectionOptions = null,
53
        array $transportConfiguration = [],
54
        string $serializer = 'object'
55
    ) {
56 26
        if ($requestTimeoutUs <= 0) {
57 1
            throw new InvalidConfigException('Request timeout must be greater than 0.');
58
        }
59 25
        if (!in_array($serializer, [self::DEFAULT_SERIALIZER, 'array'], true)) {
60 1
            throw new InvalidConfigException('Serializer option must be equal to one of: "object" or "array".');
61
        }
62
63 24
        $this->requestTimeoutUs = $requestTimeoutUs;
64 24
        $this->connectionOptions = $connectionOptions ?? new ConnectionOptions();
65 24
        $this->transportConfiguration = $transportConfiguration;
66 24
        $this->serializer = $serializer;
67 24
    }
68
69 22
    public function getRequestTimeoutUs(): int
70
    {
71 22
        return $this->requestTimeoutUs;
72
    }
73
74 12
    public function getConnectionOptions(): ConnectionOptions
75
    {
76 12
        return $this->connectionOptions;
77
    }
78
79 12
    public function getTransportConfiguration(): array
80
    {
81 12
        return $this->transportConfiguration;
82
    }
83
84 17
    public function getSerializer(): string
85
    {
86 17
        return $this->serializer;
87
    }
88
89
    /**
90
     * @throws InvalidConfigException
91
     */
92 18
    public static function createFromArray(array $options): self
93
    {
94 18
        return new self(
95 18
            $options['request_timeout_us'] ?? self::DEFAULT_REQUEST_TIMEOUT,
96 18
            ConnectionOptions::createFromArray($options['connection'] ?? []),
97 18
            $options['transport_configuration'] ?? [],
98 18
            $options['serializer'] ?? self::DEFAULT_SERIALIZER
99
        );
100
    }
101
}
102