This repository has been archived by the owner on Sep 20, 2019. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
21 changed files
with
359 additions
and
179 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
<?php | ||
|
||
namespace Spatie\EventProjector; | ||
|
||
use Carbon\Carbon; | ||
use Illuminate\Support\Collection; | ||
use Spatie\EventProjector\Models\StoredEvent; | ||
use Spatie\EventProjector\Models\EloquentStoredEvent; | ||
use Spatie\EventProjector\EventSerializers\EventSerializer; | ||
|
||
class EloquentStoredEventRepository implements StoredEventRepository | ||
{ | ||
public function retrieveAll(string $uuid = null, int $startingFrom = null): Collection | ||
{ | ||
$query = EloquentStoredEvent::query(); | ||
|
||
if ($uuid) { | ||
$query->uuid($uuid); | ||
} | ||
|
||
if ($startingFrom) { | ||
$query->startingFrom($startingFrom); | ||
} | ||
|
||
return $query->get()->map(function (EloquentStoredEvent $storedEvent) { | ||
return $storedEvent->toStoredEvent(); | ||
}); | ||
} | ||
|
||
public function persist(ShouldBeStored $event, string $uuid = null): StoredEvent | ||
{ | ||
$eloquentStoredEvent = new EloquentStoredEvent(); | ||
|
||
$eloquentStoredEvent->setRawAttributes([ | ||
'event_properties' => app(EventSerializer::class)->serialize(clone $event), | ||
'aggregate_uuid' => $uuid, | ||
'event_class' => self::getEventClass(get_class($event)), | ||
'meta_data' => json_encode([]), | ||
'created_at' => Carbon::now(), | ||
]); | ||
|
||
$eloquentStoredEvent->save(); | ||
|
||
return $eloquentStoredEvent->toStoredEvent(); | ||
} | ||
|
||
public function persistMany(array $events, string $uuid = null): Collection | ||
{ | ||
$storedEvents = []; | ||
|
||
foreach ($events as $event) { | ||
$storedEvents[] = self::persist($event, $uuid); | ||
} | ||
|
||
return collect($storedEvents); | ||
} | ||
|
||
public function update(StoredEvent $storedEvent): StoredEvent | ||
{ | ||
/** @var EloquentStoredEvent $storedEvent */ | ||
$eloquentStoredEvent = EloquentStoredEvent::find($storedEvent->id); | ||
|
||
$eloquentStoredEvent->update($storedEvent->toArray()); | ||
|
||
return $eloquentStoredEvent->toStoredEvent(); | ||
} | ||
|
||
private function getEventClass(string $class): string | ||
{ | ||
$map = config('event-projector.event_class_map', []); | ||
|
||
if (! empty($map) && in_array($class, $map)) { | ||
return array_search($class, $map, true); | ||
} | ||
|
||
return $class; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
|
||
namespace Spatie\EventProjector\Models; | ||
|
||
use Illuminate\Database\Eloquent\Model; | ||
use Illuminate\Database\Eloquent\Builder; | ||
use Spatie\EventProjector\ShouldBeStored; | ||
|
||
class EloquentStoredEvent extends Model | ||
{ | ||
public $guarded = []; | ||
|
||
public $timestamps = false; | ||
|
||
protected $table = 'stored_events'; | ||
|
||
public $casts = [ | ||
'event_properties' => 'array', | ||
'meta_data' => 'array', | ||
]; | ||
|
||
public function toStoredEvent(): StoredEvent | ||
{ | ||
return new StoredEvent([ | ||
'id' => $this->id, | ||
'event_properties' => $this->event_properties, | ||
'aggregate_uuid' => $this->aggregate_uuid, | ||
'event_class' => $this->event_class, | ||
'meta_data' => $this->meta_data, | ||
'created_at' => $this->created_at, | ||
]); | ||
} | ||
|
||
public function getEventAttribute(): ShouldBeStored | ||
{ | ||
return $this->toStoredEvent()->event; | ||
} | ||
|
||
public function scopeStartingFrom(Builder $query, int $storedEventId): void | ||
{ | ||
$query->where('id', '>=', $storedEventId); | ||
} | ||
|
||
public function scopeUuid(Builder $query, string $uuid): void | ||
{ | ||
$query->where('aggregate_uuid', $uuid); | ||
} | ||
} |
Oops, something went wrong.