1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Sonata Project package. |
5
|
|
|
* |
6
|
|
|
* (c) Thomas Rabaix <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Sonata\NotificationBundle\Backend; |
13
|
|
|
|
14
|
|
|
use Interop\Amqp\AmqpConsumer; |
15
|
|
|
use Interop\Amqp\AmqpContext; |
16
|
|
|
use Interop\Amqp\AmqpMessage; |
17
|
|
|
use Interop\Amqp\AmqpQueue; |
18
|
|
|
use Interop\Amqp\AmqpTopic; |
19
|
|
|
use Interop\Amqp\Impl\AmqpBind; |
20
|
|
|
use PhpAmqpLib\Channel\AMQPChannel; |
21
|
|
|
use Sonata\NotificationBundle\Consumer\ConsumerEvent; |
22
|
|
|
use Sonata\NotificationBundle\Exception\HandlingException; |
23
|
|
|
use Sonata\NotificationBundle\Iterator\AMQPMessageIterator; |
24
|
|
|
use Sonata\NotificationBundle\Model\Message; |
25
|
|
|
use Sonata\NotificationBundle\Model\MessageInterface; |
26
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
27
|
|
|
use ZendDiagnostics\Result\Failure; |
28
|
|
|
use ZendDiagnostics\Result\Success; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Consumer side of the rabbitMQ backend. |
32
|
|
|
*/ |
33
|
|
|
class AMQPBackend implements BackendInterface |
34
|
|
|
{ |
35
|
|
|
/** |
36
|
|
|
* @var AMQPBackendDispatcher |
37
|
|
|
*/ |
38
|
|
|
protected $dispatcher = null; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var string |
42
|
|
|
*/ |
43
|
|
|
protected $exchange; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var string |
47
|
|
|
*/ |
48
|
|
|
protected $queue; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @var string |
52
|
|
|
*/ |
53
|
|
|
protected $key; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @var string |
57
|
|
|
*/ |
58
|
|
|
protected $recover; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @var null|string |
62
|
|
|
*/ |
63
|
|
|
protected $deadLetterExchange; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @var null|string |
67
|
|
|
*/ |
68
|
|
|
protected $deadLetterRoutingKey; |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @var null|int |
72
|
|
|
*/ |
73
|
|
|
protected $ttl; |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @var null|int |
77
|
|
|
*/ |
78
|
|
|
private $prefetchCount; |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @var AmqpConsumer |
82
|
|
|
*/ |
83
|
|
|
private $consumer; |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param string $exchange |
87
|
|
|
* @param string $queue |
88
|
|
|
* @param string $recover |
89
|
|
|
* @param string $key |
90
|
|
|
* @param string $deadLetterExchange |
91
|
|
|
* @param string $deadLetterRoutingKey |
92
|
|
|
* @param null|int $ttl |
93
|
|
|
*/ |
94
|
|
|
public function __construct($exchange, $queue, $recover, $key, $deadLetterExchange = null, $deadLetterRoutingKey = null, $ttl = null, $prefetchCount = null) |
95
|
|
|
{ |
96
|
|
|
$this->exchange = $exchange; |
97
|
|
|
$this->queue = $queue; |
98
|
|
|
$this->recover = $recover; |
99
|
|
|
$this->key = $key; |
100
|
|
|
$this->deadLetterExchange = $deadLetterExchange; |
101
|
|
|
$this->deadLetterRoutingKey = $deadLetterRoutingKey; |
102
|
|
|
$this->ttl = $ttl; |
103
|
|
|
$this->prefetchCount = $prefetchCount; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param AMQPBackendDispatcher $dispatcher |
108
|
|
|
*/ |
109
|
|
|
public function setDispatcher(AMQPBackendDispatcher $dispatcher) |
110
|
|
|
{ |
111
|
|
|
$this->dispatcher = $dispatcher; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* {@inheritdoc} |
116
|
|
|
*/ |
117
|
|
|
public function initialize() |
118
|
|
|
{ |
119
|
|
|
$args = []; |
120
|
|
|
if (null !== $this->deadLetterExchange) { |
121
|
|
|
$args['x-dead-letter-exchange'] = $this->deadLetterExchange; |
122
|
|
|
|
123
|
|
|
if (null !== $this->deadLetterRoutingKey) { |
124
|
|
|
$args['x-dead-letter-routing-key'] = $this->deadLetterRoutingKey; |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
if (null !== $this->ttl) { |
129
|
|
|
$args['x-message-ttl'] = $this->ttl; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
$queue = $this->getContext()->createQueue($this->queue); |
133
|
|
|
$queue->addFlag(AmqpQueue::FLAG_DURABLE); |
134
|
|
|
$queue->setArguments($args); |
135
|
|
|
$this->getContext()->declareQueue($queue); |
|
|
|
|
136
|
|
|
|
137
|
|
|
$topic = $this->getContext()->createTopic($this->exchange); |
138
|
|
|
$topic->setType(AmqpTopic::TYPE_DIRECT); |
139
|
|
|
$topic->addFlag(AmqpTopic::FLAG_DURABLE); |
140
|
|
|
$this->getContext()->declareTopic($topic); |
|
|
|
|
141
|
|
|
|
142
|
|
|
$this->getContext()->bind(new AmqpBind($queue, $topic, $this->key)); |
|
|
|
|
143
|
|
|
|
144
|
|
|
if (null !== $this->deadLetterExchange && null === $this->deadLetterRoutingKey) { |
145
|
|
|
$deadLetterTopic = $this->getContext()->createTopic($this->deadLetterExchange); |
146
|
|
|
$deadLetterTopic->setType(AmqpTopic::TYPE_DIRECT); |
147
|
|
|
$deadLetterTopic->addFlag(AmqpTopic::FLAG_DURABLE); |
148
|
|
|
$this->getContext()->declareTopic($deadLetterTopic); |
|
|
|
|
149
|
|
|
|
150
|
|
|
$this->getContext()->bind(new AmqpBind($queue, $deadLetterTopic, $this->key)); |
|
|
|
|
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* {@inheritdoc} |
156
|
|
|
*/ |
157
|
|
|
public function publish(MessageInterface $message) |
158
|
|
|
{ |
159
|
|
|
$body = json_encode([ |
160
|
|
|
'type' => $message->getType(), |
161
|
|
|
'body' => $message->getBody(), |
162
|
|
|
'createdAt' => $message->getCreatedAt()->format('U'), |
163
|
|
|
'state' => $message->getState(), |
164
|
|
|
]); |
165
|
|
|
|
166
|
|
|
$amqpMessage = $this->getContext()->createMessage($body); |
167
|
|
|
$amqpMessage->setContentType('text/plain'); // application/json ? |
168
|
|
|
$amqpMessage->setTimestamp($message->getCreatedAt()->format('U')); |
169
|
|
|
$amqpMessage->setDeliveryMode(AmqpMessage::DELIVERY_MODE_PERSISTENT); |
170
|
|
|
$amqpMessage->setRoutingKey($this->key); |
171
|
|
|
|
172
|
|
|
$topic = $this->getContext()->createTopic($this->exchange); |
173
|
|
|
|
174
|
|
|
$this->getContext()->createProducer()->send($topic, $amqpMessage); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* {@inheritdoc} |
179
|
|
|
*/ |
180
|
|
|
public function create($type, array $body) |
181
|
|
|
{ |
182
|
|
|
$message = new Message(); |
183
|
|
|
$message->setType($type); |
184
|
|
|
$message->setBody($body); |
185
|
|
|
$message->setState(MessageInterface::STATE_OPEN); |
186
|
|
|
|
187
|
|
|
return $message; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* {@inheritdoc} |
192
|
|
|
*/ |
193
|
|
|
public function createAndPublish($type, array $body) |
194
|
|
|
{ |
195
|
|
|
$this->publish($this->create($type, $body)); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* {@inheritdoc} |
200
|
|
|
*/ |
201
|
|
|
public function getIterator() |
202
|
|
|
{ |
203
|
|
|
$context = $this->getContext(); |
204
|
|
|
|
205
|
|
|
if (null !== $this->prefetchCount) { |
206
|
|
|
$context->setQos(null, $this->prefetchCount, false); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
$this->consumer = $this->getContext()->createConsumer($this->getContext()->createQueue($this->queue)); |
210
|
|
|
$this->consumer->setConsumerTag('sonata_notification_'.uniqid()); |
211
|
|
|
|
212
|
|
|
return new AMQPMessageIterator($this->getChannel(), $this->consumer); |
|
|
|
|
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* {@inheritdoc} |
217
|
|
|
*/ |
218
|
|
|
public function handle(MessageInterface $message, EventDispatcherInterface $dispatcher) |
219
|
|
|
{ |
220
|
|
|
$event = new ConsumerEvent($message); |
221
|
|
|
|
222
|
|
|
/** @var AmqpMessage $amqpMessage */ |
223
|
|
|
$amqpMessage = $message->getValue('interopMessage]'); |
224
|
|
|
|
225
|
|
|
try { |
226
|
|
|
$dispatcher->dispatch($message->getType(), $event); |
227
|
|
|
|
228
|
|
|
$this->consumer->acknowledge($amqpMessage); |
229
|
|
|
|
230
|
|
|
$message->setCompletedAt(new \DateTime()); |
231
|
|
|
$message->setState(MessageInterface::STATE_DONE); |
232
|
|
|
} catch (HandlingException $e) { |
233
|
|
|
$message->setCompletedAt(new \DateTime()); |
234
|
|
|
$message->setState(MessageInterface::STATE_ERROR); |
235
|
|
|
|
236
|
|
|
$this->consumer->acknowledge($amqpMessage); |
237
|
|
|
|
238
|
|
|
throw new HandlingException('Error while handling a message', 0, $e); |
239
|
|
|
} catch (\Exception $e) { |
240
|
|
|
$message->setCompletedAt(new \DateTime()); |
241
|
|
|
$message->setState(MessageInterface::STATE_ERROR); |
242
|
|
|
|
243
|
|
|
$this->consumer->reject($amqpMessage, $this->recover); |
|
|
|
|
244
|
|
|
|
245
|
|
|
throw new HandlingException('Error while handling a message', 0, $e); |
246
|
|
|
} |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
/** |
250
|
|
|
* {@inheritdoc} |
251
|
|
|
*/ |
252
|
|
|
public function getStatus() |
253
|
|
|
{ |
254
|
|
|
try { |
255
|
|
|
$this->getContext(); |
256
|
|
|
} catch (\Exception $e) { |
257
|
|
|
return new Failure($e->getMessage()); |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
return new Success('Channel is running (RabbitMQ)'); |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
/** |
264
|
|
|
* {@inheritdoc} |
265
|
|
|
*/ |
266
|
|
|
public function cleanup() |
267
|
|
|
{ |
268
|
|
|
throw new \RuntimeException('Not implemented'); |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
/** |
272
|
|
|
* @deprecated since 3.2, will be removed in 4.x |
273
|
|
|
* |
274
|
|
|
* @return AMQPChannel |
275
|
|
|
*/ |
276
|
|
|
protected function getChannel() |
277
|
|
|
{ |
278
|
|
|
if (null === $this->dispatcher) { |
279
|
|
|
throw new \RuntimeException('Unable to retrieve AMQP channel without dispatcher.'); |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
return $this->dispatcher->getChannel(); |
|
|
|
|
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
/** |
286
|
|
|
* @return AmqpContext |
287
|
|
|
*/ |
288
|
|
|
protected function getContext() |
289
|
|
|
{ |
290
|
|
|
if (null === $this->dispatcher) { |
291
|
|
|
throw new \RuntimeException('Unable to retrieve AMQP context without dispatcher.'); |
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
return $this->dispatcher->getContext(); |
295
|
|
|
} |
296
|
|
|
} |
297
|
|
|
|
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.