99use Interop \Amqp \AmqpQueue as InteropAmqpQueue ;
1010use Interop \Queue \InvalidMessageException ;
1111use Interop \Queue \PsrMessage ;
12+ use Interop \Queue \PsrQueue ;
1213
1314class AmqpConsumer implements InteropAmqpConsumer
1415{
@@ -27,16 +28,6 @@ class AmqpConsumer implements InteropAmqpConsumer
2728 */
2829 private $ queue ;
2930
30- /**
31- * @var Buffer
32- */
33- private $ buffer ;
34-
35- /**
36- * @var string
37- */
38- private $ receiveMethod ;
39-
4031 /**
4132 * @var int
4233 */
@@ -47,108 +38,86 @@ class AmqpConsumer implements InteropAmqpConsumer
4738 */
4839 private $ consumerTag ;
4940
50- /**
51- * @param AmqpContext $context
52- * @param InteropAmqpQueue $queue
53- * @param Buffer $buffer
54- * @param string $receiveMethod
55- */
56- public function __construct (AmqpContext $ context , InteropAmqpQueue $ queue , Buffer $ buffer , $ receiveMethod )
41+ public function __construct (AmqpContext $ context , InteropAmqpQueue $ queue )
5742 {
5843 $ this ->context = $ context ;
5944 $ this ->channel = $ context ->getBunnyChannel ();
6045 $ this ->queue = $ queue ;
61- $ this ->buffer = $ buffer ;
62- $ this ->receiveMethod = $ receiveMethod ;
6346 $ this ->flags = self ::FLAG_NOPARAM ;
6447 }
6548
66- /**
67- * {@inheritdoc}
68- */
69- public function setConsumerTag ($ consumerTag )
49+ public function setConsumerTag (string $ consumerTag = null ): void
7050 {
7151 $ this ->consumerTag = $ consumerTag ;
7252 }
7353
74- /**
75- * {@inheritdoc}
76- */
77- public function getConsumerTag ()
54+ public function getConsumerTag (): ?string
7855 {
7956 return $ this ->consumerTag ;
8057 }
8158
82- /**
83- * {@inheritdoc}
84- */
85- public function clearFlags ()
59+ public function clearFlags (): void
8660 {
8761 $ this ->flags = self ::FLAG_NOPARAM ;
8862 }
8963
90- /**
91- * {@inheritdoc}
92- */
93- public function addFlag ($ flag )
64+ public function addFlag (int $ flag ): void
9465 {
9566 $ this ->flags |= $ flag ;
9667 }
9768
98- /**
99- * {@inheritdoc}
100- */
101- public function getFlags ()
69+ public function getFlags (): int
10270 {
10371 return $ this ->flags ;
10472 }
10573
106- /**
107- * {@inheritdoc}
108- */
109- public function setFlags ($ flags )
74+ public function setFlags (int $ flags ): void
11075 {
11176 $ this ->flags = $ flags ;
11277 }
11378
11479 /**
115- * {@inheritdoc}
80+ * @return InteropAmqpQueue
11681 */
117- public function getQueue ()
82+ public function getQueue (): PsrQueue
11883 {
11984 return $ this ->queue ;
12085 }
12186
12287 /**
123- * {@inheritdoc}
88+ * @return InteropAmqpMessage
12489 */
125- public function receive ($ timeout = 0 )
90+ public function receive (int $ timeout = 0 ): ? PsrMessage
12691 {
127- if ('basic_get ' == $ this ->receiveMethod ) {
128- return $ this ->receiveBasicGet ($ timeout );
129- }
92+ $ end = microtime (true ) + ($ timeout / 1000 );
93+
94+ while (0 === $ timeout || microtime (true ) < $ end ) {
95+ if ($ message = $ this ->receiveNoWait ()) {
96+ return $ message ;
97+ }
13098
131- if ('basic_consume ' == $ this ->receiveMethod ) {
132- return $ this ->receiveBasicConsume ($ timeout );
99+ usleep (100000 ); //100ms
133100 }
134101
135- throw new \ LogicException ( ' The "receiveMethod" is not supported ' ) ;
102+ return null ;
136103 }
137104
138105 /**
139- * {@inheritdoc}
106+ * @return InteropAmqpMessage
140107 */
141- public function receiveNoWait ()
108+ public function receiveNoWait (): ? PsrMessage
142109 {
143110 if ($ message = $ this ->channel ->get ($ this ->queue ->getQueueName (), (bool ) ($ this ->getFlags () & InteropAmqpConsumer::FLAG_NOACK ))) {
144111 return $ this ->context ->convertMessage ($ message );
145112 }
113+
114+ return null ;
146115 }
147116
148117 /**
149118 * @param InteropAmqpMessage $message
150119 */
151- public function acknowledge (PsrMessage $ message )
120+ public function acknowledge (PsrMessage $ message ): void
152121 {
153122 InvalidMessageException ::assertMessageInstanceOf ($ message , InteropAmqpMessage::class);
154123
@@ -158,76 +127,12 @@ public function acknowledge(PsrMessage $message)
158127
159128 /**
160129 * @param InteropAmqpMessage $message
161- * @param bool $requeue
162130 */
163- public function reject (PsrMessage $ message , $ requeue = false )
131+ public function reject (PsrMessage $ message , bool $ requeue = false ): void
164132 {
165133 InvalidMessageException ::assertMessageInstanceOf ($ message , InteropAmqpMessage::class);
166134
167135 $ bunnyMessage = new Message ('' , $ message ->getDeliveryTag (), '' , '' , '' , [], '' );
168136 $ this ->channel ->reject ($ bunnyMessage , $ requeue );
169137 }
170-
171- /**
172- * @param int $timeout
173- *
174- * @return InteropAmqpMessage|null
175- */
176- private function receiveBasicGet ($ timeout )
177- {
178- $ end = microtime (true ) + ($ timeout / 1000 );
179-
180- while (0 === $ timeout || microtime (true ) < $ end ) {
181- if ($ message = $ this ->receiveNoWait ()) {
182- return $ message ;
183- }
184-
185- usleep (100000 ); //100ms
186- }
187- }
188-
189- /**
190- * @param int $timeout
191- *
192- * @return InteropAmqpMessage|null
193- */
194- private function receiveBasicConsume ($ timeout )
195- {
196- if (false == $ this ->consumerTag ) {
197- $ this ->context ->subscribe ($ this , function (InteropAmqpMessage $ message ) {
198- $ this ->buffer ->push ($ message ->getConsumerTag (), $ message );
199-
200- return false ;
201- });
202- }
203-
204- if ($ message = $ this ->buffer ->pop ($ this ->consumerTag )) {
205- return $ message ;
206- }
207-
208- while (true ) {
209- $ start = microtime (true );
210-
211- $ this ->context ->consume ($ timeout );
212-
213- if ($ message = $ this ->buffer ->pop ($ this ->consumerTag )) {
214- return $ message ;
215- }
216-
217- // is here when consumed message is not for this consumer
218-
219- // as timeout is infinite have to continue consumption, but it can overflow message buffer
220- if ($ timeout <= 0 ) {
221- continue ;
222- }
223-
224- // compute remaining timeout and continue until time is up
225- $ stop = microtime (true );
226- $ timeout -= ($ stop - $ start ) * 1000 ;
227-
228- if ($ timeout <= 0 ) {
229- break ;
230- }
231- }
232- }
233138}
0 commit comments