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