1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace ApiClients\Foundation\Transport; |
4
|
|
|
|
5
|
|
|
use Clue\React\Buzz\Browser; |
6
|
|
|
use Clue\React\Buzz\Io\Sender; |
7
|
|
|
use Interop\Container\ContainerInterface; |
8
|
|
|
use React\Dns\Resolver\Factory as ResolverFactory; |
9
|
|
|
use React\Dns\Resolver\Resolver; |
10
|
|
|
use React\EventLoop\LoopInterface; |
11
|
|
|
use React\HttpClient\Client as HttpClient; |
12
|
|
|
use React\HttpClient\Factory as HttpClientFactory; |
13
|
|
|
|
14
|
|
|
class Factory |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @param ContainerInterface $container |
18
|
|
|
* @param LoopInterface $loop |
19
|
|
|
* @param array $options |
20
|
|
|
* @return Client |
21
|
|
|
*/ |
22
|
|
|
public static function create( |
23
|
|
|
ContainerInterface $container, |
24
|
|
|
LoopInterface $loop, |
25
|
|
|
array $options = [] |
26
|
|
|
): Client { |
27
|
|
|
if (!isset($options[Options::DNS])) { |
28
|
|
|
$options[Options::DNS] = '8.8.8.8'; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
$resolver = (new ResolverFactory())->createCached($options[Options::DNS], $loop); |
32
|
|
|
$httpClient = (new HttpClientFactory())->create($loop, $resolver); |
33
|
|
|
|
34
|
|
|
return self::createFromReactHttpClient( |
35
|
|
|
$container, |
36
|
|
|
$httpClient, |
37
|
|
|
$resolver, |
38
|
|
|
$loop, |
39
|
|
|
$options |
40
|
|
|
); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param ContainerInterface $container |
45
|
|
|
* @param HttpClient $httpClient |
46
|
|
|
* @param Resolver $resolver |
47
|
|
|
* @param LoopInterface $loop |
48
|
|
|
* @param array $options |
49
|
|
|
* @return Client |
50
|
|
|
*/ |
51
|
|
|
public static function createFromReactHttpClient( |
52
|
|
|
ContainerInterface $container, |
53
|
|
|
HttpClient $httpClient, |
|
|
|
|
54
|
|
|
Resolver $resolver, |
55
|
|
|
LoopInterface $loop, |
56
|
|
|
array $options = [] |
57
|
|
|
): Client { |
58
|
|
|
return self::createFromBuzz( |
59
|
|
|
$container, |
60
|
|
|
$loop, |
61
|
|
|
(new Browser($loop, Sender::createFromLoopDns($loop, $resolver)))->withOptions([ |
62
|
|
|
'streaming' => true, |
63
|
|
|
]), |
64
|
|
|
$options |
65
|
|
|
); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param ContainerInterface $container |
70
|
|
|
* @param LoopInterface $loop |
71
|
|
|
* @param Browser $buzz |
72
|
|
|
* @param array $options |
73
|
|
|
* @return Client |
74
|
|
|
*/ |
75
|
|
|
public static function createFromBuzz( |
76
|
|
|
ContainerInterface $container, |
77
|
|
|
LoopInterface $loop, |
78
|
|
|
Browser $buzz, |
79
|
|
|
array $options = [] |
80
|
|
|
): Client { |
81
|
|
|
return new Client( |
82
|
|
|
$loop, |
83
|
|
|
$container, |
84
|
|
|
$buzz, |
85
|
|
|
$options |
86
|
|
|
); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.