1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Kunnu\RabbitMQ; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Collection; |
6
|
|
|
use PhpAmqpLib\Message\AMQPMessage; |
7
|
|
|
|
8
|
|
|
class RabbitMQIncomingMessage |
9
|
|
|
{ |
10
|
|
|
protected string $stream; |
11
|
|
|
|
12
|
|
|
protected Collection $config; |
13
|
|
|
|
14
|
|
|
protected ?RabbitMQExchange $exchange = null; |
15
|
|
|
|
16
|
|
|
protected ?RabbitMQQueue $queue = null; |
17
|
|
|
|
18
|
|
|
protected ?RabbitMQMessageConsumer $consumer = null; |
19
|
|
|
|
20
|
|
|
protected ?RabbitMQDelivery $deliveryInfo = null; |
21
|
|
|
|
22
|
|
|
protected ?AMQPMessage $amqpMessage = null; |
23
|
|
|
|
24
|
|
|
public function __construct(string $stream = '', array $config = []) |
25
|
|
|
{ |
26
|
|
|
$this->stream = $stream; |
27
|
|
|
$this->setConfig($config); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param array $config |
32
|
|
|
* @return \Kunnu\RabbitMQ\RabbitMQIncomingMessage |
33
|
|
|
*/ |
34
|
|
|
public function setConfig(array $config): self |
35
|
|
|
{ |
36
|
|
|
$this->config = new Collection($config); |
|
|
|
|
37
|
|
|
|
38
|
|
|
return $this; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @return Collection |
43
|
|
|
*/ |
44
|
|
|
public function getConfig(): Collection |
45
|
|
|
{ |
46
|
|
|
return $this->config; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @return string |
51
|
|
|
*/ |
52
|
|
|
public function getStream(): string |
53
|
|
|
{ |
54
|
|
|
return $this->stream; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param string $stream |
59
|
|
|
* @return \Kunnu\RabbitMQ\RabbitMQIncomingMessage |
60
|
|
|
*/ |
61
|
|
|
public function setStream(string $stream): self |
62
|
|
|
{ |
63
|
|
|
$this->stream = $stream; |
64
|
|
|
|
65
|
|
|
return $this; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @return null|\Kunnu\RabbitMQ\RabbitMQExchange|null |
70
|
|
|
*/ |
71
|
|
|
public function getExchange(): ?RabbitMQExchange |
72
|
|
|
{ |
73
|
|
|
return $this->exchange; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param \Kunnu\RabbitMQ\RabbitMQExchange|null $exchange |
78
|
|
|
* @return self |
79
|
|
|
*/ |
80
|
|
|
public function setExchange(?RabbitMQExchange $exchange): self |
81
|
|
|
{ |
82
|
|
|
$this->exchange = $exchange; |
83
|
|
|
|
84
|
|
|
return $this; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @return null|\Kunnu\RabbitMQ\RabbitMQQueue|null |
89
|
|
|
*/ |
90
|
|
|
public function getQueue(): ?RabbitMQQueue |
91
|
|
|
{ |
92
|
|
|
return $this->queue; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param \Kunnu\RabbitMQ\RabbitMQQueue|null $queue |
97
|
|
|
* @return self |
98
|
|
|
*/ |
99
|
|
|
public function setQueue(?RabbitMQQueue $queue): self |
100
|
|
|
{ |
101
|
|
|
$this->queue = $queue; |
102
|
|
|
|
103
|
|
|
return $this; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @return \Kunnu\RabbitMQ\RabbitMQDelivery|null |
108
|
|
|
*/ |
109
|
|
|
public function getDelivery(): ?RabbitMQDelivery |
110
|
|
|
{ |
111
|
|
|
return $this->deliveryInfo; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @param \Kunnu\RabbitMQ\RabbitMQDelivery|null $deliveryInfo |
116
|
|
|
* @return \Kunnu\RabbitMQ\RabbitMQIncomingMessage |
117
|
|
|
*/ |
118
|
|
|
public function setDelivery(?RabbitMQDelivery $deliveryInfo): self |
119
|
|
|
{ |
120
|
|
|
$this->deliveryInfo = $deliveryInfo; |
121
|
|
|
|
122
|
|
|
return $this; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @return RabbitMQMessageConsumer|null |
127
|
|
|
*/ |
128
|
|
|
public function getConsumer(): ?RabbitMQMessageConsumer |
129
|
|
|
{ |
130
|
|
|
return $this->consumer; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @param RabbitMQMessageConsumer $consumer |
135
|
|
|
* @return self |
136
|
|
|
*/ |
137
|
|
|
public function setConsumer(?RabbitMQMessageConsumer $consumer): self |
138
|
|
|
{ |
139
|
|
|
$this->consumer = $consumer; |
140
|
|
|
|
141
|
|
|
return $this; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @return \PhpAmqpLib\Message\AMQPMessage|null |
146
|
|
|
*/ |
147
|
|
|
public function getAmqpMessage(): ?AMQPMessage |
148
|
|
|
{ |
149
|
|
|
return $this->amqpMessage; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* @param \PhpAmqpLib\Message\AMQPMessage|null $amqpMessage |
154
|
|
|
* @return \Kunnu\RabbitMQ\RabbitMQIncomingMessage |
155
|
|
|
*/ |
156
|
|
|
public function setAmqpMessage(?AMQPMessage $amqpMessage): RabbitMQIncomingMessage |
157
|
|
|
{ |
158
|
|
|
$this->amqpMessage = $amqpMessage; |
159
|
|
|
|
160
|
|
|
return $this; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
public function getMessageApplicationHeaders(): array |
164
|
|
|
{ |
165
|
|
|
$amqp = $this->getAmqpMessage(); |
166
|
|
|
$props = $amqp ? $amqp->get_properties() : []; |
167
|
|
|
|
168
|
|
|
return isset($props['application_headers']) ? $props['application_headers']->getNativeData() : []; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
public function getMessageApplicationHeader($key, $default = null) |
172
|
|
|
{ |
173
|
|
|
return array_key_exists($key, ($headers = $this->getMessageApplicationHeaders())) ? $headers[$key] : $default; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
public function isRedelivered(): bool |
177
|
|
|
{ |
178
|
|
|
$delivery = $this->getDelivery(); |
179
|
|
|
$info = $delivery ? $delivery->getConfig()->get('delivery_info') : null; |
180
|
|
|
|
181
|
|
|
if (!$delivery || !$info) { |
182
|
|
|
throw new RabbitMQException('Delivery info not available.'); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
return (bool) ($info['redelivered'] ?? false); |
186
|
|
|
} |
187
|
|
|
} |
188
|
|
|
|