|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Spatie\EventProjector; |
|
4
|
|
|
|
|
5
|
|
|
use Carbon\Carbon; |
|
6
|
|
|
use Illuminate\Support\Collection; |
|
7
|
|
|
use Spatie\EventProjector\EventSerializers\EventSerializer; |
|
8
|
|
|
use Spatie\EventProjector\Models\StoredEvent; |
|
9
|
|
|
use Spatie\EventProjector\Models\StoredEventData; |
|
10
|
|
|
|
|
11
|
|
|
class EloquentStoredEventRepository implements StoredEventRepository |
|
12
|
|
|
{ |
|
13
|
|
|
public static function retrieveAll(string $uuid = null, int $startingFrom = null): Collection |
|
14
|
|
|
{ |
|
15
|
|
|
$query = StoredEvent::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 (StoredEvent $storedEvent) { |
|
26
|
|
|
return $storedEvent->toStoredEventData(); |
|
27
|
|
|
}); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
public static function persist(ShouldBeStored $event, string $uuid = null): StoredEventData |
|
31
|
|
|
{ |
|
32
|
|
|
/** @var StoredEvent $storedEvent */ |
|
33
|
|
|
$storedEvent = new StoredEvent(); |
|
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): Collection |
|
49
|
|
|
{ |
|
50
|
|
|
$storedEvents = []; |
|
51
|
|
|
|
|
52
|
|
|
foreach ($events as $event) { |
|
53
|
|
|
$storedEvents[] = self::persist($event, $uuid); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
return collect($storedEvents); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
public static function update(StoredEventData $storedEventData): StoredEventData |
|
60
|
|
|
{ |
|
61
|
|
|
$storedEvent = StoredEvent::find($storedEventData->id); |
|
62
|
|
|
|
|
63
|
|
|
$storedEvent->update($storedEventData->toArray()); |
|
64
|
|
|
|
|
65
|
|
|
return $storedEvent->toStoredEventData(); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
View Code Duplication |
protected 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
|
|
|
|
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: