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