Ddd\Application\Notification\NotificationService::publishNotifications PHP Method

publishNotifications() public method

public publishNotifications ( $exchangeName ) : integer
return integer
    public function publishNotifications($exchangeName)
    {
        $publishedMessageTracker = $this->publishedMessageTracker();
        $notifications = $this->listUnpublishedNotifications($publishedMessageTracker->mostRecentPublishedMessageId($exchangeName));
        if (!$notifications) {
            return 0;
        }
        $messageProducer = $this->messageProducer();
        $messageProducer->open($exchangeName);
        try {
            $publishedMessages = 0;
            $lastPublishedNotification = null;
            foreach ($notifications as $notification) {
                $lastPublishedNotification = $this->publish($exchangeName, $notification, $messageProducer);
                $publishedMessages++;
            }
        } catch (\Exception $e) {
        }
        $this->trackMostRecentPublishedMessage($publishedMessageTracker, $exchangeName, $lastPublishedNotification);
        $messageProducer->close($exchangeName);
        return $publishedMessages;
    }

Usage Example

 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $app = $this->getApplication()->getContainer();
     $notificationService = new NotificationService($app['event_store'], $app['message_tracker'], $app['message_producer']);
     $numberOfNotifications = $notificationService->publishNotifications($input->getArgument('exchange-name'));
     $output->writeln(sprintf('<comment>%d</comment> <info>notification(s) sent!</info>', $numberOfNotifications));
 }