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