MultiTransportFactory::createTransport()   A
last analyzed

Complexity

Conditions 6
Paths 5

Size

Total Lines 22
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 6

Importance

Changes 0
Metric Value
eloc 13
c 0
b 0
f 0
dl 0
loc 22
ccs 12
cts 12
cp 1
rs 9.2222
cc 6
nc 5
nop 2
crap 6
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 Psr\Log\LoggerInterface;
14
use Strider2038\JsonRpcClient\Configuration\GeneralOptions;
15
use Strider2038\JsonRpcClient\Exception\InvalidConfigException;
16
use Strider2038\JsonRpcClient\Transport\Http\HttpTransportFactory;
17
use Strider2038\JsonRpcClient\Transport\Socket\SocketClient;
18
use Strider2038\JsonRpcClient\Transport\Socket\SocketConnector;
19
20
/**
21
 * @author Igor Lazarev <[email protected]>
22
 */
23
class MultiTransportFactory implements TransportFactoryInterface
24
{
25
    private HttpTransportFactory $httpTransportFactory;
26
27
    private ?LoggerInterface $logger;
28
29
    public function __construct(LoggerInterface $logger = null)
30
    {
31 34
        $this->httpTransportFactory = new HttpTransportFactory();
32
        $this->logger = $logger;
33 34
    }
34 34
35 34
    /**
36
     * @throws InvalidConfigException
37
     */
38
    public function createTransport(string $url, GeneralOptions $options): TransportInterface
39
    {
40 34
        $protocol = $this->parseProtocol($url);
41
42 34
        if ('tcp' === $protocol || 'unix' === $protocol) {
43
            $transport = $this->createSocketTransport($url, $options);
44 34
        } elseif ('http' === $protocol || 'https' === $protocol) {
45 13
            $transport = $this->httpTransportFactory->createTransport($url, $options);
46 21
        } else {
47 19
            throw new InvalidConfigException(
48
                sprintf(
49 2
                    'Unsupported protocol: "%s". Supported protocols: "unix", "tcp", "http", "https".',
50 2
                    $protocol
51 2
                )
52 2
            );
53
        }
54
55
        if ($this->logger) {
56
            $transport = new TransportLoggingDecorator($transport, $this->logger);
57 32
        }
58 7
59
        return $transport;
60
    }
61 32
62
    /**
63
     * @throws InvalidConfigException
64
     */
65
    private function createSocketTransport(string $connection, GeneralOptions $options): SocketTransport
66
    {
67 13
        $connector = new SocketConnector();
68
        $client = new SocketClient($connector, $connection, $options->getConnectionOptions(), $options->getRequestTimeoutUs());
69 13
70 13
        return new SocketTransport($client);
71
    }
72 13
73
    private function parseProtocol(string $connection): string
74
    {
75 34
        $protocol = '';
76
77 34
        if (false !== preg_match('/^(.*):/U', $connection, $matches)) {
78
            $protocol = strtolower($matches[1] ?? '');
79 34
        }
80 34
81
        return $protocol;
82
    }
83
}
84