1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Puzzle\AMQP\Workers; |
4
|
|
|
|
5
|
|
|
use Puzzle\AMQP\ReadableMessage; |
6
|
|
|
use Puzzle\AMQP\Messages\BodyFactory; |
7
|
|
|
|
8
|
|
|
class MessageAdapter implements ReadableMessage |
9
|
|
|
{ |
10
|
|
|
private |
11
|
|
|
$message, |
|
|
|
|
12
|
|
|
$body; |
13
|
|
|
|
14
|
21 |
|
public function __construct(\Swarrot\Broker\Message $message) |
15
|
|
|
{ |
16
|
21 |
|
$this->message = $message; |
17
|
|
|
|
18
|
21 |
|
$this->body = (new BodyFactory())->create( |
19
|
21 |
|
$this->getContentType(), |
20
|
20 |
|
$message->getBody() |
21
|
20 |
|
); |
22
|
20 |
|
} |
23
|
|
|
|
24
|
8 |
|
public function getRoutingKey() |
25
|
|
|
{ |
26
|
8 |
|
return $this->getAttribute('routing_key'); |
27
|
|
|
} |
28
|
|
|
|
29
|
21 |
|
public function getContentType() |
30
|
|
|
{ |
31
|
21 |
|
return $this->getAttribute('content_type'); |
32
|
|
|
} |
33
|
|
|
|
34
|
1 |
|
public function getAppId() |
35
|
|
|
{ |
36
|
1 |
|
return $this->getAttribute('app_id'); |
37
|
|
|
} |
38
|
|
|
|
39
|
16 |
|
public function getHeaders() |
40
|
|
|
{ |
41
|
16 |
|
return $this->getAttribute('headers'); |
|
|
|
|
42
|
|
|
} |
43
|
|
|
|
44
|
4 |
|
public function getBody() |
45
|
|
|
{ |
46
|
4 |
|
return $this->body; |
47
|
|
|
} |
48
|
|
|
|
49
|
2 |
|
public function getBodyInOriginalFormat() |
50
|
|
|
{ |
51
|
2 |
|
return $this->body->inOriginalFormat(); |
52
|
|
|
} |
53
|
|
|
|
54
|
21 |
|
public function getAttribute($attributeName) |
55
|
|
|
{ |
56
|
21 |
|
$messageProperties = $this->message->getProperties(); |
57
|
21 |
|
if(array_key_exists($attributeName, $messageProperties)) |
58
|
21 |
|
{ |
59
|
20 |
|
return $messageProperties[$attributeName]; |
60
|
|
|
} |
61
|
|
|
|
62
|
2 |
|
throw new \InvalidArgumentException(sprintf('Property "%s" is unknown or is not a message property', $attributeName)); |
63
|
|
|
} |
64
|
|
|
|
65
|
3 |
|
public function __toString() |
66
|
|
|
{ |
67
|
3 |
|
return json_encode(array( |
68
|
3 |
|
'routing_key' => $this->getRoutingKey(), |
69
|
3 |
|
'body' => (string) $this->body, |
70
|
3 |
|
'attributes' => $this->message->getProperties(), |
71
|
3 |
|
)); |
72
|
|
|
} |
73
|
|
|
|
74
|
14 |
|
private function getHeader($headerName) |
75
|
|
|
{ |
76
|
14 |
|
$headers = $this->getHeaders(); |
77
|
14 |
|
if(array_key_exists($headerName, $headers)) |
78
|
14 |
|
{ |
79
|
12 |
|
return $headers[$headerName]; |
80
|
|
|
} |
81
|
|
|
|
82
|
2 |
|
return null; |
83
|
|
|
} |
84
|
|
|
|
85
|
3 |
|
public function getAttributes() |
86
|
|
|
{ |
87
|
3 |
|
return $this->message->getProperties(); |
88
|
|
|
} |
89
|
|
|
|
90
|
11 |
|
public function isLastRetry($retryOccurence = \Puzzle\AMQP\Consumers\Retry::DEFAULT_RETRY_OCCURENCE) |
91
|
|
|
{ |
92
|
11 |
|
$retryHeader = $this->getHeader(\Puzzle\AMQP\Consumers\Retry::DEFAULT_RETRY_HEADER); |
93
|
|
|
|
94
|
11 |
|
return ($retryHeader !== null && (int) $retryHeader >= $retryOccurence); |
95
|
|
|
} |
96
|
|
|
|
97
|
3 |
|
public function getRoutingKeyFromHeader() |
98
|
|
|
{ |
99
|
3 |
|
return $this->getHeader('routing_key'); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
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.