1717use FiveLab \Component \Amqp \Consumer \Checker \RunConsumerCheckerRegistryInterface ;
1818use FiveLab \Component \Amqp \Consumer \ConsumerInterface ;
1919use FiveLab \Component \Amqp \Consumer \EventableConsumerInterface ;
20- use FiveLab \Component \Amqp \Consumer \Middleware \StopAfterNExecutesMiddleware ;
21- use FiveLab \Component \Amqp \Consumer \MiddlewareAwareInterface ;
2220use FiveLab \Component \Amqp \Consumer \Registry \ConsumerRegistryInterface ;
21+ use FiveLab \Component \Amqp \Event \ProcessedMessageEvent ;
2322use FiveLab \Component \Amqp \Exception \RunConsumerCheckerNotFoundException ;
23+ use FiveLab \Component \Amqp \Listener \StopAfterNExecutesListener ;
2424use Symfony \Component \Console \Attribute \AsCommand ;
2525use Symfony \Component \Console \Command \Command ;
2626use Symfony \Component \Console \Command \SignalableCommandInterface ;
2727use Symfony \Component \Console \Input \InputArgument ;
2828use Symfony \Component \Console \Input \InputInterface ;
2929use Symfony \Component \Console \Input \InputOption ;
3030use Symfony \Component \Console \Output \OutputInterface ;
31+ use Symfony \Component \EventDispatcher \EventDispatcherInterface ;
3132
3233#[AsCommand(name: 'event-broker:consumer:run ' , description: 'Run consumer. ' )]
3334class RunConsumerCommand extends Command implements SignalableCommandInterface
@@ -37,7 +38,8 @@ class RunConsumerCommand extends Command implements SignalableCommandInterface
3738
3839 public function __construct (
3940 private readonly ConsumerRegistryInterface $ consumerRegistry ,
40- ?RunConsumerCheckerRegistryInterface $ runCheckerRegistry = null
41+ ?RunConsumerCheckerRegistryInterface $ runCheckerRegistry = null ,
42+ private readonly ?EventDispatcherInterface $ eventDispatcher = null
4143 ) {
4244 parent ::__construct ();
4345
@@ -95,21 +97,24 @@ protected function execute(InputInterface $input, OutputInterface $output): int
9597 $ this ->consumer = $ this ->consumerRegistry ->get ($ consumerKey );
9698
9799 if ($ this ->consumer instanceof EventableConsumerInterface) {
98- $ closure = (new OutputEventHandler ($ output ))(...);
99-
100- $ this ->consumer ->addEventHandler ($ closure );
100+ $ this ->consumer ->setEventDispatcher ($ this ->eventDispatcher );
101101 }
102102
103103 if ($ input ->getOption ('messages ' )) {
104- if (!$ this ->consumer instanceof MiddlewareAwareInterface ) {
104+ if (!$ this ->consumer instanceof EventableConsumerInterface ) {
105105 throw new \InvalidArgumentException (\sprintf (
106106 'For set number of messages customer must implement "%s", but "%s" given. ' ,
107- MiddlewareAwareInterface ::class,
107+ EventableConsumerInterface ::class,
108108 \get_class ($ this ->consumer )
109109 ));
110110 }
111111
112- $ this ->consumer ->pushMiddleware (new StopAfterNExecutesMiddleware ((int ) $ input ->getOption ('messages ' )));
112+ if (!$ this ->eventDispatcher ) {
113+ throw new \RuntimeException ('A message limit can \'t be applied, since the command has no access to the event dispatcher. ' );
114+ }
115+
116+ $ listener = new StopAfterNExecutesListener ($ this ->eventDispatcher , (int ) $ input ->getOption ('messages ' ));
117+ $ this ->consumer ->getEventDispatcher ()?->addListener(ProcessedMessageEvent::class, $ listener ->onProcessedMessage (...));
113118 }
114119
115120 if ($ input ->getOption ('read-timeout ' )) {
0 commit comments