22
33namespace Gos \Component \ReactAMQP ;
44
5- use AMQPExchange ;
6- use AMQPExchangeException ;
7- use BadMethodCallException ;
8- use Countable ;
95use Evenement \EventEmitter ;
10- use IteratorAggregate ;
116use React \EventLoop \LoopInterface ;
127use React \EventLoop \TimerInterface ;
138
1611 *
1712 * @author Jeremy Cook <[email protected] > 1813 */
19- class Producer extends EventEmitter implements Countable, IteratorAggregate
14+ final class Producer extends EventEmitter implements \ Countable, \ IteratorAggregate
2015{
2116 /**
22- * AMQP message exchange to send messages to.
23- *
24- * @var AMQPExchange
17+ * @var \AMQPExchange
2518 */
26- protected $ exchange ;
19+ private $ exchange ;
2720
2821 /**
29- * Event loop.
30- *
3122 * @var LoopInterface
3223 */
33- protected $ loop ;
24+ private $ loop ;
3425
3526 /**
36- * Flag to indicate if this listener is closed.
37- *
3827 * @var bool
3928 */
40- protected $ closed = false ;
29+ private $ closed = false ;
4130
4231 /**
43- * Collection of messages waiting to be sent.
44- *
4532 * @var array
4633 */
47- protected $ messages = [];
34+ private $ messages = [];
4835
4936 /**
5037 * @var TimerInterface
5138 */
52- protected $ timer ;
39+ private $ timer ;
5340
54- /**
55- * Constructor. Stores the message queue and the event loop for use.
56- *
57- * @param AMQPExchange $exchange Message queue
58- * @param LoopInterface $loop Event loop
59- * @param float $interval Interval to run loop to send messages
60- */
61- public function __construct (AMQPExchange $ exchange , LoopInterface $ loop , $ interval )
41+ public function __construct (\AMQPExchange $ exchange , LoopInterface $ loop , float $ interval )
6242 {
6343 $ this ->exchange = $ exchange ;
6444 $ this ->loop = $ loop ;
6545 $ this ->timer = $ this ->loop ->addPeriodicTimer ($ interval , $ this );
6646 }
6747
68- /**
69- * Returns the number of messages waiting to be sent. Implements the
70- * countable interface.
71- *
72- * @return int
73- */
7448 public function count (): int
7549 {
7650 return \count ($ this ->messages );
7751 }
7852
79- /**
80- * Returns the array of messages stored. Completes the implementation of
81- * the iteratorAggregate interface.
82- *
83- * @return array
84- */
8553 public function getIterator (): array
8654 {
8755 return $ this ->messages ;
8856 }
8957
9058 /**
91- * Method to publish a message to an AMQP exchange. Has the same method
92- * signature as the exchange objects publish method.
59+ * Publishes a message to an AMQP exchange.
9360 *
94- * @param string $message Message
95- * @param string $routingKey Routing key
96- * @param int|null $flags Flags
97- * @param array $attributes Attributes
61+ * Has the same method signature as the exchange object's publish method.
9862 *
99- * @throws BadMethodCallException
63+ * @throws \ BadMethodCallException if the producer connection has been closed
10064 */
101- public function publish (string $ message , string $ routingKey , ? int $ flags = null , $ attributes = []): void
65+ public function publish (string $ message , string $ routingKey , int $ flags = 0 /* AMQP_NOPARAM */ , array $ attributes = []): void
10266 {
10367 if ($ this ->closed ) {
104- throw new BadMethodCallException ('This Producer object is closed and cannot send any more messages. ' );
68+ throw new \ BadMethodCallException ('This Producer object is closed and cannot send any more messages. ' );
10569 }
70+
10671 $ this ->messages [] = [
10772 'message ' => $ message ,
10873 'routingKey ' => $ routingKey ,
@@ -112,32 +77,31 @@ public function publish(string $message, string $routingKey, ?int $flags = null,
11277 }
11378
11479 /**
115- * Callback to dispatch on the loop timer .
80+ * Handles publishing outgoing messages .
11681 *
117- * @throws \AMQPChannelException|\AMQPConnectionException
82+ * @throws \AMQPChannelException
83+ * @throws \AMQPConnectionException
84+ * @throws \BadMethodCallException if the consumer connection has been closed
11885 */
11986 public function __invoke (): void
12087 {
12188 if ($ this ->closed ) {
122- throw new BadMethodCallException ('This Producer object is closed and cannot send any more messages. ' );
89+ throw new \ BadMethodCallException ('This Producer object is closed and cannot send any more messages. ' );
12390 }
91+
12492 foreach ($ this ->messages as $ key => $ message ) {
12593 try {
12694 $ this ->exchange ->publish ($ message ['message ' ], $ message ['routingKey ' ], $ message ['flags ' ], $ message ['attributes ' ]);
12795 unset($ this ->messages [$ key ]);
12896 $ this ->emit ('produce ' , array_values ($ message ));
129- } catch (AMQPExchangeException $ e ) {
97+ } catch (\ AMQPExchangeException $ e ) {
13098 $ this ->emit ('error ' , [$ e ]);
13199 }
132100 }
133101 }
134102
135103 /**
136- * Allows calls to unknown methods to be passed through to the exchange
137- * stored.
138- *
139- * @param string $method Method name
140- * @param mixed $args Args to pass
104+ * Allows calls to unknown methods to be passed through to the exchange store.
141105 *
142106 * @return mixed
143107 */
@@ -146,9 +110,6 @@ public function __call($method, $args)
146110 return \call_user_func_array ([$ this ->exchange , $ method ], $ args );
147111 }
148112
149- /**
150- * Method to call when stopping listening to messages.
151- */
152113 public function close (): void
153114 {
154115 if ($ this ->closed ) {
@@ -161,4 +122,9 @@ public function close(): void
161122 $ this ->exchange = null ;
162123 $ this ->closed = true ;
163124 }
125+
126+ public function isClosed (): bool
127+ {
128+ return true === $ this ->closed ;
129+ }
164130}
0 commit comments