Completed
Push — master ( 48251c...04d465 )
by David
04:13
created

ReactFactory::buildHttpClient05()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 26
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 26
ccs 0
cts 14
cp 0
rs 8.5806
cc 4
eloc 15
nc 4
nop 2
crap 20
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\HttpClient\Factory as HttpClientFactory;
11
use React\Socket\Connector;
12
use React\Socket\ConnectorInterface;
13
14
/**
15
 * Factory wrapper for React instances.
16
 *
17
 * @author Stéphane Hulard <[email protected]>
18
 */
19
class ReactFactory
20
{
21
    /**
22
     * Build a react Event Loop.
23
     *
24
     * @return LoopInterface
25
     */
26 109
    public static function buildEventLoop()
27
    {
28 109
        return EventLoopFactory::create();
29 1
    }
30
31
    /**
32
     * Build a React Dns Resolver.
33
     *
34
     * @param LoopInterface $loop
35
     * @param string        $dns
36
     *
37
     * @return DnsResolver
38
     */
39 109
    public static function buildDnsResolver(
40
        LoopInterface $loop,
41
        $dns = '8.8.8.8'
42
    ) {
43 109
        $factory = new DnsResolverFactory();
44
45 109
        return $factory->createCached($dns, $loop);
46
    }
47
48
    /**
49
     * @param LoopInterface    $loop
50
     * @param DnsResolver|null $dns
51
     *
52
     * @return ConnectorInterface
53
     */
54
    public static function buildConnector(
55
        LoopInterface $loop,
56
        DnsResolver $dns = null
57
    ) {
58
        return null !== $dns
59
            ? new Connector($loop, ['dns' => $dns])
60
            : new Connector($loop);
61
    }
62
63
    /**
64
     * Build a React Http Client.
65
     *
66
     * @param LoopInterface                       $loop
67
     * @param ConnectorInterface|DnsResolver|null $connector Only pass this argument if you need to customize DNS
68
     *                                                       behaviour. With react http client v0.5, pass a connector,
69
     *                                                       with v0.4 this must be a DnsResolver.
70
     *
71
     * @return HttpClient
72
     */
73 111
    public static function buildHttpClient(
74
        LoopInterface $loop,
75
        $connector = null
76
    ) {
77 111
        if (class_exists(HttpClientFactory::class)) {
78
            // if HttpClientFactory class exists, use old behavior for backwards compatibility
79 111
            return static::buildHttpClient04($loop, $connector);
0 ignored issues
show
Bug introduced by
It seems like $connector defined by parameter $connector on line 75 can also be of type object<React\Socket\ConnectorInterface>; however, Http\Adapter\React\React...ry::buildHttpClient04() does only seem to accept object<React\Dns\Resolver\Resolver>|null, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
80
        } else {
81
            return static::buildHttpClient05($loop, $connector);
82
        }
83
    }
84
85
    /**
86
     * Builds a React Http client v0.4 style.
87
     *
88
     * @param LoopInterface    $loop
89
     * @param DnsResolver|null $dns
90
     *
91
     * @return HttpClient
92
     */
93 111
    protected static function buildHttpClient04(
94
        LoopInterface $loop,
95
        $dns = null
96
    ) {
97
        // create dns resolver if one isn't provided
98 111
        if (null === $dns) {
99 109
            $dns = static::buildDnsResolver($loop);
100 109
        }
101
102
        // validate connector instance for proper error reporting
103 111
        if (!$dns instanceof DnsResolver) {
104 1
            throw new \InvalidArgumentException('For react http client v0.4, $dns must be an instance of DnsResolver');
105
        }
106
107 110
        $factory = new HttpClientFactory();
108
109 110
        return $factory->create($loop, $dns);
110
    }
111
112
    /**
113
     * Builds a React Http client v0.5 style.
114
     *
115
     * @param LoopInterface                       $loop
116
     * @param DnsResolver|ConnectorInterface|null $connector
117
     *
118
     * @return HttpClient
119
     */
120
    protected static function buildHttpClient05(
121
        LoopInterface $loop,
122
        $connector = null
123
    ) {
124
        // build a connector with given DnsResolver if provided (old deprecated behavior)
125
        if ($connector instanceof DnsResolver) {
126
            @trigger_error(
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
127
                sprintf(
128
                    'Passing a %s to buildHttpClient is deprecated since version 2.1.0 and will be removed in 3.0. If you need no specific behaviour, omit the $dns argument, otherwise pass a %s',
129
                    DnsResolver::class,
130
                    ConnectorInterface::class
131
                ),
132
                E_USER_DEPRECATED
133
            );
134
            $connector = static::buildConnector($loop, $connector);
135
        }
136
137
        // validate connector instance for proper error reporting
138
        if (null !== $connector && !$connector instanceof ConnectorInterface) {
139
            throw new \InvalidArgumentException(
140
                '$connector must be an instance of DnsResolver or ConnectorInterface'
141
            );
142
        }
143
144
        return new HttpClient($loop, $connector);
145
    }
146
}
147