1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Puzzle\AMQP\Workers; |
4
|
|
|
|
5
|
|
|
use Puzzle\AMQP\ReadableMessage; |
6
|
|
|
use Puzzle\AMQP\WritableMessage; |
7
|
|
|
use Puzzle\AMQP\Messages\Bodies\NullBody; |
8
|
|
|
use Puzzle\AMQP\Messages\Body; |
9
|
|
|
|
10
|
|
|
class MessageAdapter implements ReadableMessage |
11
|
|
|
{ |
12
|
|
|
private |
13
|
|
|
$message, |
14
|
|
|
$body; |
15
|
|
|
|
16
|
31 |
|
public function __construct(\Swarrot\Broker\Message $message) |
17
|
|
|
{ |
18
|
31 |
|
$this->message = $message; |
19
|
31 |
|
$this->body = new NullBody(); |
20
|
31 |
|
} |
21
|
|
|
|
22
|
19 |
|
public function setBody(Body $body) |
23
|
|
|
{ |
24
|
19 |
|
$this->body = $body; |
25
|
19 |
|
} |
26
|
|
|
|
27
|
13 |
|
public function getRoutingKey() |
28
|
|
|
{ |
29
|
13 |
|
return $this->getAttribute('routing_key'); |
30
|
|
|
} |
31
|
|
|
|
32
|
20 |
|
public function getContentType() |
33
|
|
|
{ |
34
|
20 |
|
return $this->getAttribute('content_type'); |
35
|
|
|
} |
36
|
|
|
|
37
|
1 |
|
public function getAppId() |
38
|
|
|
{ |
39
|
1 |
|
return $this->getAttribute('app_id'); |
40
|
|
|
} |
41
|
|
|
|
42
|
23 |
|
public function getHeaders() |
43
|
|
|
{ |
44
|
23 |
|
return $this->getAttribute('headers'); |
45
|
|
|
} |
46
|
|
|
|
47
|
11 |
|
public function getBodyInOriginalFormat() |
48
|
|
|
{ |
49
|
11 |
|
return $this->body->inOriginalFormat(); |
50
|
|
|
} |
51
|
|
|
|
52
|
2 |
|
public function getBodyAsTransported() |
53
|
|
|
{ |
54
|
2 |
|
return $this->message->getBody(); |
55
|
|
|
} |
56
|
|
|
|
57
|
31 |
|
public function getAttribute($attributeName) |
58
|
|
|
{ |
59
|
31 |
|
$messageProperties = $this->message->getProperties(); |
60
|
31 |
|
if(array_key_exists($attributeName, $messageProperties)) |
61
|
31 |
|
{ |
62
|
30 |
|
return $messageProperties[$attributeName]; |
63
|
|
|
} |
64
|
|
|
|
65
|
2 |
|
throw new \InvalidArgumentException(sprintf('Property "%s" is unknown or is not a message property', $attributeName)); |
66
|
|
|
} |
67
|
|
|
|
68
|
5 |
|
public function __toString() |
69
|
|
|
{ |
70
|
5 |
|
return json_encode(array( |
71
|
5 |
|
'routing_key' => $this->getRoutingKey(), |
72
|
5 |
|
'body' => (string) $this->body, |
73
|
5 |
|
'attributes' => $this->message->getProperties(), |
74
|
5 |
|
)); |
75
|
|
|
} |
76
|
|
|
|
77
|
15 |
|
private function getHeader($headerName) |
78
|
|
|
{ |
79
|
15 |
|
$headers = $this->getHeaders(); |
80
|
15 |
|
if(array_key_exists($headerName, $headers)) |
81
|
15 |
|
{ |
82
|
13 |
|
return $headers[$headerName]; |
83
|
|
|
} |
84
|
|
|
|
85
|
2 |
|
return null; |
86
|
|
|
} |
87
|
|
|
|
88
|
8 |
|
public function getAttributes() |
89
|
|
|
{ |
90
|
8 |
|
return $this->message->getProperties(); |
91
|
|
|
} |
92
|
|
|
|
93
|
11 |
|
public function isLastRetry($retryOccurence = \Puzzle\AMQP\Consumers\Retry::DEFAULT_RETRY_OCCURENCE) |
94
|
|
|
{ |
95
|
11 |
|
$retryHeader = $this->getHeader(\Puzzle\AMQP\Consumers\Retry::DEFAULT_RETRY_HEADER); |
96
|
|
|
|
97
|
11 |
|
return ($retryHeader !== null && (int) $retryHeader >= $retryOccurence); |
98
|
|
|
} |
99
|
|
|
|
100
|
4 |
|
public function getRoutingKeyFromHeader() |
101
|
|
|
{ |
102
|
4 |
|
return $this->getHeader('routing_key'); |
103
|
|
|
} |
104
|
|
|
|
105
|
2 |
|
public function cloneIntoWritableMessage(WritableMessage $writable, $copyRoutingKey = false) |
106
|
|
|
{ |
107
|
2 |
|
if($copyRoutingKey === true) |
108
|
2 |
|
{ |
109
|
1 |
|
$writable->changeRoutingKey($this->getRoutingKey()); |
110
|
1 |
|
} |
111
|
|
|
|
112
|
2 |
|
$writable->setBody($this->body); |
113
|
2 |
|
$writable->addHeaders($this->getHeaders()); |
114
|
|
|
|
115
|
2 |
|
$attributes = $this->getAttributes(); |
116
|
2 |
|
$skippedAttributes = array('timestamp', 'headers', 'app_id', 'routing_key'); |
117
|
2 |
|
foreach($attributes as $attributeName => $value) |
118
|
|
|
{ |
119
|
2 |
|
if(! in_array($attributeName, $skippedAttributes)) |
120
|
2 |
|
{ |
121
|
2 |
|
$writable->setAttribute($attributeName, $value); |
122
|
2 |
|
} |
123
|
2 |
|
} |
124
|
|
|
|
125
|
2 |
|
return $writable; |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|