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