|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Spatie\EventProjector\Models; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Support\Facades\DB; |
|
6
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
7
|
|
|
use Illuminate\Database\Eloquent\Builder; |
|
8
|
|
|
use Spatie\EventProjector\ShouldBeStored; |
|
9
|
|
|
use Spatie\SchemalessAttributes\SchemalessAttributes; |
|
10
|
|
|
use Spatie\EventProjector\EventSerializers\EventSerializer; |
|
11
|
|
|
|
|
12
|
|
|
class StoredEvent extends Model |
|
13
|
|
|
{ |
|
14
|
|
|
public $guarded = []; |
|
15
|
|
|
|
|
16
|
|
|
public $timestamps = false; |
|
17
|
|
|
|
|
18
|
|
|
public $casts = [ |
|
19
|
|
|
'event_properties' => 'array', |
|
20
|
|
|
'meta_data' => 'array', |
|
21
|
|
|
]; |
|
22
|
|
|
|
|
23
|
|
|
public static function createForEvent(ShouldBeStored $event): self |
|
24
|
|
|
{ |
|
25
|
|
|
$storedEvent = new static(); |
|
26
|
|
|
$storedEvent->event_class = get_class($event); |
|
27
|
|
|
$storedEvent->attributes['event_properties'] = app(EventSerializer::class)->serialize(clone $event); |
|
28
|
|
|
$storedEvent->created_at = now(); |
|
29
|
|
|
|
|
30
|
|
|
if (method_exists($event, 'getStreamName')) { |
|
31
|
|
|
$storedEvent->stream_name = $event->getStreamName(); |
|
|
|
|
|
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
if (method_exists($event, 'getStreamId')) { |
|
35
|
|
|
$storedEvent->stream_id = $event->getStreamId(); |
|
|
|
|
|
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
$storedEvent->save(); |
|
39
|
|
|
|
|
40
|
|
|
return $storedEvent; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public static function getMaxId(): int |
|
44
|
|
|
{ |
|
45
|
|
|
return DB::table((new static())->getTable())->max('id') ?? 0; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
public static function last(): ?StoredEvent |
|
49
|
|
|
{ |
|
50
|
|
|
return static::find(self::getMaxId()); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
public function getEventAttribute(): ShouldBeStored |
|
54
|
|
|
{ |
|
55
|
|
|
return app(EventSerializer::class)->deserialize( |
|
56
|
|
|
$this->event_class, |
|
57
|
|
|
$this->getOriginal('event_properties') |
|
58
|
|
|
); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
public function getMetaDataAttribute(): SchemalessAttributes |
|
62
|
|
|
{ |
|
63
|
|
|
return SchemalessAttributes::createForModel($this, 'meta_data'); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
public function scopeWithMetaDataAttributes(): Builder |
|
67
|
|
|
{ |
|
68
|
|
|
return SchemalessAttributes::scopeWithSchemalessAttributes('meta_data'); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
public function scopeAfter(Builder $query, int $storedEventId) |
|
72
|
|
|
{ |
|
73
|
|
|
$query->where('id', '>', $storedEventId); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
public function scopePrevious(Builder $query, StoredEvent $storedEvent): ?StoredEvent |
|
77
|
|
|
{ |
|
78
|
|
|
static::query() |
|
79
|
|
|
->where('event_class', $storedEvent->event_class) |
|
80
|
|
|
->latest() |
|
81
|
|
|
->first(); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
public function previousInStream(): ?StoredEvent |
|
85
|
|
|
{ |
|
86
|
|
|
return static::query() |
|
|
|
|
|
|
87
|
|
|
->where('event_class', $this->event_class) |
|
88
|
|
|
->where('stream_name', $this->stream_name) |
|
89
|
|
|
->where('stream_id', $this->stream_id) |
|
90
|
|
|
->where('id','<', $this->id) |
|
91
|
|
|
->orderBy('id', 'desc') |
|
92
|
|
|
->first(); |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.