|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Happyr\Mq2phpBundle\Service; |
|
4
|
|
|
|
|
5
|
|
|
use Happyr\Mq2phpBundle\Event\PrePublishMessage; |
|
6
|
|
|
use SimpleBus\Serialization\Envelope\Serializer\MessageInEnvelopSerializer; |
|
7
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* This service adds some extra headers on the message envelope. |
|
11
|
|
|
* |
|
12
|
|
|
* @author Tobias Nyholm <[email protected]> |
|
13
|
|
|
*/ |
|
14
|
|
|
class MessageSerializerDecorator implements MessageInEnvelopSerializer, HeaderAwareInterface |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* @var MessageInEnvelopSerializer |
|
18
|
|
|
*/ |
|
19
|
|
|
private $serializer; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var array |
|
23
|
|
|
*/ |
|
24
|
|
|
private $headers; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var string |
|
28
|
|
|
*/ |
|
29
|
|
|
private $secretKey; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var EventDispatcherInterface |
|
33
|
|
|
*/ |
|
34
|
|
|
private $eventDispatcher; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @param MessageInEnvelopSerializer $serializer |
|
38
|
|
|
* @param array $headers |
|
39
|
|
|
* @param string $secretKey |
|
40
|
|
|
* @param EventDispatcherInterface $eventDispatcher |
|
41
|
|
|
*/ |
|
42
|
|
|
public function __construct( |
|
43
|
1 |
|
MessageInEnvelopSerializer $serializer, |
|
44
|
|
|
EventDispatcherInterface $eventDispatcher, |
|
45
|
|
|
array $headers = [], |
|
46
|
|
|
$secretKey = null |
|
47
|
|
|
) { |
|
48
|
|
|
$this->serializer = $serializer; |
|
49
|
1 |
|
$this->eventDispatcher = $eventDispatcher; |
|
50
|
1 |
|
$this->headers = $headers; |
|
51
|
1 |
|
$this->secretKey = empty($secretKey) ? '' : $secretKey; |
|
52
|
1 |
|
} |
|
53
|
1 |
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Serialize a Message by wrapping it in an Envelope and serializing the envelope. This decoration will |
|
56
|
|
|
* take the SimpleBus envelope and add it in a json message. |
|
57
|
|
|
* |
|
58
|
|
|
* {@inheritdoc} |
|
59
|
|
|
*/ |
|
60
|
|
|
public function wrapAndSerialize($originalMessage) |
|
61
|
1 |
|
{ |
|
62
|
|
|
$serializedMessage = $this->serializer->wrapAndSerialize($originalMessage); |
|
63
|
1 |
|
|
|
64
|
|
|
$message = []; |
|
65
|
1 |
|
foreach ($this->headers as $name => $value) { |
|
66
|
1 |
|
if (empty($value)) { |
|
67
|
1 |
|
continue; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
$message['headers'][] = ['key' => $name, 'value' => $value]; |
|
71
|
1 |
|
} |
|
72
|
1 |
|
$message['body'] = $serializedMessage; |
|
73
|
1 |
|
|
|
74
|
|
|
// Add a hash where the secret key is baked in. |
|
75
|
|
|
$message['headers'][] = ['key' => 'hash', 'value' => sha1($this->secretKey.$serializedMessage)]; |
|
76
|
1 |
|
|
|
77
|
|
|
$event = new PrePublishMessage($message, is_object($originalMessage) ? get_class($originalMessage) : gettype($originalMessage)); |
|
78
|
1 |
|
$this->eventDispatcher->dispatch(PrePublishMessage::NAME, $event); |
|
79
|
1 |
|
|
|
80
|
|
|
return json_encode($event->getMessage()); |
|
81
|
1 |
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Deserialize a Message that was wrapped in an Envelope. |
|
85
|
|
|
* |
|
86
|
|
|
* {@inheritdoc} |
|
87
|
|
|
*/ |
|
88
|
|
|
public function unwrapAndDeserialize($serializedEnvelope) |
|
89
|
|
|
{ |
|
90
|
|
|
return $this->serializer->unwrapAndDeserialize($serializedEnvelope); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @param string $name |
|
95
|
|
|
* @param mixed $value |
|
96
|
|
|
* |
|
97
|
|
|
* @return $this |
|
98
|
|
|
*/ |
|
99
|
|
|
public function setHeader($name, $value) |
|
100
|
|
|
{ |
|
101
|
|
|
$this->headers[$name] = $value; |
|
102
|
|
|
|
|
103
|
|
|
return $this; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* @param string $name |
|
108
|
|
|
*/ |
|
109
|
|
|
public function getHeader($name) |
|
110
|
|
|
{ |
|
111
|
|
|
if (isset($this->headers[$name])) { |
|
112
|
|
|
return $this->headers[$name]; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
return; |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|