Passed
Push — master ( 401ce9...01f538 )
by Igor
01:14 queued 10s
created

ClientFactory::createCaller()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 3
c 1
b 0
f 1
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
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