Completed
Push — master ( 00c305...784882 )
by Nate
03:32
created

RestAdapterBuilder::setSerializerAdapter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
/*
3
 * Copyright (c) Nate Brunette.
4
 * Distributed under the MIT License (http://opensource.org/licenses/MIT)
5
 */
6
7
namespace Tebru\Retrofit\Adapter\Rest;
8
9
use JMS\Serializer\Serializer;
10
use JMS\Serializer\SerializerBuilder;
11
use Psr\Log\LoggerInterface;
12
use Psr\Log\NullLogger;
13
use Symfony\Component\EventDispatcher\EventDispatcher;
14
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
15
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
16
use Tebru;
17
use Tebru\Retrofit\Adapter\DeserializerAdapter;
18
use Tebru\Retrofit\Adapter\HttpClientAdapter;
19
use Tebru\Retrofit\Adapter\SerializerAdapter;
20
use Tebru\Retrofit\Event\EventDispatcherAware;
21
use Tebru\Retrofit\Exception\RetrofitException;
22
use Tebru\Retrofit\HttpClient\ClientProvider;
23
use Tebru\Retrofit\Subscriber\LogSubscriber;
24
use Tebru\RetrofitSerializer\Adapter\JmsSerializerAdapter;
25
26
/**
27
 * Class RestAdapterBuilder
28
 *
29
 * Builds a rest adapter
30
 *
31
 * @author Nate Brunette <[email protected]>
32
 */
33
class RestAdapterBuilder
34
{
35
    /**
36
     * Gets the http client that's available
37
     *
38
     * @var ClientProvider
39
     */
40
    private $clientProvider;
41
42
    /**
43
     * Client base url
44
     *
45
     * @var string $baseUrl
46
     */
47
    private $baseUrl;
48
49
    /**
50
     * JMS Serializer
51
     *
52
     * @var Serializer $serializer
53
     */
54
    private $serializer;
55
56
    /**
57
     * Serializer adapter
58
     *
59
     * @var SerializerAdapter
60
     */
61
    private $serializerAdapter;
62
63
    /**
64
     * Deserializer adapter
65
     *
66
     * @var DeserializerAdapter
67
     */
68
    private $deserializerAdapter;
69
70
    /**
71
     * Symfony event dispatcher
72
     *
73
     * @var EventDispatcherInterface
74
     */
75
    private $eventDispatcher;
76
77
    /**
78
     * Array of event subscribers
79
     *
80
     * @var EventSubscriberInterface[]
81
     */
82
    private $subscribers = [];
83
84
    /**
85
     * Determine if we should use the default log subscriber
86
     */
87
    private $useLogSubscriber = true;
88
89
    /**
90
     * Psr logger
91
     *
92
     * @var LoggerInterface
93
     */
94
    private $logger;
95
96
    /**
97
     * Constructor
98
     *
99
     * @param ClientProvider $clientProvider
100
     */
101
    public function __construct(ClientProvider $clientProvider)
102
    {
103
        $this->clientProvider = $clientProvider;
104
    }
105
106
107
    /**
108
     * Sets the base url for the rest client
109
     *
110
     * @param string $baseUrl
111
     * @return $this
112
     */
113
    public function setBaseUrl($baseUrl)
114
    {
115
        $this->baseUrl = $baseUrl;
116
117
        return $this;
118
    }
119
120
    /**
121
     * Sets the http client used with rest client
122
     *
123
     * Currently only supports guzzle clients
124
     *
125
     * @deprecated Will be removed in v3. Use setClientAdapter() instead.
126
     * @param mixed $httpClient
127
     * @return $this
128
     * @throws RetrofitException
129
     */
130
    public function setHttpClient($httpClient)
131
    {
132
        trigger_error(
133
           'Retrofit Deprecation: Setting an http client is deprecated and will be removed
134
            in v3.  Use RestAdapterBuilder::setClientAdapter() instead.',
135
            E_USER_DEPRECATED
136
        );
137
138
        $this->clientProvider->setClient($httpClient);
139
140
        return $this;
141
    }
142
143
    /**
144
     * Set the http client adapter to make requests
145
     *
146
     * @param HttpClientAdapter $clientAdapter
147
     * @return $this
148
     * @throws RetrofitException
149
     */
150
    public function setClientAdapter(HttpClientAdapter $clientAdapter)
151
    {
152
        $this->clientProvider->setClient($clientAdapter);
153
154
        return $this;
155
    }
156
157
    /**
158
     * Set the serializer used with rest client
159
     *
160
     * @param Serializer $serializer
161
     * @return $this
162
     */
163
    public function setSerializer(Serializer $serializer)
164
    {
165
        trigger_error(
166
            'Retrofit Deprecation: Method RestAdapterBuilder::setSerializer is deprecated and will be removed in v3. 
167
            Use setSerializerAdapter and setDeserializerAdapter instead.',
168
            E_USER_DEPRECATED
169
        );
170
171
        $this->serializerAdapter = new JmsSerializerAdapter($serializer);
172
        $this->deserializerAdapter = $this->serializerAdapter;
173
174
        return $this;
175
    }
176
177
    /**
178
     * Set the serializer adapter
179
     *
180
     * @param SerializerAdapter $serializerAdapter
181
     * @return $this
182
     */
183
    public function setSerializerAdapter(SerializerAdapter $serializerAdapter)
184
    {
185
        $this->serializerAdapter = $serializerAdapter;
186
187
        return $this;
188
    }
189
190
    /**
191
     * Set the deserializer adapter
192
     *
193
     * @param DeserializerAdapter $deserializerAdapter
194
     * @return $this
195
     */
196
    public function setDeserializerAdapter(DeserializerAdapter $deserializerAdapter)
197
    {
198
        $this->deserializerAdapter = $deserializerAdapter;
199
200
        return $this;
201
    }
202
203
    /**
204
     * Set the event dispatcher
205
     *
206
     * @param EventDispatcherInterface $eventDispatcher
207
     * @return RestAdapterBuilder
208
     */
209
    public function setEventDispatcher(EventDispatcherInterface $eventDispatcher)
210
    {
211
        $this->eventDispatcher = $eventDispatcher;
212
213
        return $this;
214
    }
215
216
    /**
217
     * Add a subscriber
218
     *
219
     * @param EventSubscriberInterface $subscriber
220
     * @return $this
221
     */
222
    public function addSubscriber(EventSubscriberInterface $subscriber)
223
    {
224
        $this->subscribers[] = $subscriber;
225
226
        return $this;
227
    }
228
229
    /**
230
     * Do not use the default log subscriber;
231
     *
232
     * @return $this
233
     */
234
    public function ignoreLogSubscriber()
235
    {
236
        $this->useLogSubscriber = false;
237
238
        return $this;
239
    }
240
241
    /**
242
     * @param LoggerInterface $logger
243
     * @return $this
244
     */
245
    public function setLogger(LoggerInterface $logger)
246
    {
247
        $this->logger = $logger;
248
249
        return $this;
250
    }
251
252
    /**
253
     * Build the rest adapter
254
     *
255
     * @return RestAdapter
256
     * @throws RetrofitException
257
     */
258
    public function build()
259
    {
260
        if (null === $this->baseUrl) {
261
            throw new RetrofitException('Could not build RestAdapter with null $baseUrl');
262
        }
263
264
        if (null === $this->serializer && null === $this->serializerAdapter) {
265
            $this->serializerAdapter = new JmsSerializerAdapter(SerializerBuilder::create()->build());
266
            $this->deserializerAdapter = $this->serializerAdapter;
267
        }
268
269
        if (null === $this->eventDispatcher) {
270
            $this->eventDispatcher = new EventDispatcher();
271
        }
272
273
        foreach ($this->subscribers as $subscriber) {
274
            $this->eventDispatcher->addSubscriber($subscriber);
275
        }
276
277
        if (null === $this->logger) {
278
            $this->logger = new NullLogger();
279
        }
280
281
        if ($this->useLogSubscriber) {
282
            $this->eventDispatcher->addSubscriber(new LogSubscriber($this->logger));
283
        }
284
285
        $client = $this->clientProvider->getClient();
286
287
        if ($client instanceof EventDispatcherAware) {
288
            $client->setEventDispatcher($this->eventDispatcher);
289
        }
290
291
        $adapter = new RestAdapter(
292
            $this->baseUrl,
293
            $client,
294
            $this->eventDispatcher,
295
            $this->serializerAdapter,
296
            $this->deserializerAdapter
297
        );
298
299
        return $adapter;
300
    }
301
}
302