Test Failed
Pull Request — master (#8)
by Igor
13:17 queued 10:05
created

GeneralOptions   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 143
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 11
eloc 45
c 1
b 1
f 0
dl 0
loc 143
ccs 40
cts 40
cp 1
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A getConnectionOptions() 0 3 1
A getHttpClientType() 0 3 1
A isResponseProcessingEnabled() 0 3 1
A getTransportConfiguration() 0 3 1
A getSerializationOptions() 0 3 1
A validateHttpClient() 0 14 2
A createFromArray() 0 9 1
A __construct() 0 19 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
use Strider2038\JsonRpcClient\Response\ResponseObjectInterface;
15
use Strider2038\JsonRpcClient\Transport\Http\HttpTransportTypeInterface;
16
17
/**
18
 * @author Igor Lazarev <[email protected]>
19
 */
20
class GeneralOptions
21
{
22
    public const DEFAULT_REQUEST_TIMEOUT = 1000000;
23
24
    private const SUPPORTED_HTTP_CLIENTS = [
25
        HttpTransportTypeInterface::AUTODETECT,
26
        HttpTransportTypeInterface::GUZZLE,
27
        HttpTransportTypeInterface::SYMFONY,
28
    ];
29
30
    /**
31
     * Request timeout in microseconds.
32
     *
33
     * @var int
34
     */
35
    private $requestTimeoutUs;
36
37
    /**
38
     * Connection configuration.
39
     *
40
     * @var ConnectionOptions
41
     */
42
    private $connectionOptions;
43
44
    /**
45
     * If enabled then all responses will be processed and client will return response payload.
46
     * All responses for a batch request will be sorted accordingly to request order.
47
     * If server returns error response, then instance of @see ErrorResponseException will be thrown.
48
     *
49
     * If disabled then client will return @see ResponseObjectInterface for each request
50
     * or an array of @see ResponseObjectInterface for each batch request.
51
     *
52
     * @var bool
53
     */
54
    private $enableResponseProcessing;
55
56
    /**
57
     * Serialization configuration.
58
     *
59
     * @var SerializationOptions
60
     */
61
    private $serializationOptions;
62
63
    /**
64
     * Preferred HTTP client.
65
     *
66
     * @see HttpTransportTypeInterface for available options.
67 41
     *
68
     * @var string
69
     */
70
    private $httpClientType;
71
72
    /**
73
     * @var array
74 41
     */
75 1
    private $transportConfiguration;
76
77 40
    /**
78
     * @throws InvalidConfigException
79 39
     */
80 39
    public function __construct(
81 39
        int $requestTimeoutUs = self::DEFAULT_REQUEST_TIMEOUT,
82 39
        ConnectionOptions $connectionOptions = null,
83 39
        bool $enableResponseProcessing = true,
84 39
        SerializationOptions $serializationOptions = null,
85
        array $transportConfiguration = [],
86 37
        string $httpClient = HttpTransportTypeInterface::AUTODETECT
87
    ) {
88 37
        if ($requestTimeoutUs <= 0) {
89
            throw new InvalidConfigException('Request timeout must be greater than 0.');
90
        }
91 15
        $this->validateHttpClient($httpClient);
92
93 15
        $this->requestTimeoutUs = $requestTimeoutUs;
94
        $this->connectionOptions = $connectionOptions ?? new ConnectionOptions();
95
        $this->enableResponseProcessing = $enableResponseProcessing;
96 20
        $this->transportConfiguration = $transportConfiguration;
97
        $this->serializationOptions = $serializationOptions ?? new SerializationOptions();
98 20
        $this->httpClientType = $httpClient;
99
    }
100
101 24
    public function getRequestTimeoutUs(): int
102
    {
103 24
        return $this->requestTimeoutUs;
104
    }
105
106 20
    public function getConnectionOptions(): ConnectionOptions
107
    {
108 20
        return $this->connectionOptions;
109
    }
110
111
    public function isResponseProcessingEnabled(): bool
112
    {
113
        return $this->enableResponseProcessing;
114 27
    }
115
116 27
    public function getTransportConfiguration(): array
117 27
    {
118 27
        return $this->transportConfiguration;
119 27
    }
120 27
121 27
    public function getSerializationOptions(): SerializationOptions
122
    {
123
        return $this->serializationOptions;
124
    }
125
126
    public function getHttpClientType(): string
127
    {
128 40
        return $this->httpClientType;
129
    }
130 40
131 1
    /**
132 1
     * @throws InvalidConfigException
133 1
     */
134 1
    public static function createFromArray(array $options): self
135 1
    {
136 1
        return new self(
137 1
            $options['request_timeout_us'] ?? self::DEFAULT_REQUEST_TIMEOUT,
138
            ConnectionOptions::createFromArray($options['connection'] ?? []),
139 1
            $options['enable_response_processing'] ?? true,
140 1
            SerializationOptions::createFromArray($options['serialization'] ?? []),
141 1
            $options['transport_configuration'] ?? [],
142
            $options['http_client_type'] ?? HttpTransportTypeInterface::AUTODETECT
143
        );
144
    }
145
146
    /**
147 39
     * @throws InvalidConfigException
148
     */
149
    private function validateHttpClient(string $httpClient): void
150
    {
151
        if (!in_array($httpClient, self::SUPPORTED_HTTP_CLIENTS, true)) {
152
            throw new InvalidConfigException(
153
                sprintf(
154
                    'Invalid value "%s" for http client option. Must be one of: %s.',
155
                    $httpClient,
156
                    implode(
157
                        ', ',
158
                        array_map(
159
                            static function (string $s): string {
160
                                return '"'.$s.'"';
161
                            },
162
                            self::SUPPORTED_HTTP_CLIENTS
163
                        )
164
                    )
165
                )
166
            );
167
        }
168
    }
169
}
170