Skip to content

Commit 2f516e5

Browse files
committed
chore(refacto): Refine ReactFactory.
1 parent da2a60e commit 2f516e5

File tree

2 files changed

+16
-143
lines changed

2 files changed

+16
-143
lines changed

src/ReactFactory.php

Lines changed: 11 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,13 @@
44

55
use React\EventLoop\LoopInterface;
66
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;
7+
use React\Http\Browser;
128
use React\Socket\ConnectorInterface;
139

1410
/**
1511
* Factory wrapper for React instances.
1612
*
17-
* @author Stéphane Hulard <stephane@hlrd.me>
13+
* @author Stéphane Hulard <s.hulard@chstudio.fr>
1814
*/
1915
class ReactFactory
2016
{
@@ -28,119 +24,21 @@ public static function buildEventLoop()
2824
return EventLoopFactory::create();
2925
}
3026

31-
/**
32-
* Build a React Dns Resolver.
33-
*
34-
* @param LoopInterface $loop
35-
* @param string $dns
36-
*
37-
* @return DnsResolver
38-
*/
39-
public static function buildDnsResolver(
40-
LoopInterface $loop,
41-
$dns = '8.8.8.8'
42-
) {
43-
$factory = new DnsResolverFactory();
44-
45-
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-
6327
/**
6428
* Build a React Http Client.
6529
*
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.
30+
* @param LoopInterface $loop
31+
* @param ConnectorInterface|null $connector Only pass this argument if you need to customize DNS
32+
* behaviour.
7033
*
71-
* @return HttpClient
34+
* @return Browser
7235
*/
7336
public static function buildHttpClient(
7437
LoopInterface $loop,
75-
$connector = null
76-
) {
77-
if (class_exists(HttpClientFactory::class)) {
78-
// if HttpClientFactory class exists, use old behavior for backwards compatibility
79-
return static::buildHttpClient04($loop, $connector);
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-
protected static function buildHttpClient04(
94-
LoopInterface $loop,
95-
$dns = null
96-
) {
97-
// create dns resolver if one isn't provided
98-
if (null === $dns) {
99-
$dns = static::buildDnsResolver($loop);
100-
}
101-
102-
// validate connector instance for proper error reporting
103-
if (!$dns instanceof DnsResolver) {
104-
throw new \InvalidArgumentException('For react http client v0.4, $dns must be an instance of DnsResolver');
105-
}
106-
107-
$factory = new HttpClientFactory();
108-
109-
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(
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);
38+
ConnectorInterface $connector = null
39+
): Browser {
40+
return (new Browser($loop, $connector))
41+
->withRejectErrorResponse(false)
42+
->withFollowRedirects(false);
14543
}
14644
}

tests/ReactFactoryTest.php

Lines changed: 5 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,8 @@
44

55
use Http\Adapter\React\ReactFactory;
66
use PHPUnit\Framework\TestCase;
7-
use React\Dns\Resolver\Resolver;
87
use React\EventLoop\LoopInterface;
9-
use React\HttpClient\Client;
10-
use React\HttpClient\Factory;
8+
use React\Http\Browser;
119
use React\Socket\ConnectorInterface;
1210

1311
/**
@@ -23,45 +21,22 @@ class ReactFactoryTest extends TestCase
2321
*/
2422
private $loop;
2523

26-
protected function setUp()
24+
protected function setUp(): void
2725
{
2826
$this->loop = $this->getMockBuilder(LoopInterface::class)->getMock();
2927
}
3028

3129
public function testBuildHttpClientWithConnector()
3230
{
33-
if (class_exists(Factory::class)) {
34-
$this->markTestSkipped('This test only runs with react http client v0.5 and above');
35-
}
36-
31+
/** @var ConnectorInterface $connector */
3732
$connector = $this->getMockBuilder(ConnectorInterface::class)->getMock();
3833
$client = ReactFactory::buildHttpClient($this->loop, $connector);
39-
$this->assertInstanceOf(Client::class, $client);
40-
}
41-
42-
/**
43-
* @deprecated Building HTTP client passing a DnsResolver instance is deprecated. Should pass a ConnectorInterface
44-
* instance instead.
45-
*/
46-
public function testBuildHttpClientWithDnsResolver()
47-
{
48-
$connector = $this->getMockBuilder(Resolver::class)->disableOriginalConstructor()->getMock();
49-
$client = ReactFactory::buildHttpClient($this->loop, $connector);
50-
$this->assertInstanceOf(Client::class, $client);
34+
$this->assertInstanceOf(Browser::class, $client);
5135
}
5236

5337
public function testBuildHttpClientWithoutConnector()
5438
{
5539
$client = ReactFactory::buildHttpClient($this->loop);
56-
$this->assertInstanceOf(Client::class, $client);
57-
}
58-
59-
/**
60-
* @expectedException \InvalidArgumentException
61-
*/
62-
public function testBuildHttpClientWithInvalidConnectorThrowsException()
63-
{
64-
$connector = $this->getMockBuilder(LoopInterface::class)->getMock();
65-
ReactFactory::buildHttpClient($this->loop, $connector);
40+
$this->assertInstanceOf(Browser::class, $client);
6641
}
6742
}

0 commit comments

Comments
 (0)