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