1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Happyr\Mq2phpBundle\Consumer; |
4
|
|
|
|
5
|
|
|
use Happyr\Mq2phpBundle\Event\PreHandleMessage; |
6
|
|
|
use SimpleBus\Serialization\Envelope\Serializer\MessageInEnvelopeSerializer; |
7
|
|
|
use SimpleBus\Message\Bus\MessageBus; |
8
|
|
|
use SimpleBus\Asynchronous\Consumer\SerializedEnvelopeConsumer as SimpleBusSerializedEnvelopeConsumer; |
9
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Use this consumer to easily implement an asynchronous message consumer. |
13
|
|
|
*/ |
14
|
|
|
class ExtendableEnvelopeConsumer implements SimpleBusSerializedEnvelopeConsumer |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var MessageInEnvelopeSerializer |
18
|
|
|
*/ |
19
|
|
|
private $messageInEnvelopeSerializer; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var MessageBus |
23
|
|
|
*/ |
24
|
|
|
private $messageBus; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var EventDispatcherInterface |
28
|
|
|
*/ |
29
|
|
|
private $dispathcer; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param MessageInEnvelopeSerializer $messageInEnvelopeSerializer |
33
|
|
|
* @param MessageBus $messageBus |
34
|
|
|
* @param EventDispatcherInterface $dispathcer |
35
|
|
|
*/ |
36
|
1 |
|
public function __construct( |
37
|
|
|
MessageInEnvelopeSerializer $messageInEnvelopeSerializer, |
38
|
|
|
MessageBus $messageBus, |
39
|
|
|
EventDispatcherInterface $dispathcer |
40
|
|
|
) { |
41
|
1 |
|
$this->messageInEnvelopeSerializer = $messageInEnvelopeSerializer; |
42
|
1 |
|
$this->messageBus = $messageBus; |
43
|
1 |
|
$this->dispathcer = $dispathcer; |
44
|
1 |
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param string $serializedEnvelope |
48
|
|
|
*/ |
49
|
|
|
public function consume($serializedEnvelope) |
50
|
|
|
{ |
51
|
|
|
// Unserialize |
52
|
|
|
$envelope = $this->messageInEnvelopeSerializer->unwrapAndDeserialize($serializedEnvelope); |
53
|
|
|
|
54
|
|
|
// Tell the world |
55
|
|
|
$this->dispathcer->dispatch(PreHandleMessage::NAME, new PreHandleMessage($envelope)); |
56
|
|
|
|
57
|
|
|
// Handle the message |
58
|
|
|
$this->messageBus->handle($envelope->message()); |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|