-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathDriver.php
107 lines (83 loc) · 2.63 KB
/
Driver.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
<?php
declare(strict_types=1);
namespace Bernard\Driver\Amqp;
use Bernard\Driver\Message;
use PhpAmqpLib\Channel\AMQPChannel;
use PhpAmqpLib\Connection\AbstractConnection;
use PhpAmqpLib\Message\AMQPMessage;
final class Driver implements \Bernard\Driver
{
private ?AMQPChannel $channel = null;
public function __construct(
private AbstractConnection $connection,
private string $exchange,
private array $defaultMessageProperties = [],
) {
}
public function listQueues(): array
{
return [];
}
public function createQueue(string $queueName): void
{
$channel = $this->getChannel();
$channel->exchange_declare($this->exchange, 'direct', false, true, false);
$channel->queue_declare($queueName, false, true, false, false);
$channel->queue_bind($queueName, $this->exchange, $queueName);
}
public function removeQueue(string $queueName): void
{
$this->getChannel()->queue_delete($queueName);
}
public function pushMessage(string $queueName, string $message): void
{
$amqpMessage = new AMQPMessage($message, $this->defaultMessageProperties);
$this->getChannel()->basic_publish($amqpMessage, $this->exchange, $queueName);
}
public function popMessage(string $queueName, int $duration = 5): ?Message
{
$runtime = microtime(true) + $duration;
while (microtime(true) < $runtime) {
$message = $this->getChannel()->basic_get($queueName);
if ($message) {
return new Message($message->body, $message->getDeliveryTag());
}
// sleep for 10 ms to prevent hammering CPU
usleep(10000);
}
return null;
}
public function acknowledgeMessage(string $queueName, mixed $receipt): void
{
$this->getChannel()->basic_ack($receipt);
}
public function __destruct()
{
if ($this->channel !== null) {
$this->channel->close();
}
}
public function info(): array
{
return [];
}
public function countMessages(string $queueName): int
{
[, $messageCount] = $this->getChannel()->queue_declare($queueName, true);
return $messageCount;
}
public function peekQueue(string $queueName, int $index = 0, int $limit = 20): array
{
return [];
}
/**
* Creates a channel or returns an already created one.
*/
private function getChannel(): AMQPChannel
{
if ($this->channel === null) {
$this->channel = $this->connection->channel();
}
return $this->channel;
}
}