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; |
12
|
|
|
|
13
|
|
|
use Psr\Log\LoggerInterface; |
14
|
|
|
use Ramsey\Uuid\Uuid; |
15
|
|
|
use Strider2038\JsonRpcClient\Configuration\GeneralOptions; |
16
|
|
|
use Strider2038\JsonRpcClient\Request\RequestObjectFactory; |
17
|
|
|
use Strider2038\JsonRpcClient\Request\SequentialIntegerIdGenerator; |
18
|
|
|
use Strider2038\JsonRpcClient\Request\UuidGenerator; |
19
|
|
|
use Strider2038\JsonRpcClient\Response\ExceptionalResponseValidator; |
20
|
|
|
use Strider2038\JsonRpcClient\Serialization\JsonObjectSerializer; |
21
|
|
|
use Strider2038\JsonRpcClient\Service\Caller; |
22
|
|
|
use Strider2038\JsonRpcClient\Service\HighLevelClient; |
23
|
|
|
use Strider2038\JsonRpcClient\Transport\TransportFactory; |
24
|
|
|
use Strider2038\JsonRpcClient\Transport\TransportInterface; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @experimental API may be changed |
28
|
|
|
* |
29
|
|
|
* @author Igor Lazarev <[email protected]> |
30
|
|
|
*/ |
31
|
|
|
class ClientFactory |
32
|
|
|
{ |
33
|
|
|
/** @var TransportFactory */ |
34
|
|
|
private $transportFactory; |
35
|
|
|
|
36
|
5 |
|
public function __construct(LoggerInterface $logger = null) |
37
|
|
|
{ |
38
|
5 |
|
$this->transportFactory = new TransportFactory($logger); |
39
|
5 |
|
} |
40
|
|
|
|
41
|
5 |
|
public function createClient(string $connection, array $options = []): ClientInterface |
42
|
|
|
{ |
43
|
5 |
|
$requestObjectFactory = $this->createRequestObjectFactory(); |
44
|
5 |
|
$transport = $this->transportFactory->createTransport($connection, GeneralOptions::createFromArray($options)); |
45
|
4 |
|
$caller = $this->createCaller($transport); |
46
|
|
|
|
47
|
4 |
|
return new HighLevelClient($requestObjectFactory, $caller); |
48
|
|
|
} |
49
|
|
|
|
50
|
5 |
|
private function createRequestObjectFactory(): RequestObjectFactory |
51
|
|
|
{ |
52
|
5 |
|
if (class_exists(Uuid::class)) { |
53
|
5 |
|
$idGenerator = new UuidGenerator(); |
54
|
|
|
} else { |
55
|
|
|
$idGenerator = new SequentialIntegerIdGenerator(); |
56
|
|
|
} |
57
|
|
|
|
58
|
5 |
|
return new RequestObjectFactory($idGenerator); |
59
|
|
|
} |
60
|
|
|
|
61
|
4 |
|
private function createCaller(TransportInterface $transport): Caller |
62
|
|
|
{ |
63
|
4 |
|
$serializer = new JsonObjectSerializer(); |
64
|
4 |
|
$validator = new ExceptionalResponseValidator(); |
65
|
|
|
|
66
|
4 |
|
return new Caller($serializer, $transport, $validator); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|