Completed
Push — master ( a6feea...7247a2 )
by Stéphane
06:46
created

ReactFactory::buildHttpClient()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2
Metric Value
dl 0
loc 11
rs 9.4286
ccs 6
cts 6
cp 1
cc 2
eloc 7
nc 2
nop 2
crap 2
1
<?php
2
3
namespace Http\Adapter\React;
4
5
use React\EventLoop\LoopInterface;
6
use React\EventLoop\Factory as EventLoopFactory;
7
use React\Dns\Resolver\Resolver as DnsResolver;
8
use React\Dns\Resolver\Factory as DnsResolverFactory;
9
use React\HttpClient\Factory as HttpClientFactory;
10
use React\HttpClient\Client as HttpClient;
11
12
/**
13
 * Factory wrapper for React instances
14
 * @author Stéphane Hulard <[email protected]>
15
 */
16
class ReactFactory
17
{
18
    /**
19
     * Build a react Event Loop
20
     * @return LoopInterface
21
     */
22 108
    public static function buildEventLoop()
23
    {
24 108
        return EventLoopFactory::create();
25
    }
26
27
    /**
28
     * Build a React Dns Resolver
29
     * @param  LoopInterface $loop
30
     * @param  string        $dns
31
     * @return DnsResolver
32
     */
33 108
    public static function buildDnsResolver(
34
        LoopInterface $loop,
35
        $dns = '8.8.8.8'
36
    ) {
37 108
        $factory = new DnsResolverFactory();
38 108
        return $factory->createCached($dns, $loop);
39
    }
40
41
    /**
42
     * Build a React Http Client
43
     * @param  LoopInterface $loop
44
     * @param  Resolver      $dns
45
     * @return HttpClient
46
     */
47 108
    public static function buildHttpClient(
48
        LoopInterface $loop,
49
        DnsResolver $dns = null
50
    ) {
51 108
        if (null === $dns) {
52 108
            $dns = self::buildDnsResolver($loop);
53 108
        }
54
55 108
        $factory = new HttpClientFactory();
56 108
        return $factory->create($loop, $dns);
57
    }
58
}
59