Skip to content

Commit a077648

Browse files
committed
(Sync) Client
1 parent 2f08fd6 commit a077648

File tree

1 file changed

+84
-0
lines changed

1 file changed

+84
-0
lines changed

src/Client.php

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace ApiClients\Client\RabbitMQ\Management;
5+
6+
use ApiClients\Foundation\Hydrator\Options;
7+
use React\EventLoop\Factory as LoopFactory;
8+
use Rx\React\Promise;
9+
use ApiClients\Client\RabbitMQ\Management\Resource\OverviewInterface;
10+
use ApiClients\Foundation\Transport\Client as Transport;
11+
use ApiClients\Foundation\Transport\Factory;
12+
use function Clue\React\Block\await;
13+
use function React\Promise\resolve;
14+
15+
final class Client
16+
{
17+
/**
18+
* @var Transport
19+
*/
20+
protected $transport;
21+
22+
/**
23+
* @var AsyncClient
24+
*/
25+
protected $client;
26+
27+
/**
28+
* @param string $baseUrl
29+
* @param string $username
30+
* @param string $password
31+
* @param Transport|null $transport
32+
*/
33+
public function __construct(
34+
string $baseUrl,
35+
string $username,
36+
string $password,
37+
Transport $transport = null
38+
) {
39+
$loop = LoopFactory::create();
40+
if (!($transport instanceof Transport)) {
41+
$options = ApiSettings::getOptions($baseUrl, $username, $password, 'Sync');
42+
$transport = Factory::create($loop, $options);
43+
}
44+
$this->transport = $transport;
45+
$this->client = new AsyncClient($loop, $baseUrl, $username, $password, $this->transport);
46+
}
47+
48+
/**
49+
* @return OverviewInterface
50+
*/
51+
public function overview(): OverviewInterface
52+
{
53+
return await(
54+
$this->client->overview(),
55+
$this->transport->getLoop()
56+
);
57+
}
58+
59+
/**
60+
* @return array
61+
*/
62+
public function queues(): array
63+
{
64+
return await(
65+
Promise::fromObservable(
66+
$this->client->queues()->toArray()
67+
),
68+
$this->transport->getLoop()
69+
);
70+
}
71+
72+
/**
73+
* @return array
74+
*/
75+
public function connections(): array
76+
{
77+
return await(
78+
Promise::fromObservable(
79+
$this->client->connections()->toArray()
80+
),
81+
$this->transport->getLoop()
82+
);
83+
}
84+
}

0 commit comments

Comments
 (0)