public function eventHandling() { // Create the event bus and subscribe the created event listener $eventBus = new SimpleEventBus(); $eventListener = new MyEventListener(); $eventBus->subscribe($eventListener); // Create a domain event stream to publish $metadata = new Metadata(['source' => 'example']); $domainMessage = DomainMessage::recordNow(42, 1, $metadata, new stdClass()); $domainEventStream = new DomainEventStream([$domainMessage]); // Publish the message, and get output from the event handler \o/ $eventBus->publish($domainEventStream); }
/** * Publishes the events from the domain event stream to the listeners. * * @param DomainEventStreamInterface $domainMessages * @throws \Exception */ public function publish(DomainEventStreamInterface $domainMessages) { $this->queue[] = $domainMessages; if (!$this->isPublishing) { $this->isPublishing = true; try { parent::publish($domainMessages); while ($domainMessageStream = array_shift($this->queue)) { $serializedDomainMessages = $this->serializer->serialize($domainMessages); $this->queuePublisher->publish($serializedDomainMessages); } } catch (\Exception $e) { throw $e; } finally { $this->isPublishing = false; } } }