Completed
Pull Request — master (#7)
by Igor
07:53 queued 03:25
created

ClientBuilder::getClient()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
c 1
b 0
f 0
dl 0
loc 14
ccs 9
cts 9
cp 1
rs 9.9666
cc 2
nc 2
nop 0
crap 2
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 Ramsey\Uuid\Uuid;
14
use Strider2038\JsonRpcClient\Request\IdGeneratorInterface;
15
use Strider2038\JsonRpcClient\Request\RequestObjectFactory;
16
use Strider2038\JsonRpcClient\Request\SequentialIntegerIdGenerator;
17
use Strider2038\JsonRpcClient\Request\UuidGenerator;
18
use Strider2038\JsonRpcClient\Response\ExceptionalResponseValidator;
19
use Strider2038\JsonRpcClient\Response\NullResponseValidator;
20
use Strider2038\JsonRpcClient\Serialization\ContextGenerator;
21
use Strider2038\JsonRpcClient\Serialization\JsonObjectSerializer;
22
use Strider2038\JsonRpcClient\Serialization\MessageSerializerInterface;
23
use Strider2038\JsonRpcClient\Service\Caller;
24
use Strider2038\JsonRpcClient\Service\ProcessingClient;
25
use Strider2038\JsonRpcClient\Service\RawClient;
26
use Strider2038\JsonRpcClient\Transport\TransportInterface;
27
28
/**
29
 * @experimental API may be changed
30
 *
31
 * @author Igor Lazarev <[email protected]>
32
 */
33
class ClientBuilder
34
{
35
    /**
36
     * If enabled then ProcessingClient will be returned with response unpacking.
37
     * If disabled then RawClient will be returned, that will return ResponseObjectInterface
38
     * for each request.
39
     *
40
     * @var bool
41
     */
42
    private $enableResponseProcessing = true;
43
44
    /** @var TransportInterface */
45
    private $transport;
46
47
    /** @var MessageSerializerInterface */
48
    private $serializer;
49
50
    /** @var IdGeneratorInterface */
51
    private $idGenerator;
52
53
    /** @var string[] */
54
    private $resultTypesByMethods = [];
55
56
    /** @var string|null */
57
    private $errorType = null;
58
59 44
    public function __construct(TransportInterface $transport)
60
    {
61 44
        $this->transport = $transport;
62 44
        $this->serializer = new JsonObjectSerializer();
63
64 44
        if (class_exists(Uuid::class)) {
65 44
            $this->idGenerator = new UuidGenerator();
66
        } else {
67
            $this->idGenerator = new SequentialIntegerIdGenerator();
68
        }
69 44
    }
70
71 6
    public function setSerializer(MessageSerializerInterface $serializer): self
72
    {
73 6
        $this->serializer = $serializer;
74
75 6
        return $this;
76
    }
77
78 10
    public function setIdGenerator(IdGeneratorInterface $idGenerator): self
79
    {
80 10
        $this->idGenerator = $idGenerator;
81
82 10
        return $this;
83
    }
84
85
    /**
86
     * If response processing is disabled then LowLevelClient will be constructed.
87
     *
88
     * @return $this
89
     */
90 11
    public function disableResponseProcessing(): self
91
    {
92 11
        $this->enableResponseProcessing = false;
93
94 11
        return $this;
95
    }
96
97 24
    public function setResultTypesByMethods(array $resultTypesByMethods): self
98
    {
99 24
        $this->resultTypesByMethods = $resultTypesByMethods;
100
101 24
        return $this;
102
    }
103
104 24
    public function setErrorType(?string $errorType): self
105
    {
106 24
        $this->errorType = $errorType;
107
108 24
        return $this;
109
    }
110
111 44
    public function getClient(): ClientInterface
112
    {
113 44
        $requestObjectFactory = $this->createRequestObjectFactory();
114 44
        $contextGenerator = new ContextGenerator($this->resultTypesByMethods, $this->errorType);
115
116 44
        if ($this->enableResponseProcessing) {
117 33
            $caller = new Caller($this->serializer, $contextGenerator, $this->transport, new ExceptionalResponseValidator());
118 33
            $client = new ProcessingClient($requestObjectFactory, $caller);
119
        } else {
120 11
            $caller = new Caller($this->serializer, $contextGenerator, $this->transport, new NullResponseValidator());
121 11
            $client = new RawClient($requestObjectFactory, $caller);
122
        }
123
124 44
        return $client;
125
    }
126
127 44
    private function createRequestObjectFactory(): RequestObjectFactory
128
    {
129 44
        return new RequestObjectFactory($this->idGenerator);
130
    }
131
}
132