1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\EventProjector; |
4
|
|
|
|
5
|
|
|
use Carbon\Carbon; |
6
|
|
|
use Illuminate\Support\Collection; |
7
|
|
|
use Spatie\EventProjector\Models\StoredEvent; |
8
|
|
|
use Spatie\EventProjector\Models\EloquentStoredEvent; |
9
|
|
|
use Spatie\EventProjector\EventSerializers\EventSerializer; |
10
|
|
|
|
11
|
|
|
class EloquentStoredEventRepository implements StoredEventRepository |
12
|
|
|
{ |
13
|
|
|
protected $storedEventModel = EloquentStoredEvent::class; |
14
|
|
|
|
15
|
|
|
public function retrieveAll(string $uuid = null, int $startingFrom = null): Collection |
16
|
|
|
{ |
17
|
|
|
$query = $this->storedEventModel::query(); |
18
|
|
|
|
19
|
|
|
if ($uuid) { |
|
|
|
|
20
|
|
|
$query->uuid($uuid); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
if ($startingFrom) { |
|
|
|
|
24
|
|
|
$query->startingFrom($startingFrom); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
return $query->get()->map(function (EloquentStoredEvent $storedEvent) { |
28
|
|
|
return $storedEvent->toStoredEvent(); |
29
|
|
|
}); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function persist(ShouldBeStored $event, string $uuid = null): StoredEvent |
33
|
|
|
{ |
34
|
|
|
$eloquentStoredEvent = new $this->storedEventModel(); |
35
|
|
|
|
36
|
|
|
$eloquentStoredEvent->setRawAttributes([ |
37
|
|
|
'event_properties' => app(EventSerializer::class)->serialize(clone $event), |
38
|
|
|
'aggregate_uuid' => $uuid, |
39
|
|
|
'event_class' => self::getEventClass(get_class($event)), |
40
|
|
|
'meta_data' => json_encode([]), |
41
|
|
|
'created_at' => Carbon::now(), |
42
|
|
|
]); |
43
|
|
|
|
44
|
|
|
$eloquentStoredEvent->save(); |
45
|
|
|
|
46
|
|
|
return $eloquentStoredEvent->toStoredEvent(); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function persistMany(array $events, string $uuid = null): Collection |
50
|
|
|
{ |
51
|
|
|
$storedEvents = []; |
52
|
|
|
|
53
|
|
|
foreach ($events as $event) { |
54
|
|
|
$storedEvents[] = self::persist($event, $uuid); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return collect($storedEvents); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function update(StoredEvent $storedEvent): StoredEvent |
61
|
|
|
{ |
62
|
|
|
/** @var EloquentStoredEvent $storedEvent */ |
63
|
|
|
$eloquentStoredEvent = $this->storedEventModel::find($storedEvent->id); |
64
|
|
|
|
65
|
|
|
$eloquentStoredEvent->update($storedEvent->toArray()); |
66
|
|
|
|
67
|
|
|
return $eloquentStoredEvent->toStoredEvent(); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
View Code Duplication |
private function getEventClass(string $class): string |
|
|
|
|
71
|
|
|
{ |
72
|
|
|
$map = config('event-projector.event_class_map', []); |
73
|
|
|
|
74
|
|
|
if (! empty($map) && in_array($class, $map)) { |
75
|
|
|
return array_search($class, $map, true); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
return $class; |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: