Completed
Pull Request — master (#28)
by
unknown
09:56
created

ReactFactory   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 8
c 2
b 0
f 0
lcom 0
cbo 4
dl 0
loc 71
ccs 11
cts 11
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A buildEventLoop() 0 4 1
A buildDnsResolver() 0 8 1
A buildConnector() 0 10 2
A buildHttpClient() 0 15 4
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\Client as HttpClient;
10
use React\Socket\Connector;
11
use React\Socket\ConnectorInterface;
12
13
/**
14
 * Factory wrapper for React instances.
15
 *
16
 * @author Stéphane Hulard <[email protected]>
17
 */
18
class ReactFactory
19
{
20
    /**
21
     * Build a react Event Loop.
22
     *
23
     * @return LoopInterface
24 109
     */
25
    public static function buildEventLoop()
26 109
    {
27
        return EventLoopFactory::create();
28
    }
29
30
    /**
31
     * Build a React Dns Resolver.
32
     *
33
     * @param LoopInterface $loop
34
     * @param string        $dns
35
     *
36
     * @return DnsResolver
37 108
     */
38
    public static function buildDnsResolver(
39
        LoopInterface $loop,
40
        $dns = '8.8.8.8'
41 108
    ) {
42
        $factory = new DnsResolverFactory();
43 108
44
        return $factory->createCached($dns, $loop);
45
    }
46
47
    /**
48
     * @param LoopInterface    $loop
49
     * @param DnsResolver|null $dns
50
     *
51
     * @return ConnectorInterface
52
     */
53
    public static function buildConnector(
54 108
        LoopInterface $loop,
55
        DnsResolver $dns = null
56
    ) {
57
        if (null === $dns) {
58 108
            $dns = self::buildDnsResolver($loop);
59 108
        }
60 108
61
        return new Connector($loop, ['dns' => $dns]);
62 108
    }
63
64 108
    /**
65
     * Build a React Http Client.
66
     *
67
     * @param LoopInterface                  $loop
68
     * @param ConnectorInterface|DnsResolver $connector Passing a DnsResolver instance is deprecated. Should pass a
69
     *                                                  ConnectorInterface instance.
70
     *
71
     * @return HttpClient
72
     */
73
    public static function buildHttpClient(
74
        LoopInterface $loop,
75
        $connector = null
76
    ) {
77
        // build a connector if none is given, or create one attaching given DnsResolver (old deprecated behavior)
78
        if (null === $connector || $connector instanceof DnsResolver) {
79
            $connector = static::buildConnector($loop, $connector);
80
        }
81
82
        if (!$connector instanceof ConnectorInterface) {
83
            throw new \InvalidArgumentException('$connector must be an instance of ConnectorInterface or DnsResolver');
84
        }
85
86
        return new HttpClient($loop, $connector);
87
    }
88
}
89