Skip to content

Commit 59299dc

Browse files
committed
Added a middleware to manage client instances + renamed parameter
1 parent 7491939 commit 59299dc

File tree

5 files changed

+41
-9
lines changed

5 files changed

+41
-9
lines changed

src/ClientMiddleware.php

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace Kiboko\Component\Flow\RabbitMQ;
4+
5+
use Bunny\Client;
6+
7+
class ClientMiddleware
8+
{
9+
private static ?Client $instance = null;
10+
11+
public static function getInstance(
12+
string $host,
13+
string $vhost,
14+
?string $user,
15+
?string $password,
16+
?int $port = null,
17+
): Client {
18+
if (null === self::$instance) {
19+
self::$instance = new Client([
20+
'host' => $host,
21+
'port' => $port,
22+
'vhost' => $vhost,
23+
'user' => $user,
24+
'password' => $password,
25+
]);
26+
27+
self::$instance->connect();
28+
}
29+
30+
return self::$instance;
31+
}
32+
}

src/Extractor.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@
1414
private Channel $channel;
1515

1616
public function __construct(
17-
private Client $connection,
17+
private Client $client,
1818
private string $topic,
1919
) {
20-
$this->channel = $this->connection->channel();
20+
$this->channel = $this->client->channel();
2121

2222
$this->channel->queueDeclare(
2323
queue: $this->topic,

src/Loader.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@
1515
private Channel $channel;
1616

1717
public function __construct(
18-
private Client $connection,
18+
private Client $client,
1919
private string $topic,
2020
private ?string $exchange = null,
2121
) {
22-
$this->channel = $this->connection->channel();
22+
$this->channel = $this->client->channel();
2323

2424
$this->channel->queueDeclare(
2525
queue: $this->topic,

src/Rejection.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@
1414
private Channel $channel;
1515

1616
public function __construct(
17-
private Client $connection,
17+
private Client $client,
1818
private string $stepUuid,
1919
private string $topic,
2020
private ?string $exchange = null,
2121
) {
22-
$this->channel = $this->connection->channel();
22+
$this->channel = $this->client->channel();
2323
$this->channel->queueDeclare(
2424
queue: $this->topic,
2525
passive: false,
@@ -32,7 +32,7 @@ public function __construct(
3232
public function teardown(): void
3333
{
3434
$this->channel->close();
35-
$this->connection->stop();
35+
$this->client->stop();
3636
}
3737

3838
public static function withoutAuthentication(

src/StateManager.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@ class StateManager
1717
private readonly Channel $channel;
1818

1919
public function __construct(
20-
private readonly Client $connection,
20+
private readonly Client $client,
2121
private readonly string $topic,
2222
private readonly int $lineThreshold = 1000,
2323
private readonly ?string $exchange = null,
2424
) {
25-
$this->channel = $this->connection->channel();
25+
$this->channel = $this->client->channel();
2626

2727
$this->channel->queueDeclare(
2828
queue: $this->topic,

0 commit comments

Comments
 (0)