Skip to content

Commit

Permalink
Adding method to save the event store queue
Browse files Browse the repository at this point in the history
  • Loading branch information
impleri committed Jan 12, 2016
1 parent 4b91003 commit 31bc02a
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
Expand Up @@ -14,13 +14,14 @@ public function up()
{
if (!Schema::hasTable('event_store')) {
Schema::create('event_store', function (Blueprint $table) {
$table->increments('id');
$table->string('identifier', 40);
$table->unsignedInteger('sequence');
$table->string('event', 100);
$table->text('payload');
$table->timestamps();

$table->primary(['identifier', 'sequence'], 'entity_version');
$table->unique(['identifier', 'sequence'], 'entity_version');
$table->index('identifier');
});
}
Expand Down
28 changes: 27 additions & 1 deletion src/Event/Store.php
Expand Up @@ -9,6 +9,16 @@ class Store extends Model implements StoreInterface
{
protected static $queue = [];

/**
* @inheritDoc
*/
protected $fillable = [
'identifier',
'sequence',
'event',
'payload'
];

public function enqueue(EventInterface $event)
{
static::$queue[] = [
Expand All @@ -24,7 +34,7 @@ public function getFor($identifier)
{
$events = new Collection;

$recorded_events = $this->newQuery()->where('identifier', '=', $identifier)->get();
$recorded_events = $this->newQuery()->forEntity($identifier)->get();
foreach ($recorded_events as $record) {
$events->append($this->restoreEvent($record));
}
Expand All @@ -38,4 +48,20 @@ protected function restoreEvent($record)

return $class::unserialize($record);
}

public static function saveQueue()
{
foreach (static::$queue as $record) {
$record['sequence'] = static::forEntity($record['identifier'])
->count();
static::create($record);
}

static::$queue = [];
}

public function scopeForEntity($query, $identifier)
{
return $query->where('identifier', '=', $identifier);
}
}

0 comments on commit 31bc02a

Please sign in to comment.