Passed
Pull Request — master (#13)
by
unknown
02:44
created

ApiServiceBuilder::withPaginationProvider()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
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
 * 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 PaginationProviderInterface|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(): ApiServiceBuilder
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): self
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): self
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): self
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): self
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): self
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): self
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): self
177
    {
178
        $this->denormalizers[] = $normalizer;
179
180
        return $this;
181
    }
182
183
    /**
184
     * @param PaginationProviderInterface $paginationProvider
185
     *
186
     * @return $this
187
     */
188 1
    public function withPaginationProvider(PaginationProviderInterface $paginationProvider): self
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(string $baseUri): self
201
    {
202 3
        $this->config['baseUri'] = $baseUri;
203
204 3
        return $this;
205
    }
206
207
    /**
208
     * @return $this
209
     */
210 1
    public function disableRequestValidation(): self
211
    {
212 1
        $this->config['validateRequest'] = false;
213
214 1
        return $this;
215
    }
216
217
    /**
218
     * @return $this
219
     */
220 2
    public function enableResponseValidation(): self
221
    {
222 2
        $this->config['validateResponse'] = true;
223
224 2
        return $this;
225
    }
226
227
    /**
228
     * @return $this
229
     */
230
    public function returnResponse(): self
231
    {
232
        $this->config['returnResponse'] = true;
233
234
        return $this;
235
    }
236
237
    /**
238
     * @param string $schemaPath
239
     *
240
     * @throws \Assert\AssertionFailedException
241
     * @throws \Psr\Cache\InvalidArgumentException
242
     *
243
     * @return ApiService
244
     */
245 9
    public function build(string $schemaPath): ApiService
246
    {
247
        // Build serializer
248 9
        if (null === $this->serializer) {
249 9
            if (empty($this->encoders)) {
250 9
                $this->encoders = [new JsonEncoder(), new XmlEncoder()];
251
            }
252
253 9
            if (empty($this->denormalizers)) {
254 9
                $this->denormalizers[] = new ResourceDenormalizer($this->paginationProvider);
255
            }
256
257 9
            $this->serializer = new Serializer(
258 9
                $this->denormalizers,
259 9
                $this->encoders
260
            );
261
        }
262
263 9
        if (null === $this->uriFactory) {
264 9
            $this->uriFactory = UriFactoryDiscovery::find();
265
        }
266
267 9
        if (null === $this->messageFactory) {
268 6
            $this->messageFactory = MessageFactoryDiscovery::find();
269
        }
270
271 9
        if (null === $this->httpClient) {
272 3
            $this->httpClient = HttpClientDiscovery::find();
273
        }
274
275 9
        $schemaFactory = new SwaggerSchemaFactory();
276 9
        if (null !== $this->cache) {
277 1
            $schemaFactory = new CachedSchemaFactoryDecorator(
278 1
                $this->cache,
279 1
                $schemaFactory
280
            );
281
        }
282
283 9
        $this->schema = $schemaFactory->createSchema($schemaPath);
284
285 9
        if (!isset($this->requestValidator)) {
286 9
            $this->requestValidator = new MessageValidator(
287 9
                new Validator(),
288 9
                new SymfonyDecoderAdapter(new ChainDecoder($this->encoders))
289
            );
290
        }
291
292 9
        return new ApiService(
293 9
            $this->uriFactory,
294 9
            new UriTemplate(),
295 9
            $this->httpClient,
296 9
            $this->messageFactory,
297 9
            $this->schema,
298 9
            $this->requestValidator,
299 9
            $this->serializer,
300 9
            $this->config
301
        );
302
    }
303
}
304