Completed
Push — master ( 841bb5...43701d )
by Nate
09:20
created

RestAdapterBuilder::build()   C

Complexity

Conditions 8
Paths 65

Size

Total Lines 41
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

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