RetrofitBuilder::createDefaultProxyFactory()   B
last analyzed

Complexity

Conditions 7
Paths 6

Size

Total Lines 73

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 52
CRAP Score 7

Importance

Changes 0
Metric Value
dl 0
loc 73
ccs 52
cts 52
cp 1
rs 7.6557
c 0
b 0
f 0
cc 7
nc 6
nop 0
crap 7

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/*
3
 * Copyright (c) Nate Brunette.
4
 * Distributed under the MIT License (http://opensource.org/licenses/MIT)
5
 */
6
7
declare(strict_types=1);
8
9
namespace Tebru\Retrofit;
10
11
use Doctrine\Common\Annotations\AnnotationReader;
12
use LogicException;
13
use PhpParser\BuilderFactory;
14
use PhpParser\PrettyPrinter\Standard;
15
use Psr\SimpleCache\CacheInterface;
16
use Tebru\AnnotationReader\AnnotationReaderAdapter;
17
use Tebru\Retrofit\Annotation as Annot;
18
use Tebru\Retrofit\Finder\ServiceResolver;
19
use Tebru\Retrofit\Internal\AnnotationHandler as AnnotHandler;
20
use Tebru\Retrofit\Internal\AnnotationProcessor;
21
use Tebru\Retrofit\Internal\CacheProvider;
22
use Tebru\Retrofit\Internal\CallAdapter\CallAdapterProvider;
23
use Tebru\Retrofit\Internal\CallAdapter\DefaultCallAdapterFactory;
24
use Tebru\Retrofit\Internal\Converter\ConverterProvider;
25
use Tebru\Retrofit\Internal\Converter\DefaultConverterFactory;
26
use Tebru\Retrofit\Internal\DefaultProxyFactory;
27
use Tebru\Retrofit\Internal\Filesystem;
28
use Tebru\Retrofit\Internal\ServiceMethod\ServiceMethodFactory;
29
30
/**
31
 * Class RetrofitBuilder
32
 *
33
 * @author Nate Brunette <[email protected]>
34
 */
35
class RetrofitBuilder
36
{
37
    /**
38
     * A cache interface to be used in place of defaults
39
     *
40
     * If this is set, [@see RetrofitBuilder::$shouldCache] will be ignored for internal
41
     * caches, however, [@see RetrofitBuilder::$shouldCache] and
42
     * [@see RetrofitBuilder::$cacheDir] will still be used to cache proxy clients.
43
     *
44
     * @var CacheInterface
45
     */
46
    private $cache;
47
48
    /**
49
     * Directory to store generated proxy clients
50
     *
51
     * @var string
52
     */
53
    private $cacheDir;
54
55
    /**
56
     * A Retrofit http client used to make requests
57
     *
58
     * @var HttpClient
59
     */
60
    private $httpClient;
61
62
    /**
63
     * The service's base url
64
     *
65
     * @var string
66
     */
67
    private $baseUrl;
68
69
    /**
70
     * An array of factories used to create [@see CallAdapter]s
71
     *
72
     * @var CallAdapterFactory[]
73
     */
74
    private $callAdapterFactories = [];
75
76
    /**
77
     * An array of factories used to convert types
78
     *
79
     * @var ConverterFactory[]
80
     */
81
    private $converterFactories = [];
82
83
    /**
84
     * An array of factories used to create [@see Proxy] objects
85
     *
86
     * @var ProxyFactory[]
87
     */
88
    private $proxyFactories = [];
89
90
    /**
91
     * An array of handlers used to modify the request based on an annotation
92
     *
93
     * @var AnnotationHandler[]
94
     */
95
    private $annotationHandlers = [];
96
97
    /**
98
     * If we should cache the proxies
99
     *
100
     * @var bool
101
     */
102
    private $shouldCache = false;
103
104
    /**
105
     * Override default cache adapters
106
     *
107
     * @param CacheInterface $cache
108
     * @return RetrofitBuilder
109
     */
110 1
    public function setCache(CacheInterface $cache): RetrofitBuilder
111
    {
112 1
        $this->cache = $cache;
113
114 1
        return $this;
115
    }
116
117
    /**
118
     * Set the cache directory
119
     *
120
     * @param string $cacheDir
121
     * @return RetrofitBuilder
122
     */
123 1
    public function setCacheDir(string $cacheDir): RetrofitBuilder
124
    {
125 1
        $this->cacheDir = $cacheDir;
126
127 1
        return $this;
128
    }
129
130
    /**
131
     * Set the Retrofit http client
132
     *
133
     * @param HttpClient $client
134
     * @return RetrofitBuilder
135
     */
136 22
    public function setHttpClient(HttpClient $client): RetrofitBuilder
137
    {
138 22
        $this->httpClient = $client;
139
140 22
        return $this;
141
    }
142
143
    /**
144
     * Set the base url
145
     *
146
     * @param string $baseUrl
147
     * @return RetrofitBuilder
148
     */
149 22
    public function setBaseUrl(string $baseUrl): RetrofitBuilder
150
    {
151 22
        $this->baseUrl = $baseUrl;
152
153 22
        return $this;
154
    }
155
156
    /**
157
     * Add a [@see CallAdapterFactory]
158
     *
159
     * @param CallAdapterFactory $callAdapterFactory
160
     * @return RetrofitBuilder
161
     */
162 1
    public function addCallAdapterFactory(CallAdapterFactory $callAdapterFactory): RetrofitBuilder
163
    {
164 1
        $this->callAdapterFactories[] = $callAdapterFactory;
165
166 1
        return $this;
167
    }
168
169
    /**
170
     * Add a [@see ConverterFactory]
171
     *
172
     * @param ConverterFactory $converterFactory
173
     * @return RetrofitBuilder
174
     */
175 2
    public function addConverterFactory(ConverterFactory $converterFactory): RetrofitBuilder
176
    {
177 2
        $this->converterFactories[] = $converterFactory;
178
179 2
        return $this;
180
    }
181
182
    /**
183
     * Add a [@see ProxyFactory]
184
     *
185
     * @param ProxyFactory $proxyFactory
186
     * @return RetrofitBuilder
187
     */
188 1
    public function addProxyFactory(ProxyFactory $proxyFactory): RetrofitBuilder
189
    {
190 1
        $this->proxyFactories[] = $proxyFactory;
191
192 1
        return $this;
193
    }
194
195
    /**
196
     * Add an [@see AnnotationHandler]
197
     *
198
     * @param string $annotationName
199
     * @param AnnotationHandler $annotationHandler
200
     * @return RetrofitBuilder
201
     */
202 1
    public function addAnnotationHandler(string $annotationName, AnnotationHandler $annotationHandler): RetrofitBuilder
203
    {
204 1
        $this->annotationHandlers[$annotationName] = $annotationHandler;
205
206 1
        return $this;
207
    }
208
209
    /**
210
     * Enable caching proxies
211
     *
212
     * @param bool $enable
213
     * @return RetrofitBuilder
214
     */
215 2
    public function enableCache(bool $enable = true): RetrofitBuilder
216
    {
217 2
        $this->shouldCache = $enable;
218
219 2
        return $this;
220
    }
221
222
    /**
223
     * Build a retrofit instance
224
     *
225
     * @return Retrofit
226
     * @throws \LogicException
227
     */
228 21
    public function build(): Retrofit
229
    {
230 21
        $defaultProxyFactory = $this->createDefaultProxyFactory();
231 18
        foreach ($this->proxyFactories as $proxyFactory) {
232 1
            if ($proxyFactory instanceof DefaultProxyFactoryAware) {
233 1
                $proxyFactory->setDefaultProxyFactory($defaultProxyFactory);
234
            }
235
        }
236 18
        $this->proxyFactories[] = $defaultProxyFactory;
237
238 18
        return new Retrofit(new ServiceResolver(), $this->proxyFactories);
239
    }
240
241
    /**
242
     * Creates the default proxy factory and all necessary dependencies
243
     *
244
     * @return ProxyFactory
245
     * @throws \LogicException
246
     */
247 21
    private function createDefaultProxyFactory(): ProxyFactory
248
    {
249 21
        if ($this->baseUrl === null) {
250 1
            throw new LogicException('Retrofit: Base URL must be provided');
251
        }
252
253 20
        if ($this->httpClient === null) {
254 1
            throw new LogicException('Retrofit: Must set http client to make requests');
255
        }
256
257 19
        if ($this->shouldCache && $this->cacheDir === null) {
258 1
            throw new LogicException('Retrofit: If caching is enabled, must specify cache directory');
259
        }
260
261 18
        $this->cacheDir .= '/retrofit';
262
263
        // add defaults to any user registered
264 18
        $this->callAdapterFactories[] = new DefaultCallAdapterFactory();
265 18
        $this->converterFactories[] = new DefaultConverterFactory();
266
267 18
        if ($this->cache === null) {
268 17
            $this->cache = $this->shouldCache === true
269 1
                ? CacheProvider::createFileCache($this->cacheDir)
270 16
                : CacheProvider::createMemoryCache();
271
        }
272
273 18
        $httpRequestHandler = new AnnotHandler\HttpRequestAnnotHandler();
274
275
        /** @noinspection ClassConstantUsageCorrectnessInspection */
276 18
        $annotationHandlers = array_merge(
277
            [
278 18
                Annot\Body::class => new AnnotHandler\BodyAnnotHandler(),
279 18
                Annot\DELETE::class => $httpRequestHandler,
280 18
                Annot\Field::class => new AnnotHandler\FieldAnnotHandler(),
281 18
                Annot\FieldMap::class => new AnnotHandler\FieldMapAnnotHandler(),
282 18
                Annot\GET::class => $httpRequestHandler,
283 18
                Annot\HEAD::class => $httpRequestHandler,
284 18
                Annot\Header::class => new AnnotHandler\HeaderAnnotHandler(),
285 18
                Annot\HeaderMap::class => new AnnotHandler\HeaderMapAnnotHandler(),
286 18
                Annot\Headers::class => new AnnotHandler\HeadersAnnotHandler(),
287 18
                Annot\OPTIONS::class => $httpRequestHandler,
288 18
                Annot\Part::class => new AnnotHandler\PartAnnotHandler(),
289 18
                Annot\PartMap::class => new AnnotHandler\PartMapAnnotHandler(),
290 18
                Annot\PATCH::class => $httpRequestHandler,
291 18
                Annot\Path::class => new AnnotHandler\PathAnnotHandler(),
292 18
                Annot\POST::class => $httpRequestHandler,
293 18
                Annot\PUT::class => $httpRequestHandler,
294 18
                Annot\Query::class => new AnnotHandler\QueryAnnotHandler(),
295 18
                Annot\QueryMap::class => new AnnotHandler\QueryMapAnnotHandler(),
296 18
                Annot\QueryName::class => new AnnotHandler\QueryNameAnnotHandler(),
297 18
                Annot\REQUEST::class => $httpRequestHandler,
298 18
                Annot\Url::class => new AnnotHandler\UrlAnnotHandler(),
299
            ],
300 18
            $this->annotationHandlers
301
        );
302 18
        $serviceMethodFactory = new ServiceMethodFactory(
303 18
            new AnnotationProcessor($annotationHandlers),
304 18
            new CallAdapterProvider($this->callAdapterFactories),
305 18
            new ConverterProvider($this->converterFactories),
306 18
            new AnnotationReaderAdapter(new AnnotationReader(), $this->cache),
307 18
            $this->baseUrl
308
        );
309
310 18
        return new DefaultProxyFactory(
311 18
            new BuilderFactory(),
312 18
            new Standard(),
313 18
            $serviceMethodFactory,
314 18
            $this->httpClient,
315 18
            new Filesystem(),
316 18
            $this->shouldCache,
317 18
            $this->cacheDir
318
        );
319
    }
320
}
321