1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Sonata Project package. |
5
|
|
|
* |
6
|
|
|
* (c) Thomas Rabaix <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Sonata\NotificationBundle\Iterator; |
13
|
|
|
|
14
|
|
|
use Interop\Amqp\AmqpConsumer; |
15
|
|
|
use PhpAmqpLib\Channel\AMQPChannel; |
16
|
|
|
use PhpAmqpLib\Message\AMQPMessage; |
17
|
|
|
use Sonata\NotificationBundle\Model\Message; |
18
|
|
|
|
19
|
|
|
class AMQPMessageIterator implements MessageIteratorInterface |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @deprecated since 3.2, will be removed in 4.x |
23
|
|
|
* |
24
|
|
|
* @var AMQPChannel |
25
|
|
|
*/ |
26
|
|
|
protected $channel; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var mixed |
30
|
|
|
*/ |
31
|
|
|
protected $message; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @deprecated since 3.2, will be removed in 4.x |
35
|
|
|
* |
36
|
|
|
* @var AMQPMessage |
37
|
|
|
*/ |
38
|
|
|
protected $AMQMessage; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @deprecated since 3.2, will be removed in 4.x |
42
|
|
|
* |
43
|
|
|
* @var string |
44
|
|
|
*/ |
45
|
|
|
protected $queue; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var int |
49
|
|
|
*/ |
50
|
|
|
protected $counter; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @var \Interop\Amqp\AmqpMessage |
54
|
|
|
*/ |
55
|
|
|
private $interopMessage; |
|
|
|
|
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @var int |
59
|
|
|
*/ |
60
|
|
|
private $timeout; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @var AmqpConsumer |
64
|
|
|
*/ |
65
|
|
|
private $consumer; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @var bool |
69
|
|
|
*/ |
70
|
|
|
private $isValid; |
71
|
|
|
|
72
|
|
|
public function __construct(AMQPChannel $channel, AmqpConsumer $consumer) |
73
|
|
|
{ |
74
|
|
|
$this->consumer = $consumer; |
75
|
|
|
$this->counter = 0; |
76
|
|
|
$this->timeout = 0; |
77
|
|
|
$this->isValid = true; |
78
|
|
|
|
79
|
|
|
$this->channel = $channel; |
|
|
|
|
80
|
|
|
$this->queue = $consumer->getQueue()->getQueueName(); |
|
|
|
|
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* {@inheritdoc} |
85
|
|
|
*/ |
86
|
|
|
public function current() |
87
|
|
|
{ |
88
|
|
|
return $this->message; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* {@inheritdoc} |
93
|
|
|
*/ |
94
|
|
|
public function next() |
95
|
|
|
{ |
96
|
|
|
$this->isValid = false; |
97
|
|
|
|
98
|
|
|
if ($amqpMessage = $this->consumer->receive($this->timeout)) { |
99
|
|
|
$this->AMQMessage = $this->convertToAmqpLibMessage($amqpMessage); |
|
|
|
|
100
|
|
|
|
101
|
|
|
$data = json_decode($amqpMessage->getBody(), true); |
102
|
|
|
$data['body']['interopMessage'] = $amqpMessage; |
103
|
|
|
|
104
|
|
|
// @deprecated |
105
|
|
|
$data['body']['AMQMessage'] = $this->AMQMessage; |
|
|
|
|
106
|
|
|
|
107
|
|
|
$message = new Message(); |
108
|
|
|
$message->setBody($data['body']); |
109
|
|
|
$message->setType($data['type']); |
110
|
|
|
$message->setState($data['state']); |
111
|
|
|
$this->message = $message; |
112
|
|
|
|
113
|
|
|
++$this->counter; |
114
|
|
|
$this->isValid = true; |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* {@inheritdoc} |
120
|
|
|
*/ |
121
|
|
|
public function key() |
122
|
|
|
{ |
123
|
|
|
$this->counter; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* {@inheritdoc} |
128
|
|
|
*/ |
129
|
|
|
public function valid() |
130
|
|
|
{ |
131
|
|
|
return $this->isValid; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* {@inheritdoc} |
136
|
|
|
*/ |
137
|
|
|
public function rewind() |
138
|
|
|
{ |
139
|
|
|
$this->isValid = true; |
140
|
|
|
$this->next(); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @deprecated since 3.2, will be removed in 4.x |
145
|
|
|
* |
146
|
|
|
* @param \Interop\Amqp\AmqpMessage $amqpMessage |
147
|
|
|
* |
148
|
|
|
* @return AMQPMessage |
149
|
|
|
*/ |
150
|
|
|
private function convertToAmqpLibMessage(\Interop\Amqp\AmqpMessage $amqpMessage) |
151
|
|
|
{ |
152
|
|
|
$amqpLibProperties = $amqpMessage->getHeaders(); |
153
|
|
|
$amqpLibProperties['application_headers'] = $amqpMessage->getProperties(); |
154
|
|
|
|
155
|
|
|
$amqpLibMessage = new AMQPMessage($amqpMessage->getBody(), $amqpLibProperties); |
156
|
|
|
$amqpLibMessage->delivery_info = [ |
157
|
|
|
'consumer_tag' => $this->consumer->getConsumerTag(), |
158
|
|
|
'delivery_tag' => $amqpMessage->getDeliveryTag(), |
159
|
|
|
'redelivered' => $amqpMessage->isRedelivered(), |
160
|
|
|
'routing_key' => $amqpMessage->getRoutingKey(), |
161
|
|
|
'channel' => $this->channel, |
|
|
|
|
162
|
|
|
]; |
163
|
|
|
|
164
|
|
|
return $amqpLibMessage; |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|
This check marks private properties in classes that are never used. Those properties can be removed.