Passed
Push — master ( 461fcb...e43cf9 )
by Igor
03:06
created

GeneralOptions::validateHttpClient()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 11
c 0
b 0
f 0
dl 0
loc 14
ccs 12
cts 12
cp 1
rs 9.9
cc 2
nc 2
nop 1
crap 2
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
     *
68
     * @var string
69
     */
70
    private $httpClientType;
71
72
    /**
73
     * @var array
74
     */
75
    private $transportConfiguration;
76
77
    /**
78
     * @throws InvalidConfigException
79
     */
80 47
    public function __construct(
81
        int $requestTimeoutUs = self::DEFAULT_REQUEST_TIMEOUT,
82
        ConnectionOptions $connectionOptions = null,
83
        bool $enableResponseProcessing = true,
84
        SerializationOptions $serializationOptions = null,
85
        array $transportConfiguration = [],
86
        string $httpClient = HttpTransportTypeInterface::AUTODETECT
87
    ) {
88 47
        if ($requestTimeoutUs <= 0) {
89 1
            throw new InvalidConfigException('Request timeout must be greater than 0.');
90
        }
91 46
        $this->validateHttpClient($httpClient);
92
93 45
        $this->requestTimeoutUs = $requestTimeoutUs;
94 45
        $this->connectionOptions = $connectionOptions ?? new ConnectionOptions();
95 45
        $this->enableResponseProcessing = $enableResponseProcessing;
96 45
        $this->transportConfiguration = $transportConfiguration;
97 45
        $this->serializationOptions = $serializationOptions ?? new SerializationOptions();
98 45
        $this->httpClientType = $httpClient;
99 45
    }
100
101 43
    public function getRequestTimeoutUs(): int
102
    {
103 43
        return $this->requestTimeoutUs;
104
    }
105
106 16
    public function getConnectionOptions(): ConnectionOptions
107
    {
108 16
        return $this->connectionOptions;
109
    }
110
111 30
    public function isResponseProcessingEnabled(): bool
112
    {
113 30
        return $this->enableResponseProcessing;
114
    }
115
116 25
    public function getTransportConfiguration(): array
117
    {
118 25
        return $this->transportConfiguration;
119
    }
120
121 30
    public function getSerializationOptions(): SerializationOptions
122
    {
123 30
        return $this->serializationOptions;
124
    }
125
126 25
    public function getHttpClientType(): string
127
    {
128 25
        return $this->httpClientType;
129
    }
130
131
    /**
132
     * @throws InvalidConfigException
133
     */
134 33
    public static function createFromArray(array $options): self
135
    {
136 33
        return new self(
137 33
            $options['request_timeout_us'] ?? self::DEFAULT_REQUEST_TIMEOUT,
138 33
            ConnectionOptions::createFromArray($options['connection'] ?? []),
139 33
            $options['enable_response_processing'] ?? true,
140 33
            SerializationOptions::createFromArray($options['serialization'] ?? []),
141 33
            $options['transport_configuration'] ?? [],
142 33
            $options['http_client_type'] ?? HttpTransportTypeInterface::AUTODETECT
143
        );
144
    }
145
146
    /**
147
     * @throws InvalidConfigException
148
     */
149 46
    private function validateHttpClient(string $httpClient): void
150
    {
151 46
        if (!in_array($httpClient, self::SUPPORTED_HTTP_CLIENTS, true)) {
152 1
            throw new InvalidConfigException(
153 1
                sprintf(
154 1
                    'Invalid value "%s" for http client option. Must be one of: %s.',
155 1
                    $httpClient,
156 1
                    implode(
157 1
                        ', ',
158 1
                        array_map(
159
                            static function (string $s): string {
160 1
                                return '"'.$s.'"';
161 1
                            },
162 1
                            self::SUPPORTED_HTTP_CLIENTS
163
                        )
164
                    )
165
                )
166
            );
167
        }
168 45
    }
169
}
170