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

ClientBuilder::enableResponseProcessing()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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