1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace ApiClients\Client\RabbitMQ\Management; |
5
|
|
|
|
6
|
|
|
use ApiClients\Foundation\ClientInterface as FoundationClientInterface; |
7
|
|
|
use ApiClients\Foundation\Factory; |
8
|
|
|
use ApiClients\Foundation\Hydrator\CommandBus\Command\HydrateCommand; |
9
|
|
|
use ApiClients\Foundation\Transport\CommandBus\Command\SimpleRequestCommand; |
10
|
|
|
use Psr\Http\Message\ResponseInterface; |
11
|
|
|
use React\EventLoop\LoopInterface; |
12
|
|
|
use React\Promise\PromiseInterface; |
13
|
|
|
use Rx\Observable; |
14
|
|
|
use Rx\ObservableInterface; |
15
|
|
|
use Rx\React\Promise; |
16
|
|
|
use function React\Promise\resolve; |
17
|
|
|
|
18
|
|
|
final class AsyncClient implements AsyncClientInterface |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var FoundationClientInterface |
22
|
|
|
*/ |
23
|
|
|
private $client; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Create a new AsyncClient based on the loop and other options pass |
27
|
|
|
* |
28
|
|
|
* @param LoopInterface $loop |
29
|
|
|
* @param string $baseUrl |
30
|
|
|
* @param string $username |
31
|
|
|
* @param string $password |
32
|
|
|
* @param array $options |
33
|
|
|
* @return AsyncClient |
34
|
|
|
*/ |
35
|
|
|
public static function create( |
36
|
|
|
LoopInterface $loop, |
37
|
|
|
string $baseUrl, |
38
|
|
|
string $username, |
39
|
|
|
string $password, |
40
|
|
|
array $options = [] |
|
|
|
|
41
|
|
|
): self { |
42
|
|
|
$options = ApiSettings::getOptions($baseUrl, $username, $password, 'Async'); |
43
|
|
|
$client = Factory::create($loop, $options); |
44
|
|
|
return new self($client); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Create an AsyncClient from a ApiClients\Foundation\ClientInterface. |
49
|
|
|
* Be sure to pass in a client with the options from ApiSettings and the Async namespace suffix. |
50
|
|
|
* |
51
|
|
|
* @param FoundationClientInterface $client |
52
|
|
|
* @return AsyncClient |
53
|
|
|
*/ |
54
|
|
|
public static function createFromClient(FoundationClientInterface $client): self |
55
|
|
|
{ |
56
|
|
|
return new self($client); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param FoundationClientInterface $client |
61
|
|
|
*/ |
62
|
|
|
private function __construct(FoundationClientInterface $client) |
63
|
|
|
{ |
64
|
|
|
$this->client = $client; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @return PromiseInterface |
69
|
|
|
*/ |
70
|
|
|
public function overview(): PromiseInterface |
71
|
|
|
{ |
72
|
|
|
return $this->client->handle( |
73
|
|
|
new SimpleRequestCommand('overview') |
74
|
|
|
)->then(function (ResponseInterface $response) { |
75
|
|
|
return resolve($this->client->handle( |
76
|
|
|
new HydrateCommand('Overview', $response->getBody()->getJson()) |
|
|
|
|
77
|
|
|
)); |
78
|
|
|
}); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @return ObservableInterface |
83
|
|
|
*/ |
84
|
|
View Code Duplication |
public function queues(): ObservableInterface |
|
|
|
|
85
|
|
|
{ |
86
|
|
|
return Promise::toObservable($this->client->handle( |
87
|
|
|
new SimpleRequestCommand('queues') |
88
|
|
|
))->flatMap(function (ResponseInterface $response) { |
89
|
|
|
return Observable::fromArray($response->getBody()->getJson()); |
|
|
|
|
90
|
|
|
})->flatMap(function ($queue) { |
91
|
|
|
return Promise::toObservable($this->client->handle( |
92
|
|
|
new HydrateCommand('Queue', $queue) |
93
|
|
|
)); |
94
|
|
|
}); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @return ObservableInterface |
99
|
|
|
*/ |
100
|
|
View Code Duplication |
public function connections(): ObservableInterface |
|
|
|
|
101
|
|
|
{ |
102
|
|
|
return Promise::toObservable($this->client->handle( |
103
|
|
|
new SimpleRequestCommand('connections') |
104
|
|
|
))->flatMap(function (ResponseInterface $response) { |
105
|
|
|
return Observable::fromArray($response->getBody()->getJson()); |
|
|
|
|
106
|
|
|
})->flatMap(function ($connection) { |
107
|
|
|
return Promise::toObservable($this->client->handle( |
108
|
|
|
new HydrateCommand('Connection', $connection) |
109
|
|
|
)); |
110
|
|
|
}); |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.