Completed
Pull Request — master (#13)
by
unknown
02:14
created

ApiServiceBuilder::build()   B

Complexity

Conditions 9
Paths 160

Size

Total Lines 56
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 34
CRAP Score 9

Importance

Changes 0
Metric Value
cc 9
eloc 33
nc 160
nop 1
dl 0
loc 56
ccs 34
cts 34
cp 1
crap 9
rs 7.5555
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace ElevenLabs\Api\Service;
6
7
use ElevenLabs\Api\Decoder\Adapter\SymfonyDecoderAdapter;
8
use ElevenLabs\Api\Factory\CachedSchemaFactoryDecorator;
9
use ElevenLabs\Api\Factory\SwaggerSchemaFactory;
10
use ElevenLabs\Api\Schema;
11
use ElevenLabs\Api\Service\Denormalizer\ResourceDenormalizer;
12
use ElevenLabs\Api\Service\Pagination\Provider\PaginationProviderInterface;
13
use ElevenLabs\Api\Validator\MessageValidator;
14
use Http\Client\HttpClient;
15
use Http\Discovery\HttpClientDiscovery;
16
use Http\Discovery\MessageFactoryDiscovery;
17
use Http\Discovery\UriFactoryDiscovery;
18
use Http\Message\MessageFactory;
19
use Http\Message\UriFactory;
20
use JsonSchema\Validator;
21
use Psr\Cache\CacheItemPoolInterface;
22
use Rize\UriTemplate;
23
use Symfony\Component\Serializer\Encoder\ChainDecoder;
24
use Symfony\Component\Serializer\Encoder\EncoderInterface;
25
use Symfony\Component\Serializer\Encoder\JsonEncoder;
26
use Symfony\Component\Serializer\Encoder\XmlEncoder;
27
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
28
use Symfony\Component\Serializer\Serializer;
29
use Symfony\Component\Serializer\SerializerInterface;
30
31
/**
32
 * Builder for ApiService instances
33
 *
34
 * Class ApiServiceBuilder.
35
 */
36
class ApiServiceBuilder
37
{
38
    /**
39
     * @var HttpClient
40
     */
41
    private $httpClient;
42
43
    /**
44
     * @var MessageFactory
45
     */
46
    private $messageFactory;
47
48
    /**
49
     * @var UriFactory
50
     */
51
    private $uriFactory;
52
53
    /**
54
     * @var SerializerInterface
55
     */
56
    private $serializer;
57
58
    /**
59
     * @var array
60
     */
61
    private $denormalizers = [];
62
63
    /**
64
     * @var array
65
     */
66
    private $encoders = [];
67
68
    /**
69
     * @var Schema
70
     */
71
    private $schema;
72
73
    /**
74
     * @var CacheItemPoolInterface
75
     */
76
    private $cache;
77
78
    /**
79
     * @var array
80
     */
81
    private $config = [];
82
83
    /**
84
     * @var PaginationProviderInterface|null
85
     */
86
    private $paginationProvider = null;
87
88
    /**
89
     * @var MessageValidator
90
     */
91
    private $requestValidator;
92
93
    /**
94
     * @return ApiServiceBuilder
95
     */
96 7
    public static function create(): ApiServiceBuilder
97
    {
98 7
        return new static();
99
    }
100
101
    /**
102
     * @param CacheItemPoolInterface $cache
103
     *
104
     * @return $this
105
     */
106 1
    public function withCacheProvider(CacheItemPoolInterface $cache): self
107
    {
108 1
        $this->cache = $cache;
109
110 1
        return $this;
111
    }
112
113
    /**
114
     * @param HttpClient $httpClient
115
     *
116
     * @return $this
117
     */
118 6
    public function withHttpClient(HttpClient $httpClient): self
119
    {
120 6
        $this->httpClient = $httpClient;
121
122 6
        return $this;
123
    }
124
125
    /**
126
     * @param MessageFactory $messageFactory
127
     *
128
     * @return $this
129
     */
130 3
    public function withMessageFactory(MessageFactory $messageFactory): self
131
    {
132 3
        $this->messageFactory = $messageFactory;
133
134 3
        return $this;
135
    }
136
137
    /**
138
     * @param UriFactory $uriFactory
139
     *
140
     * @return $this
141
     */
142
    public function withUriFactory(UriFactory $uriFactory): self
143
    {
144
        $this->uriFactory = $uriFactory;
145
146
        return $this;
147
    }
148
149
    /**
150
     * @param SerializerInterface $serializer
151
     *
152
     * @return $this
153
     */
154
    public function withSerializer(SerializerInterface $serializer): self
155
    {
156
        $this->serializer = $serializer;
157
158
        return $this;
159
    }
160
161
    /**
162
     * @param EncoderInterface $encoder
163
     *
164
     * @return $this
165
     */
166
    public function withEncoder(EncoderInterface $encoder): self
167
    {
168
        $this->encoders[] = $encoder;
169
170
        return $this;
171
    }
172
173
    /**
174
     * @param NormalizerInterface $normalizer
175
     *
176
     * @return $this
177
     */
178
    public function withDenormalizer(NormalizerInterface $normalizer): self
179
    {
180
        $this->denormalizers[] = $normalizer;
181
182
        return $this;
183
    }
184
185
    /**
186
     * @param PaginationProviderInterface $paginationProvider
187
     *
188
     * @return $this
189
     */
190 1
    public function withPaginationProvider(PaginationProviderInterface $paginationProvider): self
191
    {
192 1
        $this->paginationProvider = $paginationProvider;
193
194 1
        return $this;
195
    }
196
197
    /**
198
     * @param string $baseUri
199
     *
200
     * @return $this
201
     */
202 3
    public function withBaseUri(string $baseUri): self
203
    {
204 3
        $this->config['baseUri'] = $baseUri;
205
206 3
        return $this;
207
    }
208
209
    /**
210
     * @return $this
211
     */
212 1
    public function disableRequestValidation(): self
213
    {
214 1
        $this->config['validateRequest'] = false;
215
216 1
        return $this;
217
    }
218
219
    /**
220
     * @return $this
221
     */
222 2
    public function enableResponseValidation(): self
223
    {
224 2
        $this->config['validateResponse'] = true;
225
226 2
        return $this;
227
    }
228
229
    /**
230
     * @return $this
231
     */
232
    public function returnResponse(): self
233
    {
234
        $this->config['returnResponse'] = true;
235
236
        return $this;
237
    }
238
239
    /**
240
     * @param string $schemaPath
241
     *
242
     * @throws \Assert\AssertionFailedException
243
     *
244
     * @return ApiService
245
     */
246 9
    public function build(string $schemaPath): ApiService
247
    {
248
        // Build serializer
249 9
        if (null === $this->serializer) {
250 9
            if (empty($this->encoders)) {
251 9
                $this->encoders = [new JsonEncoder(), new XmlEncoder()];
252
            }
253
254 9
            if (empty($this->denormalizers)) {
255 9
                $this->denormalizers[] = new ResourceDenormalizer($this->paginationProvider);
256
            }
257
258 9
            $this->serializer = new Serializer(
259 9
                $this->denormalizers,
260 9
                $this->encoders
261
            );
262
        }
263
264 9
        if (null === $this->uriFactory) {
265 9
            $this->uriFactory = UriFactoryDiscovery::find();
266
        }
267
268 9
        if (null === $this->messageFactory) {
269 6
            $this->messageFactory = MessageFactoryDiscovery::find();
270
        }
271
272 9
        if (null === $this->httpClient) {
273 3
            $this->httpClient = HttpClientDiscovery::find();
274
        }
275
276 9
        $schemaFactory = new SwaggerSchemaFactory();
277 9
        if (null !== $this->cache) {
278 1
            $schemaFactory = new CachedSchemaFactoryDecorator(
279 1
                $this->cache,
280 1
                $schemaFactory
281
            );
282
        }
283
284 9
        $this->schema = $schemaFactory->createSchema($schemaPath);
285
286 9
        if (!isset($this->requestValidator)) {
287 9
            $this->requestValidator = new MessageValidator(
288 9
                new Validator(),
289 9
                new SymfonyDecoderAdapter(new ChainDecoder($this->encoders))
290
            );
291
        }
292
293 9
        return new ApiService(
294 9
            $this->uriFactory,
295 9
            new UriTemplate(),
296 9
            $this->httpClient,
297 9
            $this->messageFactory,
298 9
            $this->schema,
299 9
            $this->requestValidator,
300 9
            $this->serializer,
301 9
            $this->config
302
        );
303
    }
304
}
305