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 Strider2038\JsonRpcClient\Bridge\Symfony\DependencyInjection\Factory\SerializerFactory; |
15
|
|
|
use Strider2038\JsonRpcClient\Bridge\Symfony\Serialization\SymfonySerializerAdapter; |
16
|
|
|
use Strider2038\JsonRpcClient\Configuration\GeneralOptions; |
17
|
|
|
use Strider2038\JsonRpcClient\Configuration\SerializationOptions; |
18
|
|
|
use Strider2038\JsonRpcClient\Serialization\JsonArraySerializer; |
19
|
|
|
use Strider2038\JsonRpcClient\Serialization\JsonObjectSerializer; |
20
|
|
|
use Strider2038\JsonRpcClient\Serialization\MessageSerializerInterface; |
21
|
|
|
use Strider2038\JsonRpcClient\Transport\MultiTransportFactory; |
22
|
|
|
use Strider2038\JsonRpcClient\Transport\TransportFactoryInterface; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @experimental API may be changed |
26
|
|
|
* |
27
|
|
|
* @author Igor Lazarev <[email protected]> |
28
|
|
|
*/ |
29
|
|
|
class ClientFactory implements ClientFactoryInterface |
30
|
|
|
{ |
31
|
|
|
private TransportFactoryInterface $transportFactory; |
32
|
|
|
|
33
|
|
|
public function __construct(LoggerInterface $logger = null) |
34
|
28 |
|
{ |
35
|
|
|
$this->transportFactory = new MultiTransportFactory($logger); |
36
|
28 |
|
} |
37
|
28 |
|
|
38
|
|
|
public function createClient(string $url, array $options = []): ClientInterface |
39
|
28 |
|
{ |
40
|
|
|
$generalOptions = GeneralOptions::createFromArray($options); |
41
|
28 |
|
$transport = $this->transportFactory->createTransport($url, $generalOptions); |
42
|
28 |
|
$clientBuilder = new ClientBuilder($transport); |
43
|
27 |
|
|
44
|
|
|
$serializationOptions = $generalOptions->getSerializationOptions(); |
45
|
27 |
|
|
46
|
|
|
$clientBuilder->setResultTypesByMethods($serializationOptions->getResultTypesByMethods()); |
47
|
27 |
|
$clientBuilder->setDefaultErrorType($serializationOptions->getDefaultErrorType()); |
48
|
27 |
|
|
49
|
|
|
$serializerType = $serializationOptions->getSerializerType(); |
50
|
27 |
|
$serializer = $this->createSerializer($serializerType); |
51
|
27 |
|
$clientBuilder->setSerializer($serializer); |
52
|
27 |
|
|
53
|
|
|
if ($generalOptions->isResponseProcessingEnabled()) { |
54
|
27 |
|
$clientBuilder->enableResponseProcessing(); |
55
|
26 |
|
} else { |
56
|
|
|
$clientBuilder->disableResponseProcessing(); |
57
|
1 |
|
} |
58
|
|
|
|
59
|
|
|
return $clientBuilder->getClient(); |
60
|
27 |
|
} |
61
|
|
|
|
62
|
|
|
protected function createSerializer(string $serializerType): MessageSerializerInterface |
63
|
22 |
|
{ |
64
|
|
|
if (SerializationOptions::OBJECT_SERIALIZER === $serializerType) { |
65
|
22 |
|
$serializer = new JsonObjectSerializer(); |
66
|
20 |
|
} elseif (SerializationOptions::SYMFONY_SERIALIZER === $serializerType) { |
67
|
2 |
|
$serializer = new SymfonySerializerAdapter(SerializerFactory::createSerializer()); |
68
|
1 |
|
} else { |
69
|
|
|
$serializer = new JsonArraySerializer(); |
70
|
1 |
|
} |
71
|
|
|
|
72
|
|
|
return $serializer; |
73
|
22 |
|
} |
74
|
|
|
} |
75
|
|
|
|