44
55use Interop \Amqp \AmqpConsumer as InteropAmqpConsumer ;
66use Interop \Amqp \AmqpMessage as InteropAmqpMessage ;
7- use Interop \Amqp \AmqpQueue ;
8- use Interop \Amqp \Impl \AmqpMessage ;
7+ use Interop \Amqp \AmqpQueue as InteropAmqpQueue ;
98use Interop \Queue \InvalidMessageException ;
109use Interop \Queue \PsrMessage ;
10+ use Interop \Queue \PsrQueue ;
1111
1212class AmqpConsumer implements InteropAmqpConsumer
1313{
@@ -17,25 +17,15 @@ class AmqpConsumer implements InteropAmqpConsumer
1717 private $ context ;
1818
1919 /**
20- * @var AmqpQueue
20+ * @var InteropAmqpQueue
2121 */
2222 private $ queue ;
2323
24- /**
25- * @var Buffer
26- */
27- private $ buffer ;
28-
2924 /**
3025 * @var \AMQPQueue
3126 */
3227 private $ extQueue ;
3328
34- /**
35- * @var string
36- */
37- private $ receiveMethod ;
38-
3929 /**
4030 * @var int
4131 */
@@ -46,127 +36,95 @@ class AmqpConsumer implements InteropAmqpConsumer
4636 */
4737 private $ consumerTag ;
4838
49- /**
50- * @param AmqpContext $context
51- * @param AmqpQueue $queue
52- * @param Buffer $buffer
53- * @param string $receiveMethod
54- */
55- public function __construct (AmqpContext $ context , AmqpQueue $ queue , Buffer $ buffer , $ receiveMethod )
39+ public function __construct (AmqpContext $ context , InteropAmqpQueue $ queue )
5640 {
5741 $ this ->queue = $ queue ;
5842 $ this ->context = $ context ;
59- $ this ->buffer = $ buffer ;
60- $ this ->receiveMethod = $ receiveMethod ;
6143 $ this ->flags = self ::FLAG_NOPARAM ;
6244 }
6345
64- /**
65- * {@inheritdoc}
66- */
67- public function setConsumerTag ($ consumerTag )
46+ public function setConsumerTag (string $ consumerTag = null ): void
6847 {
6948 $ this ->consumerTag = $ consumerTag ;
7049 }
7150
72- /**
73- * {@inheritdoc}
74- */
75- public function getConsumerTag ()
51+ public function getConsumerTag (): ?string
7652 {
7753 return $ this ->consumerTag ;
7854 }
7955
80- /**
81- * {@inheritdoc}
82- */
83- public function clearFlags ()
56+ public function clearFlags (): void
8457 {
8558 $ this ->flags = self ::FLAG_NOPARAM ;
8659 }
8760
88- /**
89- * {@inheritdoc}
90- */
91- public function addFlag ($ flag )
61+ public function addFlag (int $ flag ): void
9262 {
9363 $ this ->flags |= $ flag ;
9464 }
9565
96- /**
97- * {@inheritdoc}
98- */
99- public function getFlags ()
66+ public function getFlags (): int
10067 {
10168 return $ this ->flags ;
10269 }
10370
104- /**
105- * {@inheritdoc}
106- */
107- public function setFlags ($ flags )
71+ public function setFlags (int $ flags ): void
10872 {
10973 $ this ->flags = $ flags ;
11074 }
11175
11276 /**
113- * {@inheritdoc}
114- *
115- * @return AmqpQueue
77+ * @return InteropAmqpQueue
11678 */
117- public function getQueue ()
79+ public function getQueue (): PsrQueue
11880 {
11981 return $ this ->queue ;
12082 }
12183
12284 /**
123- * {@inheritdoc}
124- *
125- * @return InteropAmqpMessage|null
85+ * @return InteropAmqpMessage
12686 */
127- public function receive ($ timeout = 0 )
87+ public function receive (int $ timeout = 0 ): ? PsrMessage
12888 {
129- if ('basic_get ' == $ this ->receiveMethod ) {
130- return $ this ->receiveBasicGet ($ timeout );
131- }
89+ $ end = microtime (true ) + ($ timeout / 1000 );
13290
133- if ('basic_consume ' == $ this ->receiveMethod ) {
134- return $ this ->receiveBasicConsume ($ timeout );
91+ while (0 === $ timeout || microtime (true ) < $ end ) {
92+ if ($ message = $ this ->receiveNoWait ()) {
93+ return $ message ;
94+ }
95+
96+ usleep (100000 ); //100ms
13597 }
13698
137- throw new \ LogicException ( ' The "receiveMethod" is not supported ' ) ;
99+ return null ;
138100 }
139101
140102 /**
141- * {@inheritdoc}
142- *
143- * @return AmqpMessage|null
103+ * @return InteropAmqpMessage
144104 */
145- public function receiveNoWait ()
105+ public function receiveNoWait (): ? PsrMessage
146106 {
147107 if ($ extMessage = $ this ->getExtQueue ()->get (Flags ::convertConsumerFlags ($ this ->flags ))) {
148108 return $ this ->context ->convertMessage ($ extMessage );
149109 }
110+
111+ return null ;
150112 }
151113
152114 /**
153- * {@inheritdoc}
154- *
155- * @param AmqpMessage $message
115+ * @param InteropAmqpMessage $message
156116 */
157- public function acknowledge (PsrMessage $ message )
117+ public function acknowledge (PsrMessage $ message ): void
158118 {
159119 InvalidMessageException ::assertMessageInstanceOf ($ message , InteropAmqpMessage::class);
160120
161121 $ this ->getExtQueue ()->ack ($ message ->getDeliveryTag ());
162122 }
163123
164124 /**
165- * {@inheritdoc}
166- *
167- * @param AmqpMessage $message
125+ * @param InteropAmqpMessage $message
168126 */
169- public function reject (PsrMessage $ message , $ requeue = false )
127+ public function reject (PsrMessage $ message , bool $ requeue = false ): void
170128 {
171129 InvalidMessageException ::assertMessageInstanceOf ($ message , InteropAmqpMessage::class);
172130
@@ -176,73 +134,7 @@ public function reject(PsrMessage $message, $requeue = false)
176134 );
177135 }
178136
179- /**
180- * @param int $timeout
181- *
182- * @return InteropAmqpMessage|null
183- */
184- private function receiveBasicGet ($ timeout )
185- {
186- $ end = microtime (true ) + ($ timeout / 1000 );
187-
188- while (0 === $ timeout || microtime (true ) < $ end ) {
189- if ($ message = $ this ->receiveNoWait ()) {
190- return $ message ;
191- }
192-
193- usleep (100000 ); //100ms
194- }
195- }
196-
197- /**
198- * @param int $timeout
199- *
200- * @return InteropAmqpMessage|null
201- */
202- private function receiveBasicConsume ($ timeout )
203- {
204- if (false == $ this ->consumerTag ) {
205- $ this ->context ->subscribe ($ this , function (InteropAmqpMessage $ message ) {
206- $ this ->buffer ->push ($ message ->getConsumerTag (), $ message );
207-
208- return false ;
209- });
210- }
211-
212- if ($ message = $ this ->buffer ->pop ($ this ->consumerTag )) {
213- return $ message ;
214- }
215-
216- while (true ) {
217- $ start = microtime (true );
218-
219- $ this ->context ->consume ($ timeout );
220-
221- if ($ message = $ this ->buffer ->pop ($ this ->consumerTag )) {
222- return $ message ;
223- }
224-
225- // is here when consumed message is not for this consumer
226-
227- // as timeout is infinite have to continue consumption, but it can overflow message buffer
228- if ($ timeout <= 0 ) {
229- continue ;
230- }
231-
232- // compute remaining timeout and continue until time is up
233- $ stop = microtime (true );
234- $ timeout -= ($ stop - $ start ) * 1000 ;
235-
236- if ($ timeout <= 0 ) {
237- break ;
238- }
239- }
240- }
241-
242- /**
243- * @return \AMQPQueue
244- */
245- private function getExtQueue ()
137+ private function getExtQueue (): \AMQPQueue
246138 {
247139 if (false == $ this ->extQueue ) {
248140 $ extQueue = new \AMQPQueue ($ this ->context ->getExtChannel ());
0 commit comments