Skip to content

Commit f25614b

Browse files
committed
Updated clients
1 parent a077648 commit f25614b

File tree

4 files changed

+136
-38
lines changed

4 files changed

+136
-38
lines changed

src/AsyncClient.php

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
namespace ApiClients\Client\RabbitMQ\Management;
55

6+
use ApiClients\Foundation\ClientInterface as FoundationClientInterface;
7+
use ApiClients\Foundation\Factory;
68
use ApiClients\Foundation\Hydrator\CommandBus\Command\HydrateCommand;
79
use ApiClients\Foundation\Transport\CommandBus\Command\SimpleRequestCommand;
810
use Psr\Http\Message\ResponseInterface;
@@ -11,35 +13,54 @@
1113
use Rx\Observable;
1214
use Rx\ObservableInterface;
1315
use Rx\React\Promise;
14-
use ApiClients\Foundation\Client;
15-
use ApiClients\Foundation\Factory;
1616
use function React\Promise\resolve;
1717

18-
final class AsyncClient
18+
final class AsyncClient implements AsyncClientInterface
1919
{
2020
/**
21-
* @var Client
21+
* @var FoundationClientInterface
2222
*/
23-
protected $client;
23+
private $client;
2424

2525
/**
26+
* Create a new AsyncClient based on the loop and other options pass
27+
*
2628
* @param LoopInterface $loop
2729
* @param string $baseUrl
2830
* @param string $username
2931
* @param string $password
30-
* @param Client|null $client
32+
* @param array $options
33+
* @return AsyncClient
3134
*/
32-
public function __construct(
35+
public static function create(
3336
LoopInterface $loop,
3437
string $baseUrl,
3538
string $username,
3639
string $password,
37-
Client $client = null
38-
) {
39-
if (!($client instanceof Client)) {
40-
$options = ApiSettings::getOptions($baseUrl, $username, $password, 'Async');
41-
$client = Factory::create($loop, $options);
42-
}
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+
{
4364
$this->client = $client;
4465
}
4566

src/AsyncClientInterface.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace ApiClients\Client\RabbitMQ\Management;
5+
6+
use React\Promise\PromiseInterface;
7+
use Rx\Observable;
8+
use Rx\ObservableInterface;
9+
use function React\Promise\resolve;
10+
11+
interface AsyncClientInterface
12+
{
13+
/**
14+
* @return PromiseInterface
15+
*/
16+
public function overview(): PromiseInterface;
17+
18+
/**
19+
* @return ObservableInterface
20+
*/
21+
public function queues(): ObservableInterface;
22+
23+
/**
24+
* @return ObservableInterface
25+
*/
26+
public function connections(): ObservableInterface;
27+
}

src/Client.php

Lines changed: 49 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,46 +3,70 @@
33

44
namespace ApiClients\Client\RabbitMQ\Management;
55

6-
use ApiClients\Foundation\Hydrator\Options;
6+
use ApiClients\Client\RabbitMQ\Management\Resource\OverviewInterface;
7+
use ApiClients\Foundation\Factory;
78
use React\EventLoop\Factory as LoopFactory;
9+
use React\EventLoop\LoopInterface;
810
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;
1211
use function Clue\React\Block\await;
1312
use function React\Promise\resolve;
1413

15-
final class Client
14+
final class Client implements ClientInterface
1615
{
1716
/**
18-
* @var Transport
17+
* @var LoopInterface
1918
*/
20-
protected $transport;
19+
private $loop;
2120

2221
/**
23-
* @var AsyncClient
22+
* @var AsyncClientInterface
2423
*/
25-
protected $client;
24+
private $asyncClient;
2625

2726
/**
27+
* Create a new AsyncClient based on the loop and other options pass
28+
*
2829
* @param string $baseUrl
2930
* @param string $username
3031
* @param string $password
31-
* @param Transport|null $transport
32+
* @param array $options
33+
* @return Client
3234
*/
33-
public function __construct(
35+
public static function create(
3436
string $baseUrl,
3537
string $username,
3638
string $password,
37-
Transport $transport = null
38-
) {
39+
array $options = []
40+
): self {
3941
$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);
42+
$options = ApiSettings::getOptions($baseUrl, $username, $password, 'Sync');
43+
$client = Factory::create($loop, $options);
44+
$asyncClient = AsyncClient::createFromClient($client);
45+
return self::createFromClient($loop, $asyncClient);
46+
}
47+
48+
/**
49+
* Create an Client from a AsyncClientInterface.
50+
* Be sure to pass in a client with the options from ApiSettings and the Sync namespace suffix,
51+
* and pass in the same loop as associated with the AsyncClient you're passing in.
52+
*
53+
* @param LoopInterface $loop
54+
* @param AsyncClientInterface $asyncClient
55+
* @return Client
56+
*/
57+
public static function createFromClient(LoopInterface $loop, AsyncClientInterface $asyncClient): self
58+
{
59+
return new self($loop, $asyncClient);
60+
}
61+
62+
/**
63+
* @param LoopInterface $loop
64+
* @param AsyncClientInterface $asyncClient
65+
*/
66+
private function __construct(LoopInterface $loop, AsyncClientInterface $asyncClient)
67+
{
68+
$this->loop = $loop;
69+
$this->asyncClient = $asyncClient;
4670
}
4771

4872
/**
@@ -51,8 +75,8 @@ public function __construct(
5175
public function overview(): OverviewInterface
5276
{
5377
return await(
54-
$this->client->overview(),
55-
$this->transport->getLoop()
78+
$this->asyncClient->overview(),
79+
$this->loop
5680
);
5781
}
5882

@@ -63,9 +87,9 @@ public function queues(): array
6387
{
6488
return await(
6589
Promise::fromObservable(
66-
$this->client->queues()->toArray()
90+
$this->asyncClient->queues()->toArray()
6791
),
68-
$this->transport->getLoop()
92+
$this->loop
6993
);
7094
}
7195

@@ -76,9 +100,9 @@ public function connections(): array
76100
{
77101
return await(
78102
Promise::fromObservable(
79-
$this->client->connections()->toArray()
103+
$this->asyncClient->connections()->toArray()
80104
),
81-
$this->transport->getLoop()
105+
$this->loop
82106
);
83107
}
84108
}

src/ClientInterface.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace ApiClients\Client\RabbitMQ\Management;
5+
6+
use ApiClients\Client\RabbitMQ\Management\Resource\OverviewInterface;
7+
use function Clue\React\Block\await;
8+
use function React\Promise\resolve;
9+
10+
interface ClientInterface
11+
{
12+
/**
13+
* @return OverviewInterface
14+
*/
15+
public function overview(): OverviewInterface;
16+
17+
/**
18+
* @return array
19+
*/
20+
public function queues(): array;
21+
22+
/**
23+
* @return array
24+
*/
25+
public function connections(): array;
26+
}

0 commit comments

Comments
 (0)