1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Mouf\AmqpClient\Objects; |
4
|
|
|
|
5
|
|
|
use PhpAmqpLib\Message\AMQPMessage; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* A message sent to RabbitMQ. |
9
|
|
|
* |
10
|
|
|
* Note: by default, those message are set to "persistent delivery mode" |
11
|
|
|
*/ |
12
|
|
|
class Message |
13
|
|
|
{ |
14
|
|
|
private $body; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var string |
18
|
|
|
*/ |
19
|
|
|
private $content_type; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
private $content_encoding; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var array |
28
|
|
|
*/ |
29
|
|
|
private $application_headers; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var int |
33
|
|
|
*/ |
34
|
|
|
private $delivery_mode = AMQPMessage::DELIVERY_MODE_PERSISTENT; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var int |
38
|
|
|
*/ |
39
|
|
|
private $priority; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var string |
43
|
|
|
*/ |
44
|
|
|
private $correlation_id; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var string |
48
|
|
|
*/ |
49
|
|
|
private $reply_to; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @var string |
53
|
|
|
*/ |
54
|
|
|
private $expiration; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @var string |
58
|
|
|
*/ |
59
|
|
|
private $message_id; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @var \DateTimeInterface |
63
|
|
|
*/ |
64
|
|
|
private $timestamp; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @var string |
68
|
|
|
*/ |
69
|
|
|
private $type; |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @var string |
73
|
|
|
*/ |
74
|
|
|
private $user_id; |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @var string |
78
|
|
|
*/ |
79
|
|
|
private $app_id; |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @var string |
83
|
|
|
*/ |
84
|
|
|
private $cluster_id; |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Message constructor. |
88
|
|
|
* |
89
|
|
|
* @param string $body |
90
|
|
|
*/ |
91
|
|
|
public function __construct($body) |
92
|
|
|
{ |
93
|
|
|
$this->body = $body; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @return string |
98
|
|
|
*/ |
99
|
|
|
public function getBody() |
100
|
|
|
{ |
101
|
|
|
return $this->body; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @return string |
106
|
|
|
*/ |
107
|
|
|
public function getContentType() |
108
|
|
|
{ |
109
|
|
|
return $this->content_type; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @param string $content_type |
114
|
|
|
* |
115
|
|
|
* @return Message |
116
|
|
|
*/ |
117
|
|
|
public function setContentType($content_type) |
118
|
|
|
{ |
119
|
|
|
$this->content_type = $content_type; |
120
|
|
|
|
121
|
|
|
return $this; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @return string |
126
|
|
|
*/ |
127
|
|
|
public function getContentEncoding() |
128
|
|
|
{ |
129
|
|
|
return $this->content_encoding; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @param string $content_encoding |
134
|
|
|
* |
135
|
|
|
* @return Message |
136
|
|
|
*/ |
137
|
|
|
public function setContentEncoding($content_encoding) |
138
|
|
|
{ |
139
|
|
|
$this->content_encoding = $content_encoding; |
140
|
|
|
|
141
|
|
|
return $this; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @return array |
146
|
|
|
*/ |
147
|
|
|
public function getApplicationHeaders() |
148
|
|
|
{ |
149
|
|
|
return $this->application_headers; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* @param array $application_headers |
154
|
|
|
* |
155
|
|
|
* @return Message |
156
|
|
|
*/ |
157
|
|
|
public function setApplicationHeaders($application_headers) |
158
|
|
|
{ |
159
|
|
|
$this->application_headers = $application_headers; |
160
|
|
|
|
161
|
|
|
return $this; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* @return int |
166
|
|
|
*/ |
167
|
|
|
public function getDeliveryMode() |
168
|
|
|
{ |
169
|
|
|
return $this->delivery_mode; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* @param int $delivery_mode |
174
|
|
|
* |
175
|
|
|
* @return Message |
176
|
|
|
*/ |
177
|
|
|
public function setDeliveryMode($delivery_mode) |
178
|
|
|
{ |
179
|
|
|
$this->delivery_mode = $delivery_mode; |
180
|
|
|
|
181
|
|
|
return $this; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* @return int |
186
|
|
|
*/ |
187
|
|
|
public function getPriority() |
188
|
|
|
{ |
189
|
|
|
return $this->priority; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* @param int $priority |
194
|
|
|
* |
195
|
|
|
* @return Message |
196
|
|
|
*/ |
197
|
|
|
public function setPriority($priority) |
198
|
|
|
{ |
199
|
|
|
$this->priority = $priority; |
200
|
|
|
|
201
|
|
|
return $this; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* @return string |
206
|
|
|
*/ |
207
|
|
|
public function getCorrelationId() |
208
|
|
|
{ |
209
|
|
|
return $this->correlation_id; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* @param string $correlation_id |
214
|
|
|
* |
215
|
|
|
* @return Message |
216
|
|
|
*/ |
217
|
|
|
public function setCorrelationId($correlation_id) |
218
|
|
|
{ |
219
|
|
|
$this->correlation_id = $correlation_id; |
220
|
|
|
|
221
|
|
|
return $this; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* @return string |
226
|
|
|
*/ |
227
|
|
|
public function getReplyTo() |
228
|
|
|
{ |
229
|
|
|
return $this->reply_to; |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* @param string $reply_to |
234
|
|
|
* |
235
|
|
|
* @return Message |
236
|
|
|
*/ |
237
|
|
|
public function setReplyTo($reply_to) |
238
|
|
|
{ |
239
|
|
|
$this->reply_to = $reply_to; |
240
|
|
|
|
241
|
|
|
return $this; |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* @return string |
246
|
|
|
*/ |
247
|
|
|
public function getExpiration() |
248
|
|
|
{ |
249
|
|
|
return $this->expiration; |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* @param string $expiration |
254
|
|
|
* |
255
|
|
|
* @return Message |
256
|
|
|
*/ |
257
|
|
|
public function setExpiration($expiration) |
258
|
|
|
{ |
259
|
|
|
$this->expiration = $expiration; |
260
|
|
|
|
261
|
|
|
return $this; |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
/** |
265
|
|
|
* @return string |
266
|
|
|
*/ |
267
|
|
|
public function getMessageId() |
268
|
|
|
{ |
269
|
|
|
return $this->message_id; |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
/** |
273
|
|
|
* @param string $message_id |
274
|
|
|
* |
275
|
|
|
* @return Message |
276
|
|
|
*/ |
277
|
|
|
public function setMessageId($message_id) |
278
|
|
|
{ |
279
|
|
|
$this->message_id = $message_id; |
280
|
|
|
|
281
|
|
|
return $this; |
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
/** |
285
|
|
|
* @return \DateTimeInterface |
286
|
|
|
*/ |
287
|
|
|
public function getTimestamp() |
288
|
|
|
{ |
289
|
|
|
return $this->timestamp; |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
/** |
293
|
|
|
* @param \DateTimeInterface $timestamp |
294
|
|
|
* |
295
|
|
|
* @return Message |
296
|
|
|
*/ |
297
|
|
|
public function setTimestamp($timestamp) |
298
|
|
|
{ |
299
|
|
|
$this->timestamp = $timestamp; |
300
|
|
|
|
301
|
|
|
return $this; |
302
|
|
|
} |
303
|
|
|
|
304
|
|
|
/** |
305
|
|
|
* @return string |
306
|
|
|
*/ |
307
|
|
|
public function getType() |
308
|
|
|
{ |
309
|
|
|
return $this->type; |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
/** |
313
|
|
|
* @param string $type |
314
|
|
|
* |
315
|
|
|
* @return Message |
316
|
|
|
*/ |
317
|
|
|
public function setType($type) |
318
|
|
|
{ |
319
|
|
|
$this->type = $type; |
320
|
|
|
|
321
|
|
|
return $this; |
322
|
|
|
} |
323
|
|
|
|
324
|
|
|
/** |
325
|
|
|
* @return string |
326
|
|
|
*/ |
327
|
|
|
public function getUserId() |
328
|
|
|
{ |
329
|
|
|
return $this->user_id; |
330
|
|
|
} |
331
|
|
|
|
332
|
|
|
/** |
333
|
|
|
* @param string $user_id |
334
|
|
|
* |
335
|
|
|
* @return Message |
336
|
|
|
*/ |
337
|
|
|
public function setUserId($user_id) |
338
|
|
|
{ |
339
|
|
|
$this->user_id = $user_id; |
340
|
|
|
|
341
|
|
|
return $this; |
342
|
|
|
} |
343
|
|
|
|
344
|
|
|
/** |
345
|
|
|
* @return string |
346
|
|
|
*/ |
347
|
|
|
public function getAppId() |
348
|
|
|
{ |
349
|
|
|
return $this->app_id; |
350
|
|
|
} |
351
|
|
|
|
352
|
|
|
/** |
353
|
|
|
* @param string $app_id |
354
|
|
|
* |
355
|
|
|
* @return Message |
356
|
|
|
*/ |
357
|
|
|
public function setAppId($app_id) |
358
|
|
|
{ |
359
|
|
|
$this->app_id = $app_id; |
360
|
|
|
|
361
|
|
|
return $this; |
362
|
|
|
} |
363
|
|
|
|
364
|
|
|
/** |
365
|
|
|
* @return string |
366
|
|
|
*/ |
367
|
|
|
public function getClusterId() |
368
|
|
|
{ |
369
|
|
|
return $this->cluster_id; |
370
|
|
|
} |
371
|
|
|
|
372
|
|
|
/** |
373
|
|
|
* @param string $cluster_id |
374
|
|
|
* |
375
|
|
|
* @return Message |
376
|
|
|
*/ |
377
|
|
|
public function setClusterId($cluster_id) |
378
|
|
|
{ |
379
|
|
|
$this->cluster_id = $cluster_id; |
380
|
|
|
|
381
|
|
|
return $this; |
382
|
|
|
} |
383
|
|
|
|
384
|
|
|
/** |
385
|
|
|
* @return AMQPMessage |
386
|
|
|
*/ |
387
|
|
|
public function toAMQPMessage() |
388
|
|
|
{ |
389
|
|
|
$parameters = []; |
390
|
|
|
$this->fillParameters('content_type', $parameters); |
391
|
|
|
$this->fillParameters('content_encoding', $parameters); |
392
|
|
|
$this->fillParameters('application_headers', $parameters); |
393
|
|
|
$this->fillParameters('delivery_mode', $parameters); |
394
|
|
|
$this->fillParameters('priority', $parameters); |
395
|
|
|
$this->fillParameters('correlation_id', $parameters); |
396
|
|
|
$this->fillParameters('reply_to', $parameters); |
397
|
|
|
$this->fillParameters('expiration', $parameters); |
398
|
|
|
$this->fillParameters('message_id', $parameters); |
399
|
|
|
$this->fillParameters('timestamp', $parameters); |
400
|
|
|
$this->fillParameters('type', $parameters); |
401
|
|
|
$this->fillParameters('user_id', $parameters); |
402
|
|
|
$this->fillParameters('app_id', $parameters); |
403
|
|
|
$this->fillParameters('cluster_id', $parameters); |
404
|
|
|
|
405
|
|
|
$amqpMessage = new AMQPMessage($this->body, $parameters); |
406
|
|
|
|
407
|
|
|
return $amqpMessage; |
408
|
|
|
} |
409
|
|
|
|
410
|
|
|
private function fillParameters($parameterName, &$parameters) |
411
|
|
|
{ |
412
|
|
|
if ($this->$parameterName !== null) { |
413
|
|
|
$parameters[$parameterName] = $this->$parameterName; |
414
|
|
|
} |
415
|
|
|
} |
416
|
|
|
} |
417
|
|
|
|