1
|
|
|
<?php |
2
|
|
|
declare(strict_types = 1); |
3
|
|
|
|
4
|
|
|
namespace Innmind\AMQP\Transport\Connection; |
5
|
|
|
|
6
|
|
|
use Innmind\AMQP\{ |
7
|
|
|
Model\Basic\Message, |
8
|
|
|
Model\Basic\Message\Generic, |
9
|
|
|
Model\Basic\Message\ContentType, |
10
|
|
|
Model\Basic\Message\ContentEncoding, |
11
|
|
|
Model\Basic\Message\DeliveryMode, |
12
|
|
|
Model\Basic\Message\Priority, |
13
|
|
|
Model\Basic\Message\CorrelationId, |
14
|
|
|
Model\Basic\Message\ReplyTo, |
15
|
|
|
Model\Basic\Message\Id, |
16
|
|
|
Model\Basic\Message\Type, |
17
|
|
|
Model\Basic\Message\UserId, |
18
|
|
|
Model\Basic\Message\AppId, |
19
|
|
|
Transport\Connection as ConnectionInterface, |
20
|
|
|
Transport\Frame\Value, |
21
|
|
|
}; |
22
|
|
|
use Innmind\TimeContinuum\ElapsedPeriod; |
23
|
|
|
use Innmind\Immutable\{ |
24
|
|
|
Map, |
25
|
|
|
Str, |
26
|
|
|
}; |
27
|
|
|
|
28
|
|
|
final class MessageReader |
29
|
|
|
{ |
30
|
26 |
|
public function __invoke(ConnectionInterface $connection): Message |
31
|
|
|
{ |
32
|
26 |
|
$header = $connection->wait(); |
33
|
|
|
$bodySize = $header |
34
|
26 |
|
->values() |
35
|
26 |
|
->first() |
36
|
26 |
|
->original() |
37
|
26 |
|
->value(); |
38
|
|
|
$flagBits = $header |
39
|
26 |
|
->values() |
40
|
26 |
|
->get(1) |
41
|
26 |
|
->original() |
42
|
26 |
|
->value(); |
43
|
26 |
|
$payload = new Str(''); |
44
|
|
|
|
45
|
26 |
|
while ($payload->length() !== $bodySize) { |
46
|
26 |
|
$payload = $payload->append( |
47
|
|
|
(string) $connection |
48
|
26 |
|
->wait() |
49
|
26 |
|
->values() |
50
|
26 |
|
->first() |
51
|
26 |
|
->original() |
52
|
|
|
); |
53
|
|
|
} |
54
|
|
|
|
55
|
26 |
|
$message = new Generic($payload); |
56
|
|
|
$properties = $header |
57
|
26 |
|
->values() |
58
|
26 |
|
->drop(2); |
59
|
|
|
|
60
|
26 |
|
if ($flagBits & (1 << 15)) { |
61
|
2 |
|
[$topLevel, $subType] = explode( |
62
|
2 |
|
'/', |
63
|
2 |
|
(string) $properties->first()->original() |
64
|
|
|
); |
65
|
2 |
|
$message = $message->withContentType(new ContentType( |
66
|
2 |
|
$topLevel, |
67
|
1 |
|
$subType |
68
|
|
|
)); |
69
|
2 |
|
$properties = $properties->drop(1); |
70
|
|
|
} |
71
|
|
|
|
72
|
26 |
|
if ($flagBits & (1 << 14)) { |
73
|
2 |
|
$message = $message->withContentEncoding(new ContentEncoding( |
74
|
2 |
|
(string) $properties->first()->original() |
75
|
|
|
)); |
76
|
2 |
|
$properties = $properties->drop(1); |
77
|
|
|
} |
78
|
|
|
|
79
|
26 |
|
if ($flagBits & (1 << 13)) { |
80
|
2 |
|
$message = $message->withHeaders( |
81
|
|
|
$properties |
82
|
2 |
|
->first() |
83
|
2 |
|
->original() |
84
|
2 |
|
->reduce( |
85
|
2 |
|
new Map('string', 'mixed'), |
86
|
|
|
static function(Map $carry, string $key, Value $value): Map { |
87
|
2 |
|
return $carry->put( |
88
|
2 |
|
$key, |
89
|
2 |
|
$value->original() |
90
|
|
|
); |
91
|
2 |
|
} |
92
|
|
|
) |
93
|
|
|
); |
94
|
2 |
|
$properties = $properties->drop(1); |
95
|
|
|
} |
96
|
|
|
|
97
|
26 |
|
if ($flagBits & (1 << 12)) { |
98
|
2 |
|
$message = $message->withDeliveryMode( |
99
|
2 |
|
$properties->first()->original()->value() === DeliveryMode::persistent()->toInt() ? |
100
|
2 |
|
DeliveryMode::persistent() : DeliveryMode::nonPersistent() |
101
|
|
|
); |
102
|
2 |
|
$properties = $properties->drop(1); |
103
|
|
|
} |
104
|
|
|
|
105
|
26 |
|
if ($flagBits & (1 << 11)) { |
106
|
2 |
|
$message = $message->withPriority(new Priority( |
107
|
2 |
|
$properties->first()->original()->value() |
108
|
|
|
)); |
109
|
2 |
|
$properties = $properties->drop(1); |
110
|
|
|
} |
111
|
|
|
|
112
|
26 |
|
if ($flagBits & (1 << 10)) { |
113
|
2 |
|
$message = $message->withCorrelationId(new CorrelationId( |
114
|
2 |
|
(string) $properties->first()->original() |
115
|
|
|
)); |
116
|
2 |
|
$properties = $properties->drop(1); |
117
|
|
|
} |
118
|
|
|
|
119
|
26 |
|
if ($flagBits & (1 << 9)) { |
120
|
2 |
|
$message = $message->withReplyTo(new ReplyTo( |
121
|
2 |
|
(string) $properties->first()->original() |
122
|
|
|
)); |
123
|
2 |
|
$properties = $properties->drop(1); |
124
|
|
|
} |
125
|
|
|
|
126
|
26 |
|
if ($flagBits & (1 << 8)) { |
127
|
2 |
|
$message = $message->withExpiration(new ElapsedPeriod( |
128
|
2 |
|
(int) (string) $properties->first()->original() |
129
|
|
|
)); |
130
|
2 |
|
$properties = $properties->drop(1); |
131
|
|
|
} |
132
|
|
|
|
133
|
26 |
|
if ($flagBits & (1 << 7)) { |
134
|
2 |
|
$message = $message->withId(new Id( |
135
|
2 |
|
(string) $properties->first()->original() |
136
|
|
|
)); |
137
|
2 |
|
$properties = $properties->drop(1); |
138
|
|
|
} |
139
|
|
|
|
140
|
26 |
|
if ($flagBits & (1 << 6)) { |
141
|
2 |
|
$message = $message->withTimestamp( |
142
|
2 |
|
$properties->first()->original() |
143
|
|
|
); |
144
|
2 |
|
$properties = $properties->drop(1); |
145
|
|
|
} |
146
|
|
|
|
147
|
26 |
|
if ($flagBits & (1 << 5)) { |
148
|
2 |
|
$message = $message->withType(new Type( |
149
|
2 |
|
(string) $properties->first()->original() |
150
|
|
|
)); |
151
|
2 |
|
$properties = $properties->drop(1); |
152
|
|
|
} |
153
|
|
|
|
154
|
26 |
|
if ($flagBits & (1 << 4)) { |
155
|
2 |
|
$message = $message->withUserId(new UserId( |
156
|
2 |
|
(string) $properties->first()->original() |
157
|
|
|
)); |
158
|
2 |
|
$properties = $properties->drop(1); |
159
|
|
|
} |
160
|
|
|
|
161
|
26 |
|
if ($flagBits & (1 << 3)) { |
162
|
2 |
|
$message = $message->withAppId(new AppId( |
163
|
2 |
|
(string) $properties->first()->original() |
164
|
|
|
)); |
165
|
2 |
|
$properties = $properties->drop(1); |
|
|
|
|
166
|
|
|
} |
167
|
|
|
|
168
|
26 |
|
return $message; |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
|