Completed
Pull Request — master (#7)
by Igor
04:25
created

GeneralOptions::getSerializationOptions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
ccs 0
cts 0
cp 0
cc 1
nc 1
nop 0
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\Transport\Http\HttpTransportTypeInterface;
15
16
/**
17
 * @author Igor Lazarev <[email protected]>
18
 */
19
class GeneralOptions
20
{
21
    public const DEFAULT_REQUEST_TIMEOUT = 1000000;
22
23
    private const SUPPORTED_HTTP_CLIENTS = [
24
        HttpTransportTypeInterface::AUTODETECT,
25
        HttpTransportTypeInterface::GUZZLE,
26
        HttpTransportTypeInterface::SYMFONY,
27
    ];
28
29
    /**
30
     * Request timeout in microseconds.
31
     *
32
     * @var int
33
     */
34
    private $requestTimeoutUs;
35
36
    /**
37
     * Connection configuration.
38
     *
39
     * @var ConnectionOptions
40
     */
41
    private $connectionOptions;
42
43
    /**
44
     * Serialization configuration.
45
     *
46
     * @var SerializationOptions
47
     */
48
    private $serializationOptions;
49
50 26
    /**
51
     * Preferred HTTP client.
52
     *
53
     * @see HttpTransportTypeInterface for available options.
54
     *
55
     * @var string
56 26
     */
57 1
    private $httpClient;
58
59 25
    /**
60 1
     * @var array
61
     */
62
    private $transportConfiguration;
63 24
64 24
    /**
65 24
     * @throws InvalidConfigException
66 24
     */
67 24
    public function __construct(
68
        int $requestTimeoutUs = self::DEFAULT_REQUEST_TIMEOUT,
69 22
        ConnectionOptions $connectionOptions = null,
70
        SerializationOptions $serializationOptions = null,
71 22
        array $transportConfiguration = [],
72
        string $httpClient = HttpTransportTypeInterface::AUTODETECT
73
    ) {
74 12
        if ($requestTimeoutUs <= 0) {
75
            throw new InvalidConfigException('Request timeout must be greater than 0.');
76 12
        }
77
        $this->validateHttpClient($httpClient);
78
79 12
        $this->requestTimeoutUs = $requestTimeoutUs;
80
        $this->connectionOptions = $connectionOptions ?? new ConnectionOptions();
81 12
        $this->transportConfiguration = $transportConfiguration;
82
        $this->serializationOptions = $serializationOptions ?? new SerializationOptions();
83
        $this->httpClient = $httpClient;
84 17
    }
85
86 17
    public function getRequestTimeoutUs(): int
87
    {
88
        return $this->requestTimeoutUs;
89
    }
90
91
    public function getConnectionOptions(): ConnectionOptions
92 18
    {
93
        return $this->connectionOptions;
94 18
    }
95 18
96 18
    public function getTransportConfiguration(): array
97 18
    {
98 18
        return $this->transportConfiguration;
99
    }
100
101
    public function getSerializationOptions(): SerializationOptions
102
    {
103
        return $this->serializationOptions;
104
    }
105
106
    public function getHttpClient(): string
107
    {
108
        return $this->httpClient;
109
    }
110
111
    /**
112
     * @throws InvalidConfigException
113
     */
114
    public static function createFromArray(array $options): self
115
    {
116
        return new self(
117
            $options['request_timeout_us'] ?? self::DEFAULT_REQUEST_TIMEOUT,
118
            ConnectionOptions::createFromArray($options['connection'] ?? []),
119
            SerializationOptions::createFromArray($options['serialization'] ?? []),
120
            $options['transport_configuration'] ?? [],
121
            $options['http_client'] ?? HttpTransportTypeInterface::AUTODETECT
122
        );
123
    }
124
125
    /**
126
     * @throws InvalidConfigException
127
     */
128
    private function validateHttpClient(string $httpClient): void
129
    {
130
        if (!in_array($httpClient, self::SUPPORTED_HTTP_CLIENTS, true)) {
131
            throw new InvalidConfigException(
132
                sprintf(
133
                    'Invalid value "%s" for http client option. Must be one of: %s.',
134
                    $httpClient,
135
                    implode(
136
                        ', ',
137
                        array_map(
138
                            static function (string $s): string {
139
                                return '"'.$s.'"';
140
                            },
141
                            self::SUPPORTED_HTTP_CLIENTS
142
                        )
143
                    )
144
                )
145
            );
146
        }
147
    }
148
}
149