1
|
|
|
<?php |
2
|
|
|
namespace PSB\Core\Transport\RabbitMq; |
3
|
|
|
|
4
|
|
|
|
5
|
|
|
use PSB\Core\Exception\InvalidArgumentException; |
6
|
|
|
use PSB\Core\HeaderTypeEnum; |
7
|
|
|
use PSB\Core\Transport\OutgoingPhysicalMessage; |
8
|
|
|
|
9
|
|
|
class MessageConverter |
10
|
|
|
{ |
11
|
2 |
|
public function retrieveMessageId(\AMQPEnvelope $envelope) |
12
|
|
|
{ |
13
|
2 |
|
$messageId = $envelope->getMessageId(); |
14
|
2 |
|
if ($messageId === '' || $messageId === null) { |
15
|
1 |
|
throw new InvalidArgumentException( |
16
|
1 |
|
"A non empty message-id attribute is required when running PHPServiceBus on top of RabbitMq." |
17
|
|
|
); |
18
|
|
|
} |
19
|
|
|
|
20
|
1 |
|
return $messageId; |
21
|
|
|
} |
22
|
|
|
|
23
|
3 |
|
public function retrieveHeaders(\AMQPEnvelope $envelope) |
24
|
|
|
{ |
25
|
3 |
|
$headers = $envelope->getHeaders(); |
26
|
|
|
|
27
|
3 |
View Code Duplication |
if ($envelope->getReplyTo() !== '' && !isset($headers[HeaderTypeEnum::REPLY_TO_ADDRESS])) { |
28
|
1 |
|
$headers[HeaderTypeEnum::REPLY_TO_ADDRESS] = $envelope->getReplyTo(); |
29
|
|
|
} |
30
|
|
|
|
31
|
3 |
|
if ($envelope->getCorrelationId() !== '') { |
32
|
2 |
|
$headers[HeaderTypeEnum::CORRELATION_ID] = $envelope->getCorrelationId(); |
33
|
|
|
} |
34
|
|
|
|
35
|
3 |
View Code Duplication |
if ($envelope->getType() !== '' && !isset($headers[HeaderTypeEnum::ENCLOSED_CLASS])) { |
36
|
1 |
|
$headers[HeaderTypeEnum::ENCLOSED_CLASS] = $envelope->getType(); |
37
|
|
|
} |
38
|
|
|
|
39
|
3 |
|
return $headers; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param OutgoingPhysicalMessage $message |
44
|
|
|
* |
45
|
|
|
* @return array |
46
|
|
|
*/ |
47
|
5 |
|
public function composeRabbitMqAttributes(OutgoingPhysicalMessage $message) |
48
|
|
|
{ |
49
|
5 |
|
$headers = $message->getHeaders(); |
50
|
|
|
|
51
|
5 |
|
$attributes = []; |
52
|
5 |
|
$attributes['message_id'] = $message->getMessageId(); |
53
|
5 |
|
$attributes['headers'] = $headers; |
54
|
|
|
|
55
|
5 |
|
if (isset($headers[HeaderTypeEnum::CORRELATION_ID])) { |
56
|
1 |
|
$attributes['correlation_id'] = $headers[HeaderTypeEnum::CORRELATION_ID]; |
57
|
|
|
} |
58
|
|
|
|
59
|
5 |
|
if (isset($headers[HeaderTypeEnum::CONTENT_TYPE])) { |
60
|
1 |
|
$attributes['content_type'] = $headers[HeaderTypeEnum::CONTENT_TYPE]; |
61
|
|
|
} else { |
62
|
4 |
|
$attributes['content_type'] = 'application/octet-stream'; |
63
|
|
|
} |
64
|
|
|
|
65
|
5 |
|
if (isset($headers[HeaderTypeEnum::REPLY_TO_ADDRESS])) { |
66
|
1 |
|
$attributes['reply_to'] = $headers[HeaderTypeEnum::REPLY_TO_ADDRESS]; |
67
|
|
|
} |
68
|
|
|
|
69
|
5 |
|
if (isset($headers[HeaderTypeEnum::ENCLOSED_CLASS])) { |
70
|
1 |
|
$attributes['type'] = $headers[HeaderTypeEnum::ENCLOSED_CLASS]; |
71
|
|
|
} |
72
|
|
|
|
73
|
5 |
|
return $attributes; |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|