|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Puzzle\AMQP\Workers; |
|
4
|
|
|
|
|
5
|
|
|
use Puzzle\AMQP\ReadableMessage; |
|
6
|
|
|
use Puzzle\AMQP\Messages\Body; |
|
7
|
|
|
|
|
8
|
|
|
class ReadableMessageModifier |
|
9
|
|
|
{ |
|
10
|
|
|
private |
|
11
|
|
|
$originalMessage, |
|
12
|
|
|
$newRoutingKey, |
|
13
|
|
|
$newBody, |
|
14
|
|
|
$newAttributeValues, |
|
15
|
|
|
$newHeaders, |
|
16
|
|
|
$droppedHeaders; |
|
17
|
|
|
|
|
18
|
5 |
|
public function __construct(ReadableMessage $originalMessage) |
|
19
|
|
|
{ |
|
20
|
5 |
|
$this->originalMessage = $originalMessage; |
|
21
|
|
|
|
|
22
|
5 |
|
$this->newRoutingKey= $originalMessage->getRoutingKey(); |
|
23
|
5 |
|
$this->newBody = null; |
|
24
|
|
|
|
|
25
|
5 |
|
$this->newAttributeValues = []; |
|
26
|
5 |
|
$this->newHeaders = []; |
|
27
|
5 |
|
$this->droppedHeaders = []; |
|
28
|
5 |
|
} |
|
29
|
|
|
|
|
30
|
1 |
|
public function changeRoutingKey($newRoutingKey) |
|
31
|
|
|
{ |
|
32
|
1 |
|
$this->newRoutingKey = $newRoutingKey; |
|
33
|
|
|
|
|
34
|
1 |
|
return $this; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
3 |
|
public function changeBody(Body $newBody) |
|
38
|
|
|
{ |
|
39
|
3 |
|
$this->newBody = $newBody; |
|
40
|
|
|
|
|
41
|
3 |
|
return $this; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
1 |
|
public function changeAttribute($name, $value) |
|
45
|
|
|
{ |
|
46
|
1 |
|
$this->newAttributeValues[$name] = $value; |
|
47
|
|
|
|
|
48
|
1 |
|
return $this; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
1 |
|
public function addHeader($headerName, $value) |
|
52
|
|
|
{ |
|
53
|
1 |
|
$this->newHeaders[$headerName] = $value; |
|
54
|
|
|
|
|
55
|
1 |
|
return $this; |
|
56
|
|
|
} |
|
57
|
2 |
|
public function dropHeader($headerName) |
|
58
|
|
|
{ |
|
59
|
2 |
|
$this->droppedHeaders[] = $headerName; |
|
60
|
|
|
|
|
61
|
2 |
|
return $this; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
5 |
|
public function build() |
|
65
|
|
|
{ |
|
66
|
5 |
|
$properties = $this->buildAttributes( |
|
67
|
5 |
|
$this->originalMessage->getAttributes() |
|
68
|
5 |
|
); |
|
69
|
|
|
|
|
70
|
5 |
|
$properties['headers'] = $this->buildHeaders( |
|
71
|
5 |
|
$this->extractHeaders($properties) |
|
72
|
5 |
|
); |
|
73
|
|
|
|
|
74
|
5 |
|
return new MessageAdapter( |
|
75
|
5 |
|
new \Swarrot\Broker\Message($this->buildBody(), $properties) |
|
76
|
5 |
|
); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
5 |
|
private function extractHeaders(array $properties) |
|
80
|
|
|
{ |
|
81
|
5 |
|
$headers = []; |
|
82
|
|
|
|
|
83
|
5 |
|
if(isset($properties['headers'])) |
|
84
|
5 |
|
{ |
|
85
|
5 |
|
$headers = $properties['headers']; |
|
86
|
5 |
|
} |
|
87
|
|
|
|
|
88
|
5 |
|
return $headers; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
5 |
|
private function buildBody() |
|
92
|
|
|
{ |
|
93
|
5 |
|
if($this->newBody instanceof Body) |
|
94
|
5 |
|
{ |
|
95
|
3 |
|
return $this->newBody->asTransported(); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
2 |
|
return $this->originalMessage->getBodyAsTransported(); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
5 |
|
private function buildAttributes(array $attributes) |
|
102
|
|
|
{ |
|
103
|
5 |
|
if($this->newBody instanceof Body) |
|
104
|
5 |
|
{ |
|
105
|
3 |
|
$attributes['content_type'] = $this->newBody->getContentType(); |
|
106
|
3 |
|
} |
|
107
|
|
|
|
|
108
|
5 |
|
$attributes['routing_key'] = $this->newRoutingKey; |
|
109
|
|
|
|
|
110
|
5 |
|
foreach($this->newAttributeValues as $name => $value) |
|
111
|
|
|
{ |
|
112
|
1 |
|
if(isset($attributes[$name])) |
|
113
|
1 |
|
{ |
|
114
|
1 |
|
$attributes[$name] = $value; |
|
115
|
1 |
|
} |
|
116
|
5 |
|
} |
|
117
|
|
|
|
|
118
|
5 |
|
return $attributes; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
5 |
|
private function buildHeaders(array $headers) |
|
122
|
|
|
{ |
|
123
|
5 |
|
foreach($this->droppedHeaders as $droppedHeader) |
|
124
|
|
|
{ |
|
125
|
2 |
|
if(isset($headers[$droppedHeader])) |
|
126
|
2 |
|
{ |
|
127
|
2 |
|
unset($headers[$droppedHeader]); |
|
128
|
2 |
|
} |
|
129
|
5 |
|
} |
|
130
|
|
|
|
|
131
|
5 |
|
foreach($this->newHeaders as $name => $value) |
|
132
|
|
|
{ |
|
133
|
1 |
|
$headers[$name] = $value; |
|
134
|
5 |
|
} |
|
135
|
|
|
|
|
136
|
5 |
|
return $headers; |
|
137
|
|
|
} |
|
138
|
|
|
} |
|
139
|
|
|
|