Completed
Push — master ( 4cc180...eceda3 )
by Nate
03:04
created

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