Passed
Branch master (203e41)
by Nate
02:00
created

RetrofitBuilder::createDefaultProxyFactory()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 69
Code Lines 51

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 51
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 69
ccs 51
cts 51
cp 1
rs 8.56
c 0
b 0
f 0
cc 6
eloc 51
nc 5
nop 0
crap 6

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