1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\EventProjector; |
4
|
|
|
|
5
|
|
|
use Carbon\Carbon; |
6
|
|
|
use Illuminate\Support\Collection; |
7
|
|
|
use Spatie\EventProjector\Models\EloquentStoredEvent; |
8
|
|
|
use Spatie\EventProjector\Models\StoredEvent; |
9
|
|
|
use Spatie\EventProjector\EventSerializers\EventSerializer; |
10
|
|
|
|
11
|
|
|
class EloquentStoredEventRepository implements StoredEventRepository |
12
|
|
|
{ |
13
|
|
|
public static function retrieveAll(string $uuid = null, int $startingFrom = null): Collection |
14
|
|
|
{ |
15
|
|
|
$query = self::getStoredEventModel()::query(); |
|
|
|
|
16
|
|
|
|
17
|
|
|
if ($uuid) { |
|
|
|
|
18
|
|
|
$query->uuid($uuid); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
if ($startingFrom) { |
|
|
|
|
22
|
|
|
$query->startingFrom($startingFrom); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
return $query->get()->map(function (EloquentStoredEvent $storedEvent) { |
26
|
|
|
return $storedEvent->toStoredEventData(); |
27
|
|
|
}); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public static function persist(ShouldBeStored $event, string $uuid = null, string $model = null): StoredEvent |
31
|
|
|
{ |
32
|
|
|
/** @var EloquentStoredEvent $storedEvent */ |
33
|
|
|
$storedEvent = self::getStoredEventModel($model)::make(); |
|
|
|
|
34
|
|
|
|
35
|
|
|
$storedEvent->setRawAttributes([ |
36
|
|
|
'event_properties' => app(EventSerializer::class)->serialize(clone $event), |
37
|
|
|
'aggregate_uuid' => $uuid, |
38
|
|
|
'event_class' => self::getEventClass(get_class($event)), |
39
|
|
|
'meta_data' => json_encode([]), |
40
|
|
|
'created_at' => Carbon::now(), |
41
|
|
|
]); |
42
|
|
|
|
43
|
|
|
$storedEvent->save(); |
44
|
|
|
|
45
|
|
|
return $storedEvent->toStoredEventData(); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public static function persistMany(array $events, string $uuid = null, string $model = null): Collection |
49
|
|
|
{ |
50
|
|
|
$storedEvents = []; |
51
|
|
|
|
52
|
|
|
foreach ($events as $event) { |
53
|
|
|
$storedEvents[] = self::persist($event, $uuid, $model); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
return collect($storedEvents); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public static function update(StoredEvent $storedEventData): StoredEvent |
60
|
|
|
{ |
61
|
|
|
$storedEvent = self::getStoredEventModel()::find($storedEventData->id); |
|
|
|
|
62
|
|
|
|
63
|
|
|
$storedEvent->update($storedEventData->toArray()); |
64
|
|
|
|
65
|
|
|
return $storedEvent->toStoredEventData(); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
View Code Duplication |
private static function getEventClass(string $class): string |
|
|
|
|
69
|
|
|
{ |
70
|
|
|
$map = config('event-projector.event_class_map', []); |
71
|
|
|
|
72
|
|
|
if (! empty($map) && in_array($class, $map)) { |
73
|
|
|
return array_search($class, $map, true); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
return $class; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
private static function getStoredEventModel(string $model = null): string |
80
|
|
|
{ |
81
|
|
|
return $model ?? config('event-projector.stored_event_model'); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.