ClientBuilder::setErrorTypesByMethods()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
ccs 1
cts 1
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 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
    private bool $enableResponseProcessing = true;
41
42
    private TransportInterface $transport;
43
44
    private MessageSerializerInterface $serializer;
45
46
    private IdGeneratorInterface $idGenerator;
47
48
    /** @var string[] */
49
    private array $resultTypesByMethods = [];
50
51
    private ?string $defaultErrorType = null;
52
53
    /** @var string[] */
54
    private array $errorTypesByMethods = [];
55
56
    public function __construct(TransportInterface $transport)
57
    {
58
        $this->transport = $transport;
59
        $this->serializer = new JsonObjectSerializer();
60
61
        if (class_exists(Uuid::class)) {
62 51
            $this->idGenerator = new UuidGenerator();
63
        } else {
64 51
            $this->idGenerator = new SequentialIntegerIdGenerator();
65 51
        }
66
    }
67 51
68 51
    public function setSerializer(MessageSerializerInterface $serializer): self
69
    {
70
        $this->serializer = $serializer;
71
72 51
        return $this;
73
    }
74 32
75
    public function setIdGenerator(IdGeneratorInterface $idGenerator): self
76 32
    {
77
        $this->idGenerator = $idGenerator;
78 32
79
        return $this;
80
    }
81 11
82
    /**
83 11
     * If response processing is enabled then ProcessingClient will be constructed.
84
     *
85 11
     * @return $this
86
     */
87
    public function enableResponseProcessing(): self
88
    {
89
        $this->enableResponseProcessing = true;
90
91
        return $this;
92
    }
93 26
94
    /**
95 26
     * If response processing is disabled then RawClient will be constructed.
96
     *
97 26
     * @return $this
98
     */
99
    public function disableResponseProcessing(): self
100
    {
101
        $this->enableResponseProcessing = false;
102
103
        return $this;
104
    }
105 13
106
    public function setResultTypesByMethods(array $resultTypesByMethods): self
107 13
    {
108
        $this->resultTypesByMethods = $resultTypesByMethods;
109 13
110
        return $this;
111
    }
112 31
113
    public function setDefaultErrorType(?string $defaultErrorType): self
114 31
    {
115
        $this->defaultErrorType = $defaultErrorType;
116 31
117
        return $this;
118
    }
119 31
120
    public function setErrorTypesByMethods(array $errorTypesByMethods): void
121 31
    {
122
        $this->errorTypesByMethods = $errorTypesByMethods;
123 31
    }
124
125
    public function getClient(): ClientInterface
126 4
    {
127
        $requestObjectFactory = $this->createRequestObjectFactory();
128 4
        $contextGenerator = new ContextGenerator($this->resultTypesByMethods, $this->defaultErrorType, $this->errorTypesByMethods);
129 4
130
        if ($this->enableResponseProcessing) {
131 51
            $caller = new Caller($this->serializer, $contextGenerator, $this->transport, new ExceptionalResponseValidator());
132
            $client = new ProcessingClient($requestObjectFactory, $caller);
133 51
        } else {
134 51
            $caller = new Caller($this->serializer, $contextGenerator, $this->transport, new NullResponseValidator());
135
            $client = new RawClient($requestObjectFactory, $caller);
136 51
        }
137 38
138 38
        return $client;
139
    }
140 13
141 13
    private function createRequestObjectFactory(): RequestObjectFactory
142
    {
143
        return new RequestObjectFactory($this->idGenerator);
144 51
    }
145
}
146