1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bernard\Driver\Interop; |
4
|
|
|
|
5
|
|
|
use Interop\Amqp\AmqpContext; |
6
|
|
|
use Interop\Amqp\AmqpQueue; |
7
|
|
|
use Interop\Queue\PsrConsumer; |
8
|
|
|
use Interop\Queue\PsrContext; |
9
|
|
|
|
10
|
|
|
final class Driver implements \Bernard\Driver |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var PsrContext |
14
|
|
|
*/ |
15
|
|
|
private $context; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var PsrConsumer[] |
19
|
|
|
*/ |
20
|
|
|
private $consumers; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @param PsrContext $context |
24
|
|
|
*/ |
25
|
13 |
|
public function __construct(PsrContext $context) |
26
|
|
|
{ |
27
|
13 |
|
$this->context = $context; |
28
|
|
|
|
29
|
13 |
|
$this->consumers = []; |
30
|
13 |
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* {@inheritdoc} |
34
|
|
|
*/ |
35
|
1 |
|
public function listQueues() |
36
|
|
|
{ |
37
|
1 |
|
return []; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* {@inheritdoc} |
42
|
|
|
*/ |
43
|
2 |
|
public function createQueue($queueName) |
44
|
|
|
{ |
45
|
2 |
|
if ($this->context instanceof AmqpContext) { |
46
|
1 |
|
$this->context->declareQueue($this->createAmqpQueue($queueName)); |
47
|
1 |
|
} |
48
|
2 |
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* {@inheritdoc} |
52
|
|
|
*/ |
53
|
1 |
|
public function countMessages($queueName) |
54
|
|
|
{ |
55
|
1 |
|
if ($this->context instanceof AmqpContext) { |
56
|
1 |
|
return $this->context->declareQueue($this->createAmqpQueue($queueName)); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
return 0; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* {@inheritdoc} |
64
|
|
|
*/ |
65
|
1 |
|
public function pushMessage($queueName, $message) |
66
|
|
|
{ |
67
|
1 |
|
$queue = $this->context->createQueue($queueName); |
68
|
1 |
|
$message = $this->context->createMessage($message); |
69
|
|
|
|
70
|
1 |
|
$this->context->createProducer()->send($queue, $message); |
71
|
1 |
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* {@inheritdoc} |
75
|
|
|
*/ |
76
|
3 |
|
public function popMessage($queueName, $duration = 5) |
77
|
|
|
{ |
78
|
3 |
|
if ($message = $this->getQueueConsumer($queueName)->receive($duration * 1000)) { |
79
|
2 |
|
return [$message->getBody(), $message]; |
80
|
|
|
} |
81
|
1 |
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* {@inheritdoc} |
85
|
|
|
*/ |
86
|
1 |
|
public function acknowledgeMessage($queueName, $receipt) |
87
|
|
|
{ |
88
|
1 |
|
$this->getQueueConsumer($queueName)->acknowledge($receipt); |
89
|
1 |
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* {@inheritdoc} |
93
|
|
|
*/ |
94
|
1 |
|
public function peekQueue($queueName, $index = 0, $limit = 20) |
95
|
|
|
{ |
96
|
1 |
|
return []; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* {@inheritdoc} |
101
|
|
|
*/ |
102
|
3 |
|
public function removeQueue($queueName) |
103
|
|
|
{ |
104
|
3 |
|
if ($this->context instanceof AmqpContext) { |
105
|
1 |
|
$queue = $this->createAmqpQueue($queueName); |
106
|
|
|
|
107
|
1 |
|
$this->context->deleteQueue($queue); |
108
|
1 |
|
} |
109
|
3 |
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* {@inheritdoc} |
113
|
|
|
*/ |
114
|
|
|
public function info() |
115
|
|
|
{ |
116
|
|
|
return []; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @param string $queueName |
121
|
|
|
* |
122
|
|
|
* @return PsrConsumer |
123
|
|
|
*/ |
124
|
3 |
|
private function getQueueConsumer($queueName) |
125
|
|
|
{ |
126
|
3 |
|
if (false == array_key_exists($queueName, $this->consumers)) { |
|
|
|
|
127
|
3 |
|
$queue = $this->context->createQueue($queueName); |
128
|
|
|
|
129
|
3 |
|
$this->consumers[$queueName] = $this->context->createConsumer($queue); |
130
|
3 |
|
} |
131
|
|
|
|
132
|
3 |
|
return $this->consumers[$queueName]; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @param string $queueName |
137
|
|
|
* |
138
|
|
|
* @return AmqpQueue |
139
|
|
|
*/ |
140
|
3 |
|
private function createAmqpQueue($queueName) |
141
|
|
|
{ |
142
|
|
|
/** @var AmqpContext $context */ |
143
|
3 |
|
$context = $this->context; |
144
|
|
|
|
145
|
3 |
|
$queue = $context->createQueue($queueName); |
146
|
3 |
|
$queue->addFlag(AmqpQueue::FLAG_DURABLE); |
147
|
|
|
|
148
|
3 |
|
return $queue; |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|
When comparing two booleans, it is generally considered safer to use the strict comparison operator.