Completed
Pull Request — master (#7)
by Igor
07:53 queued 03:25
created

HttpTransportFactory   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Test Coverage

Coverage 68.28%

Importance

Changes 3
Bugs 1 Features 0
Metric Value
wmc 11
eloc 41
c 3
b 1
f 0
dl 0
loc 94
ccs 28
cts 41
cp 0.6828
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A createSymfonyTransport() 0 30 3
A detectHttpClientType() 0 17 4
A createGuzzleTransport() 0 19 2
A createTransport() 0 11 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\Transport\Http;
12
13
use GuzzleHttp\Client;
14
use Strider2038\JsonRpcClient\Configuration\GeneralOptions;
15
use Strider2038\JsonRpcClient\Exception\InvalidConfigException;
16
use Strider2038\JsonRpcClient\Transport\TransportFactoryInterface;
17
use Strider2038\JsonRpcClient\Transport\TransportInterface;
18
use Symfony\Component\HttpClient\HttpClient;
19
20
/**
21
 * @internal
22
 *
23
 * @author Igor Lazarev <[email protected]>
24
 */
25
class HttpTransportFactory implements TransportFactoryInterface
26
{
27 17
    public function createTransport(string $connection, GeneralOptions $options): TransportInterface
28
    {
29 17
        $client = $this->detectHttpClientType($options);
30
31 17
        if (HttpTransportTypeInterface::SYMFONY === $client) {
32 11
            $transport = $this->createSymfonyTransport($connection, $options);
33
        } else {
34 6
            $transport = $this->createGuzzleTransport($connection, $options);
35
        }
36
37 17
        return $transport;
38
    }
39
40
    /**
41
     * @throws InvalidConfigException
42
     */
43 17
    private function detectHttpClientType(GeneralOptions $options): string
44
    {
45 17
        $client = $options->getHttpClient();
46
47 17
        if (HttpTransportTypeInterface::AUTODETECT === $client) {
48 5
            if (class_exists(HttpClient::class)) {
49 5
                $client = HttpTransportTypeInterface::SYMFONY;
50
            } elseif (class_exists(Client::class)) {
51
                $client = HttpTransportTypeInterface::GUZZLE;
52
            } else {
53
                throw new InvalidConfigException(
54
                    'Cannot create HTTP client: one of packages "symfony/http-client" or "guzzlehttp/guzzle" is required.'
55
                );
56
            }
57
        }
58
59 17
        return $client;
60
    }
61
62
    /**
63
     * @throws InvalidConfigException
64
     */
65 11
    private function createSymfonyTransport(string $connection, GeneralOptions $options): SymfonyTransport
66
    {
67 11
        if (!class_exists(HttpClient::class)) {
68
            throw new InvalidConfigException(
69
                'Cannot create Symfony HTTP client: package "symfony/http-client" is required.'
70
            );
71
        }
72
73 11
        if (method_exists(HttpClient::class, 'createForBaseUri')) {
74 11
            $config = array_merge(
75 11
                $options->getTransportConfiguration(),
76
                [
77 11
                    'timeout' => (float) $options->getRequestTimeoutUs() / 1000000,
78
                ]
79
            );
80
81 11
            $client = HttpClient::createForBaseUri($connection, $config);
82
        } else {
83
            $config = array_merge(
84
                $options->getTransportConfiguration(),
85
                [
86
                    'base_uri' => $connection,
87
                    'timeout'  => (float) $options->getRequestTimeoutUs() / 1000000,
88
                ]
89
            );
90
91
            $client = HttpClient::create($config);
92
        }
93
94 11
        return new SymfonyTransport($client);
95
    }
96
97
    /**
98
     * @throws InvalidConfigException
99
     */
100 6
    private function createGuzzleTransport(string $connection, GeneralOptions $options): GuzzleTransport
101
    {
102 6
        if (!class_exists(Client::class)) {
103
            throw new InvalidConfigException(
104
                'Cannot create Guzzle HTTP client: package "guzzlehttp/guzzle" is required.'
105
            );
106
        }
107
108 6
        $config = array_merge(
109 6
            $options->getTransportConfiguration(),
110
            [
111 6
                'base_uri' => $connection,
112 6
                'timeout'  => (float) $options->getRequestTimeoutUs() / 1000000,
113
            ]
114
        );
115
116 6
        $client = new Client($config);
117
118 6
        return new GuzzleTransport($client);
119
    }
120
}
121