Passed
Push — master ( 401ce9...01f538 )
by Igor
01:14 queued 10s
created

TransportFactory::createTransport()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 23
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 5

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 5
eloc 14
c 1
b 1
f 0
nc 5
nop 2
dl 0
loc 23
ccs 14
cts 14
cp 1
crap 5
rs 9.4888
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;
12
13
use GuzzleHttp\Client;
14
use Psr\Log\LoggerInterface;
15
use Strider2038\JsonRpcClient\Configuration\GeneralOptions;
16
use Strider2038\JsonRpcClient\Exception\InvalidConfigException;
17
use Strider2038\JsonRpcClient\Transport\Socket\SocketClient;
18
19
/**
20
 * @author Igor Lazarev <[email protected]>
21
 */
22
class TransportFactory
23
{
24
    /** @var LoggerInterface|null */
25
    private $logger;
26
27 11
    public function __construct(LoggerInterface $logger = null)
28
    {
29 11
        $this->logger = $logger;
30 11
    }
31
32
    /**
33
     * @throws InvalidConfigException
34
     */
35 11
    public function createTransport(string $connection, GeneralOptions $options): TransportInterface
36
    {
37 11
        $this->validateUrl($connection);
38 10
        $protocol = strtolower(parse_url($connection, PHP_URL_SCHEME));
39
40 10
        if ('tcp' === $protocol) {
41 4
            $transport = $this->createTcpTransport($connection, $options);
42 6
        } elseif ('http' === $protocol || 'https' === $protocol) {
43 4
            $transport = $this->createHttpTransport($connection, $options);
44
        } else {
45 2
            throw new InvalidConfigException(
46 2
                sprintf(
47 2
                    'Unsupported protocol: "%s". Supported protocols: "tcp", "http", "https".',
48 2
                    $protocol
49
                )
50
            );
51
        }
52
53 8
        if ($this->logger) {
54 2
            $transport = new TransportLoggingDecorator($transport, $this->logger);
55
        }
56
57 8
        return $transport;
58
    }
59
60
    /**
61
     * @throws InvalidConfigException
62
     */
63 11
    private function validateUrl(string $url): void
64
    {
65 11
        if (false === filter_var($url, FILTER_VALIDATE_URL)) {
66 1
            throw new InvalidConfigException(
67 1
                sprintf('Valid URL is expected for TCP/IP transport. Given value is "%s".', $url)
68
            );
69
        }
70 10
    }
71
72
    /**
73
     * @throws InvalidConfigException
74
     */
75 4
    private function createTcpTransport(string $connection, GeneralOptions $options): SocketTransport
76
    {
77 4
        $client = new SocketClient($connection, $options->getConnectionTimeoutUs(), $options->getRequestTimeoutUs());
78
79 4
        return new SocketTransport($client);
80
    }
81
82 4
    private function createHttpTransport(string $connection, GeneralOptions $options): GuzzleHttpTransport
83
    {
84 4
        $guzzle = new Client([
85 4
            'base_uri' => $connection,
86 4
            'timeout'  => (float) $options->getRequestTimeoutUs() / 1000000,
87
        ]);
88
89 4
        return new GuzzleHttpTransport($guzzle);
90
    }
91
}
92