1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of ReactGuzzleRing. |
5
|
|
|
* |
6
|
|
|
** (c) 2014 Cees-Jan Kiewiet |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
namespace WyriHaximus\React\Guzzle\HttpClient; |
12
|
|
|
|
13
|
|
|
use Clue\React\Buzz\Browser; |
14
|
|
|
use Clue\React\Buzz\Io\Sender; |
15
|
|
|
use Clue\React\Socks\Client as SocksClient; |
16
|
|
|
use Psr\Http\Message\RequestInterface; |
17
|
|
|
use React\EventLoop\LoopInterface; |
18
|
|
|
use React\HttpClient\Client as HttpClient; |
19
|
|
|
use React\SocketClient\Connector; |
20
|
|
|
use React\SocketClient\ConnectorInterface; |
21
|
|
|
use React\SocketClient\DnsConnector; |
22
|
|
|
use ReflectionObject; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Class RequestFactory |
26
|
|
|
* |
27
|
|
|
* @package WyriHaximus\React\Guzzle\HttpClient |
28
|
|
|
*/ |
29
|
|
|
class RequestFactory |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* |
33
|
|
|
* @param RequestInterface $request |
34
|
|
|
* @param array $options |
35
|
|
|
* @param HttpClient $httpClient |
36
|
|
|
* @param LoopInterface $loop |
37
|
|
|
* @return \React\Promise\Promise |
38
|
|
|
*/ |
39
|
1 |
|
public function create(RequestInterface $request, array $options, HttpClient $httpClient, LoopInterface $loop) |
40
|
|
|
{ |
41
|
1 |
|
$sender = $this->createSender($options, $httpClient, $loop); |
42
|
1 |
|
return (new Browser($loop, $sender)) |
43
|
1 |
|
->withOptions($this->convertOptions($options)) |
44
|
1 |
|
->send($request); |
45
|
|
|
} |
46
|
|
|
|
47
|
1 |
|
protected function createSender(array $options, HttpClient $httpClient, LoopInterface $loop) |
48
|
|
|
{ |
49
|
1 |
|
if (isset($options['proxy'])) { |
50
|
|
|
$connector = $this->extractConnector($httpClient); |
51
|
|
|
$resolver = $this->extractResolver($connector); |
52
|
|
|
$socks = new SocksClient($options['proxy'], $loop, $connector, $resolver); |
53
|
|
|
return Sender::createFromLoopConnectors($loop, $socks->createConnector()); |
54
|
|
|
} |
55
|
|
|
|
56
|
1 |
|
return new Sender($httpClient); |
57
|
|
|
} |
58
|
|
|
|
59
|
1 |
|
protected function convertOptions(array $options) |
60
|
|
|
{ |
61
|
1 |
|
return $options; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
protected function extractConnector(HttpClient $httpClient) |
65
|
|
|
{ |
66
|
|
|
$reflection = new ReflectionObject($httpClient); |
67
|
|
|
$property = $reflection->getProperty('connector'); |
68
|
|
|
$property->setAccessible(true); |
69
|
|
|
return $property->getValue($httpClient); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
protected function extractResolver(ConnectorInterface $connector) |
73
|
|
|
{ |
74
|
|
View Code Duplication |
if ($connector instanceof Connector) { |
|
|
|
|
75
|
|
|
$reflection = new ReflectionObject($connector); |
76
|
|
|
$property = $reflection->getProperty('resolver'); |
77
|
|
|
$property->setAccessible(true); |
78
|
|
|
return $property->getValue($connector); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
View Code Duplication |
if ($connector instanceof DnsConnector) { |
|
|
|
|
82
|
|
|
$reflection = new ReflectionObject($connector); |
83
|
|
|
$property = $reflection->getProperty('resolver'); |
84
|
|
|
$property->setAccessible(true); |
85
|
|
|
return $property->getValue($connector); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
return null; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.