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

MultiTransportFactory::createSocketTransport()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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