|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Puzzle\AMQP\Workers; |
|
4
|
|
|
|
|
5
|
|
|
use Psr\Log\InvalidArgumentException; |
|
6
|
|
|
use Puzzle\AMQP\ReadableMessage; |
|
7
|
|
|
use Puzzle\AMQP\Collections\MessageHookCollection; |
|
8
|
|
|
use Puzzle\AMQP\Hooks\MessageHook; |
|
9
|
|
|
use Puzzle\AMQP\Messages\ContentType; |
|
10
|
|
|
|
|
11
|
|
|
class MessageAdapter implements ReadableMessage |
|
12
|
|
|
{ |
|
13
|
|
|
private |
|
14
|
|
|
$message, |
|
|
|
|
|
|
15
|
|
|
$decodedBody, |
|
16
|
|
|
$messageHooks; |
|
|
|
|
|
|
17
|
|
|
|
|
18
|
|
|
public function __construct(\Swarrot\Broker\Message $message) |
|
19
|
|
|
{ |
|
20
|
|
|
$this->message = $message; |
|
21
|
|
|
$this->decodedBody = $this->decodeBody(); |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
public function getRoutingKey() |
|
25
|
|
|
{ |
|
26
|
|
|
return $this->getAttribute('routing_key'); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
public function getContentType() |
|
30
|
|
|
{ |
|
31
|
|
|
return $this->getAttribute('content_type'); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
public function getAppId() |
|
35
|
|
|
{ |
|
36
|
|
|
return $this->getAttribute('app_id'); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
public function getHeaders() |
|
40
|
|
|
{ |
|
41
|
|
|
return $this->getAttribute('headers'); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
public function getDecodedBody() |
|
45
|
|
|
{ |
|
46
|
|
|
return $this->decodedBody; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
private function decodeBody() |
|
50
|
|
|
{ |
|
51
|
|
|
$callable = $this->getFormatterStrategy($this->getContentType()); |
|
52
|
|
|
$body = $this->getRawBody(); |
|
53
|
|
|
|
|
54
|
|
|
if($callable instanceof \Closure) |
|
55
|
|
|
{ |
|
56
|
|
|
$body = $callable($body); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
return $body; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
public function getRawBody() |
|
63
|
|
|
{ |
|
64
|
|
|
return $this->message->getBody(); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
public function applyHooks(MessageHookCollection $messageHookCollection) |
|
68
|
|
|
{ |
|
69
|
|
|
if(!empty($messageHookCollection)) |
|
70
|
|
|
{ |
|
71
|
|
|
foreach($messageHookCollection as $messageHook) |
|
72
|
|
|
{ |
|
73
|
|
|
if($messageHook instanceof MessageHook) |
|
74
|
|
|
{ |
|
75
|
|
|
$this->decodedBody = $messageHook->process($this->decodedBody); |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
public function getFlags() |
|
82
|
|
|
{ |
|
83
|
|
|
throw new \LogicException('Consumed messages have no flags'); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
public function getAttribute($attributeName) |
|
87
|
|
|
{ |
|
88
|
|
|
$messageProperties = $this->message->getProperties(); |
|
89
|
|
|
if(array_key_exists($attributeName, $messageProperties)) |
|
90
|
|
|
{ |
|
91
|
|
|
return $messageProperties[$attributeName]; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
throw new InvalidArgumentException(sprintf('Property "%s" is unknown or is not a message property', $attributeName)); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
private function getFormatterStrategy($contentType) |
|
98
|
|
|
{ |
|
99
|
|
|
$formatterStrategies = array( |
|
100
|
|
|
ContentType::JSON => function($body) { |
|
101
|
|
|
return json_decode($body, true); |
|
102
|
|
|
}, |
|
103
|
|
|
); |
|
104
|
|
|
|
|
105
|
|
|
if(array_key_exists($contentType, $formatterStrategies) === true) |
|
106
|
|
|
{ |
|
107
|
|
|
return $formatterStrategies[$contentType]; |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
public function __toString() |
|
112
|
|
|
{ |
|
113
|
|
|
return json_encode(array( |
|
114
|
|
|
'routing_key' => $this->getRoutingKey(), |
|
115
|
|
|
'body' => $this->bodyToString(), |
|
116
|
|
|
'attributes' => $this->message->getProperties(), |
|
117
|
|
|
)); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
private function isBinary() |
|
121
|
|
|
{ |
|
122
|
|
|
$this->getContentType() === ContentType::BINARY; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
private function bodyToString() |
|
126
|
|
|
{ |
|
127
|
|
|
if($this->isBinary()) |
|
128
|
|
|
{ |
|
129
|
|
|
return sprintf('<binary stream of %d bytes>', strlen($this->getRawBody())); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
return $this->getRawBody(); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
public function getService() |
|
136
|
|
|
{ |
|
137
|
|
|
return $this->getHeader('service'); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
public function getAction() |
|
141
|
|
|
{ |
|
142
|
|
|
return $this->getHeader('action'); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
public function getAuthor() |
|
146
|
|
|
{ |
|
147
|
|
|
return $this->getHeader('author'); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
private function getHeader($headerName) |
|
151
|
|
|
{ |
|
152
|
|
|
$headers = $this->getHeaders(); |
|
153
|
|
|
if(array_key_exists($headerName, $headers)) |
|
154
|
|
|
{ |
|
155
|
|
|
return $headers[$headerName]; |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
return null; |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
public function getAttributes() |
|
162
|
|
|
{ |
|
163
|
|
|
return $this->message->getProperties(); |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
public function isLastRetry($retryOccurence = \Puzzle\AMQP\Consumers\Retry::DEFAULT_RETRY_OCCURENCE) |
|
167
|
|
|
{ |
|
168
|
|
|
$retryHeader = $this->getHeader(\Puzzle\AMQP\Consumers\Retry::DEFAULT_RETRY_HEADER); |
|
169
|
|
|
|
|
170
|
|
|
return (!empty($retryHeader) && (int) $retryHeader === $retryOccurence); |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
View Code Duplication |
public function getRoutingKeyFromHeader() |
|
|
|
|
|
|
174
|
|
|
{ |
|
175
|
|
|
$headers = $this->getHeaders(); |
|
176
|
|
|
|
|
177
|
|
|
if(! array_key_exists('routing_key', $headers)) |
|
178
|
|
|
{ |
|
179
|
|
|
return null; |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
return $headers['routing_key']; |
|
183
|
|
|
} |
|
184
|
|
|
} |
|
185
|
|
|
|
The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using
the property is implicitly global.
To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.