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\Http\GuzzleTransport; |
18
|
|
|
use Strider2038\JsonRpcClient\Transport\Socket\SocketClient; |
19
|
|
|
use Strider2038\JsonRpcClient\Transport\Socket\SocketConnector; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @author Igor Lazarev <[email protected]> |
23
|
|
|
*/ |
24
|
|
|
class TransportFactory |
25
|
|
|
{ |
26
|
|
|
/** @var LoggerInterface|null */ |
27
|
|
|
private $logger; |
28
|
|
|
|
29
|
20 |
|
public function __construct(LoggerInterface $logger = null) |
30
|
|
|
{ |
31
|
20 |
|
$this->logger = $logger; |
32
|
20 |
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @throws InvalidConfigException |
36
|
|
|
*/ |
37
|
20 |
|
public function createTransport(string $connection, GeneralOptions $options): TransportInterface |
38
|
|
|
{ |
39
|
20 |
|
$protocol = strtolower(parse_url($connection, PHP_URL_SCHEME)); |
40
|
|
|
|
41
|
20 |
|
if ('tcp' === $protocol) { |
42
|
9 |
|
$transport = $this->createTcpTransport($connection, $options); |
43
|
11 |
|
} elseif ('http' === $protocol || 'https' === $protocol) { |
44
|
9 |
|
$transport = $this->createHttpTransport($connection, $options); |
45
|
|
|
} else { |
46
|
2 |
|
throw new InvalidConfigException( |
47
|
2 |
|
sprintf( |
48
|
2 |
|
'Unsupported protocol: "%s". Supported protocols: "tcp", "http", "https".', |
49
|
2 |
|
$protocol |
50
|
|
|
) |
51
|
|
|
); |
52
|
|
|
} |
53
|
|
|
|
54
|
18 |
|
if ($this->logger) { |
55
|
2 |
|
$transport = new TransportLoggingDecorator($transport, $this->logger); |
56
|
|
|
} |
57
|
|
|
|
58
|
18 |
|
return $transport; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @throws InvalidConfigException |
63
|
|
|
*/ |
64
|
9 |
|
private function createTcpTransport(string $connection, GeneralOptions $options): SocketTransport |
65
|
|
|
{ |
66
|
9 |
|
$connector = new SocketConnector(); |
67
|
9 |
|
$client = new SocketClient($connector, $connection, $options->getConnectionOptions(), $options->getRequestTimeoutUs()); |
68
|
|
|
|
69
|
9 |
|
return new SocketTransport($client); |
70
|
|
|
} |
71
|
|
|
|
72
|
9 |
|
private function createHttpTransport(string $connection, GeneralOptions $options): GuzzleTransport |
73
|
|
|
{ |
74
|
9 |
|
$config = array_merge( |
75
|
9 |
|
$options->getTransportConfiguration(), |
76
|
|
|
[ |
77
|
9 |
|
'base_uri' => $connection, |
78
|
9 |
|
'timeout' => (float) $options->getRequestTimeoutUs() / 1000000, |
79
|
|
|
] |
80
|
|
|
); |
81
|
|
|
|
82
|
9 |
|
$guzzle = new Client($config); |
83
|
|
|
|
84
|
9 |
|
return new GuzzleTransport($guzzle); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|