From 2619633898abcc2912bc4c19c62df04b0a22208d Mon Sep 17 00:00:00 2001 From: Craig Morris Date: Mon, 5 Aug 2019 10:40:27 +0100 Subject: [PATCH] feat: dispatch a serializable event so queued listeners can be used (#6) --- src/EventStoreWorker.php | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/EventStoreWorker.php b/src/EventStoreWorker.php index aa07246..f213eb8 100644 --- a/src/EventStoreWorker.php +++ b/src/EventStoreWorker.php @@ -6,8 +6,10 @@ use EventLoop\EventLoop; use Rxnet\EventStore\EventStore; +use Rxnet\EventStore\Data\EventRecord as EventData; use Rxnet\EventStore\Record\AcknowledgeableEventRecord; use Rxnet\EventStore\Record\EventRecord; +use Rxnet\EventStore\Record\JsonEventRecord; class EventStoreWorker extends Command { @@ -89,11 +91,25 @@ private function processStream($eventStore, string $stream) }, 'report'); } - public function dispatch(EventRecord $event) + public function dispatch(EventRecord $eventRecord) { + $event = $this->makeSerializableEvent($eventRecord); + $type = $event->getType(); $class = config('eventstore.namespace') . '\\' . $type; class_exists($class) ? event(new $class($event)) : event($type, $event); } + + protected function makeSerializableEvent(EventRecord $event) + { + $data = new EventData(); + + $data->setEventType($event->getType()); + $data->setCreatedEpoch($event->getCreated()->getTimestamp() * 1000); + $data->setData(json_encode($event->getData())); + $data->setMetadata(json_encode($event->getMetadata())); + + return new JsonEventRecord($data); + } }