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
|
|
|
|