Skip to content

Commit

Permalink
feat: dispatch a serializable event so queued listeners can be used (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
Craig Morris authored Aug 5, 2019
1 parent 29b67eb commit 2619633
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion src/EventStoreWorker.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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);
}
}

0 comments on commit 2619633

Please sign in to comment.