|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace ApiClients\Foundation\Transport; |
|
5
|
|
|
|
|
6
|
|
|
use GuzzleHttp\Client as GuzzleClient; |
|
7
|
|
|
use GuzzleHttp\HandlerStack; |
|
8
|
|
|
use React\Dns\Resolver\Resolver; |
|
9
|
|
|
use React\EventLoop\Factory as LoopFactory; |
|
10
|
|
|
use React\EventLoop\LoopInterface; |
|
11
|
|
|
use React\HttpClient\Client as HttpClient; |
|
12
|
|
|
use React\HttpClient\Factory as HttpClientFactory; |
|
13
|
|
|
use React\Dns\Resolver\Factory as ResolverFactory; |
|
14
|
|
|
use WyriHaximus\React\GuzzlePsr7\HttpClientAdapter; |
|
15
|
|
|
|
|
16
|
|
|
class Factory |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* @param LoopInterface|null $loop |
|
20
|
|
|
* @param array $options |
|
21
|
|
|
* @return Client |
|
22
|
|
|
*/ |
|
23
|
2 |
|
public static function create(LoopInterface $loop = null, array $options = []): Client |
|
24
|
|
|
{ |
|
25
|
2 |
|
if (!($loop instanceof LoopInterface)) { |
|
26
|
1 |
|
$loop = LoopFactory::create(); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
2 |
|
if (!isset($options[Options::DNS])) { |
|
30
|
2 |
|
$options[Options::DNS] = '8.8.8.8'; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
2 |
|
$resolver = (new ResolverFactory())->createCached($options[Options::DNS], $loop); |
|
34
|
2 |
|
$httpClient = (new HttpClientFactory())->create($loop, $resolver); |
|
35
|
|
|
|
|
36
|
2 |
|
return self::createFromReactHttpClient( |
|
37
|
|
|
$httpClient, |
|
38
|
|
|
$resolver, |
|
39
|
|
|
$loop, |
|
40
|
|
|
$options |
|
41
|
|
|
); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @param HttpClient $httpClient |
|
46
|
|
|
* @param Resolver $resolver |
|
47
|
|
|
* @param LoopInterface|null $loop |
|
48
|
|
|
* @param array $options |
|
49
|
|
|
* @return Client |
|
50
|
|
|
*/ |
|
51
|
2 |
|
public static function createFromReactHttpClient( |
|
52
|
|
|
HttpClient $httpClient, |
|
53
|
|
|
Resolver $resolver, |
|
54
|
|
|
LoopInterface $loop = null, |
|
55
|
|
|
array $options = [] |
|
56
|
|
|
): Client { |
|
57
|
2 |
|
return self::createFromGuzzleClient( |
|
58
|
|
|
$loop, |
|
|
|
|
|
|
59
|
2 |
|
new GuzzleClient( |
|
60
|
|
|
[ |
|
61
|
2 |
|
'handler' => HandlerStack::create( |
|
62
|
2 |
|
new HttpClientAdapter( |
|
63
|
|
|
$loop, |
|
|
|
|
|
|
64
|
|
|
$httpClient, |
|
65
|
|
|
$resolver |
|
66
|
|
|
) |
|
67
|
|
|
), |
|
68
|
|
|
] |
|
69
|
|
|
), |
|
70
|
|
|
$options |
|
71
|
|
|
); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @param LoopInterface $loop |
|
76
|
|
|
* @param GuzzleClient $guzzle |
|
77
|
|
|
* @param array $options |
|
78
|
|
|
* @return Client |
|
79
|
|
|
*/ |
|
80
|
2 |
|
public static function createFromGuzzleClient( |
|
81
|
|
|
LoopInterface $loop, |
|
82
|
|
|
GuzzleClient $guzzle, |
|
83
|
|
|
array $options = [] |
|
84
|
|
|
): Client { |
|
85
|
2 |
|
return new Client( |
|
86
|
|
|
$loop, |
|
87
|
|
|
$guzzle, |
|
88
|
|
|
$options |
|
89
|
|
|
); |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|
It seems like you allow that null is being passed for a parameter, however the function which is called does not seem to accept null.
We recommend to add an additional type check (or disallow null for the parameter):