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

ApiServiceBuilder::withDenormalizer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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