Test Failed
Pull Request — master (#8)
by Igor
13:01
created

ClientFactory::createSerializer()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
eloc 7
c 0
b 0
f 0
dl 0
loc 11
ccs 0
cts 0
cp 0
rs 10
cc 3
nc 3
nop 1
crap 12
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
    /** @var TransportFactoryInterface */
32 22
    private $transportFactory;
33
34 22
    public function __construct(LoggerInterface $logger = null)
35 22
    {
36
        $this->transportFactory = new MultiTransportFactory($logger);
37
    }
38
39
    public function createClient(string $url, array $options = []): ClientInterface
40 22
    {
41
        $generalOptions = GeneralOptions::createFromArray($options);
42 22
        $transport = $this->transportFactory->createTransport($url, $generalOptions);
43 22
        $clientBuilder = new ClientBuilder($transport);
44 21
45
        $serializationOptions = $generalOptions->getSerializationOptions();
46 21
47
        $clientBuilder->setResultTypesByMethods($serializationOptions->getResultTypesByMethods());
48 21
        $clientBuilder->setDefaultErrorType($serializationOptions->getDefaultErrorType());
49 21
50
        $serializerType = $serializationOptions->getSerializerType();
51 21
        $serializer = $this->createSerializer($serializerType);
52
        $clientBuilder->setSerializer($serializer);
53 21
54 1
        if ($generalOptions->isResponseProcessingEnabled()) {
55 20
            $clientBuilder->enableResponseProcessing();
56 1
        } else {
57 1
            $clientBuilder->disableResponseProcessing();
58
        }
59
60 21
        return $clientBuilder->getClient();
61
    }
62
63
    protected function createSerializer(string $serializerType): MessageSerializerInterface
64
    {
65
        if (SerializationOptions::OBJECT_SERIALIZER === $serializerType) {
66
            $serializer = new JsonObjectSerializer();
67
        } elseif (SerializationOptions::SYMFONY_SERIALIZER === $serializerType) {
68
            $serializer = new SymfonySerializerAdapter(SerializerFactory::createSerializer());
69
        } else {
70
            $serializer = new JsonArraySerializer();
71
        }
72
73
        return $serializer;
74
    }
75
}
76