77use Interop \Amqp \AmqpQueue as InteropAmqpQueue ;
88use Interop \Queue \InvalidMessageException ;
99use Interop \Queue \PsrMessage ;
10+ use Interop \Queue \PsrQueue ;
1011use PhpAmqpLib \Channel \AMQPChannel ;
1112
1213class AmqpConsumer implements InteropAmqpConsumer
@@ -26,16 +27,6 @@ class AmqpConsumer implements InteropAmqpConsumer
2627 */
2728 private $ queue ;
2829
29- /**
30- * @var Buffer
31- */
32- private $ buffer ;
33-
34- /**
35- * @var string
36- */
37- private $ receiveMethod ;
38-
3930 /**
4031 * @var int
4132 */
@@ -46,110 +37,86 @@ class AmqpConsumer implements InteropAmqpConsumer
4637 */
4738 private $ consumerTag ;
4839
49- /**
50- * @param AmqpContext $context
51- * @param InteropAmqpQueue $queue
52- * @param Buffer $buffer
53- * @param string $receiveMethod
54- */
55- public function __construct (AmqpContext $ context , InteropAmqpQueue $ queue , Buffer $ buffer , $ receiveMethod )
40+ public function __construct (AmqpContext $ context , InteropAmqpQueue $ queue )
5641 {
5742 $ this ->context = $ context ;
5843 $ this ->channel = $ context ->getLibChannel ();
5944 $ this ->queue = $ queue ;
60- $ this ->buffer = $ buffer ;
61- $ this ->receiveMethod = $ receiveMethod ;
6245 $ this ->flags = self ::FLAG_NOPARAM ;
6346 }
6447
65- /**
66- * {@inheritdoc}
67- */
68- public function setConsumerTag ($ consumerTag )
48+ public function setConsumerTag (string $ consumerTag = null ): void
6949 {
7050 $ this ->consumerTag = $ consumerTag ;
7151 }
7252
73- /**
74- * {@inheritdoc}
75- */
76- public function getConsumerTag ()
53+ public function getConsumerTag (): ?string
7754 {
7855 return $ this ->consumerTag ;
7956 }
8057
81- /**
82- * {@inheritdoc}
83- */
84- public function clearFlags ()
58+ public function clearFlags (): void
8559 {
8660 $ this ->flags = self ::FLAG_NOPARAM ;
8761 }
8862
89- /**
90- * {@inheritdoc}
91- */
92- public function addFlag ($ flag )
63+ public function addFlag (int $ flag ): void
9364 {
9465 $ this ->flags |= $ flag ;
9566 }
9667
97- /**
98- * {@inheritdoc}
99- */
100- public function getFlags ()
68+ public function getFlags (): int
10169 {
10270 return $ this ->flags ;
10371 }
10472
105- /**
106- * {@inheritdoc}
107- */
108- public function setFlags ($ flags )
73+ public function setFlags (int $ flags ): void
10974 {
11075 $ this ->flags = $ flags ;
11176 }
11277
11378 /**
11479 * @return InteropAmqpQueue
11580 */
116- public function getQueue ()
81+ public function getQueue (): PsrQueue
11782 {
11883 return $ this ->queue ;
11984 }
12085
12186 /**
122- * {@inheritdoc}
123- *
124- * @return InteropAmqpMessage|null
87+ * @return InteropAmqpMessage
12588 */
126- public function receive ($ timeout = 0 )
89+ public function receive (int $ timeout = 0 ): ? PsrMessage
12790 {
128- if ('basic_get ' == $ this ->receiveMethod ) {
129- return $ this ->receiveBasicGet ($ timeout );
130- }
91+ $ end = microtime (true ) + ($ timeout / 1000 );
92+
93+ while (0 === $ timeout || microtime (true ) < $ end ) {
94+ if ($ message = $ this ->receiveNoWait ()) {
95+ return $ message ;
96+ }
13197
132- if ('basic_consume ' == $ this ->receiveMethod ) {
133- return $ this ->receiveBasicConsume ($ timeout );
98+ usleep (100000 ); //100ms
13499 }
135100
136- throw new \ LogicException ( ' The "receiveMethod" is not supported ' ) ;
101+ return null ;
137102 }
138103
139104 /**
140- * @return InteropAmqpMessage|null
105+ * @return InteropAmqpMessage
141106 */
142- public function receiveNoWait ()
107+ public function receiveNoWait (): ? PsrMessage
143108 {
144109 if ($ message = $ this ->channel ->basic_get ($ this ->queue ->getQueueName (), (bool ) ($ this ->getFlags () & InteropAmqpConsumer::FLAG_NOACK ))) {
145110 return $ this ->context ->convertMessage ($ message );
146111 }
112+
113+ return null ;
147114 }
148115
149116 /**
150117 * @param InteropAmqpMessage $message
151118 */
152- public function acknowledge (PsrMessage $ message )
119+ public function acknowledge (PsrMessage $ message ): void
153120 {
154121 InvalidMessageException ::assertMessageInstanceOf ($ message , InteropAmqpMessage::class);
155122
@@ -160,73 +127,10 @@ public function acknowledge(PsrMessage $message)
160127 * @param InteropAmqpMessage $message
161128 * @param bool $requeue
162129 */
163- public function reject (PsrMessage $ message , $ requeue = false )
130+ public function reject (PsrMessage $ message , bool $ requeue = false ): void
164131 {
165132 InvalidMessageException ::assertMessageInstanceOf ($ message , InteropAmqpMessage::class);
166133
167134 $ this ->channel ->basic_reject ($ message ->getDeliveryTag (), $ requeue );
168135 }
169-
170- /**
171- * @param int $timeout
172- *
173- * @return InteropAmqpMessage|null
174- */
175- private function receiveBasicGet ($ timeout )
176- {
177- $ end = microtime (true ) + ($ timeout / 1000 );
178-
179- while (0 === $ timeout || microtime (true ) < $ end ) {
180- if ($ message = $ this ->receiveNoWait ()) {
181- return $ message ;
182- }
183-
184- usleep (100000 ); //100ms
185- }
186- }
187-
188- /**
189- * @param int $timeout
190- *
191- * @return InteropAmqpMessage|null
192- */
193- private function receiveBasicConsume ($ timeout )
194- {
195- if (false == $ this ->consumerTag ) {
196- $ this ->context ->subscribe ($ this , function (InteropAmqpMessage $ message ) {
197- $ this ->buffer ->push ($ message ->getConsumerTag (), $ message );
198-
199- return false ;
200- });
201- }
202-
203- if ($ message = $ this ->buffer ->pop ($ this ->consumerTag )) {
204- return $ message ;
205- }
206-
207- while (true ) {
208- $ start = microtime (true );
209-
210- $ this ->context ->consume ($ timeout );
211-
212- if ($ message = $ this ->buffer ->pop ($ this ->consumerTag )) {
213- return $ message ;
214- }
215-
216- // is here when consumed message is not for this consumer
217-
218- // as timeout is infinite have to continue consumption, but it can overflow message buffer
219- if ($ timeout <= 0 ) {
220- continue ;
221- }
222-
223- // compute remaining timeout and continue until time is up
224- $ stop = microtime (true );
225- $ timeout -= ($ stop - $ start ) * 1000 ;
226-
227- if ($ timeout <= 0 ) {
228- break ;
229- }
230- }
231- }
232136}
0 commit comments