1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\EventProjector; |
4
|
|
|
|
5
|
|
|
use Carbon\Carbon; |
6
|
|
|
use Illuminate\Support\LazyCollection; |
7
|
|
|
use Spatie\EventProjector\Models\EloquentStoredEvent; |
8
|
|
|
use Spatie\EventProjector\EventSerializers\EventSerializer; |
9
|
|
|
use Spatie\EventProjector\Exceptions\InvalidEloquentStoredEventModel; |
10
|
|
|
|
11
|
|
|
class EloquentStoredEventRepository implements StoredEventRepository |
12
|
|
|
{ |
13
|
|
|
protected $storedEventModel; |
14
|
|
|
|
15
|
|
|
public function __construct() |
16
|
|
|
{ |
17
|
|
|
$this->storedEventModel = config('event-projector.stored_event_model', EloquentStoredEvent::class); |
18
|
|
|
|
19
|
|
|
if (! new $this->storedEventModel instanceof EloquentStoredEvent) { |
20
|
|
|
throw new InvalidEloquentStoredEventModel("The class {$this->storedEventModel} must extend EloquentStoredEvent"); |
21
|
|
|
} |
22
|
|
|
} |
23
|
|
|
|
24
|
|
View Code Duplication |
public function retrieveAll(string $uuid = null): LazyCollection |
|
|
|
|
25
|
|
|
{ |
26
|
|
|
/** @var \Illuminate\Database\Query\Builder $query */ |
27
|
|
|
$query = $this->storedEventModel::query(); |
28
|
|
|
|
29
|
|
|
if ($uuid) { |
|
|
|
|
30
|
|
|
$query->uuid($uuid); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
return $query->orderBy('id')->cursor()->map(function (EloquentStoredEvent $storedEvent) { |
34
|
|
|
return $storedEvent->toStoredEvent(); |
35
|
|
|
}); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
View Code Duplication |
public function retrieveAllStartingFrom(int $startingFrom, string $uuid = null): LazyCollection |
|
|
|
|
39
|
|
|
{ |
40
|
|
|
/** @var \Illuminate\Database\Query\Builder $query */ |
41
|
|
|
$query = $this->storedEventModel::query()->startingFrom($startingFrom); |
42
|
|
|
|
43
|
|
|
if ($uuid) { |
|
|
|
|
44
|
|
|
$query->uuid($uuid); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
return $query->orderBy('id')->cursor()->map(function (EloquentStoredEvent $storedEvent) { |
48
|
|
|
return $storedEvent->toStoredEvent(); |
49
|
|
|
}); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function persist(ShouldBeStored $event, string $uuid = null): StoredEvent |
53
|
|
|
{ |
54
|
|
|
/** @var EloquentStoredEvent $eloquentStoredEvent */ |
55
|
|
|
$eloquentStoredEvent = new $this->storedEventModel(); |
56
|
|
|
|
57
|
|
|
$eloquentStoredEvent->setRawAttributes([ |
58
|
|
|
'event_properties' => app(EventSerializer::class)->serialize(clone $event), |
59
|
|
|
'aggregate_uuid' => $uuid, |
60
|
|
|
'event_class' => self::getEventClass(get_class($event)), |
61
|
|
|
'meta_data' => json_encode([]), |
62
|
|
|
'created_at' => Carbon::now(), |
63
|
|
|
]); |
64
|
|
|
|
65
|
|
|
$eloquentStoredEvent->save(); |
66
|
|
|
|
67
|
|
|
return $eloquentStoredEvent->toStoredEvent(); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function persistMany(array $events, string $uuid = null): LazyCollection |
71
|
|
|
{ |
72
|
|
|
$storedEvents = []; |
73
|
|
|
|
74
|
|
|
foreach ($events as $event) { |
75
|
|
|
$storedEvents[] = self::persist($event, $uuid); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
return new LazyCollection($storedEvents); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function update(StoredEvent $storedEvent): StoredEvent |
82
|
|
|
{ |
83
|
|
|
/** @var EloquentStoredEvent $eloquentStoredEvent */ |
84
|
|
|
$eloquentStoredEvent = $this->storedEventModel::find($storedEvent->id); |
85
|
|
|
|
86
|
|
|
$eloquentStoredEvent->update($storedEvent->toArray()); |
87
|
|
|
|
88
|
|
|
return $eloquentStoredEvent->toStoredEvent(); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
View Code Duplication |
private function getEventClass(string $class): string |
|
|
|
|
92
|
|
|
{ |
93
|
|
|
$map = config('event-projector.event_class_map', []); |
94
|
|
|
|
95
|
|
|
if (! empty($map) && in_array($class, $map)) { |
96
|
|
|
return array_search($class, $map, true); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
return $class; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.