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->meta_data = []; |
29
|
|
|
$storedEvent->created_at = now(); |
30
|
|
|
|
31
|
|
|
$storedEvent->save(); |
32
|
|
|
|
33
|
|
|
return $storedEvent; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public static function getMaxId(): int |
37
|
|
|
{ |
38
|
|
|
return DB::table((new static())->getTable())->max('id') ?? 0; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public static function last(): ?self |
42
|
|
|
{ |
43
|
|
|
return static::find(self::getMaxId()); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function getEventAttribute(): ShouldBeStored |
47
|
|
|
{ |
48
|
|
|
return app(EventSerializer::class)->deserialize( |
49
|
|
|
$this->event_class, |
50
|
|
|
$this->getOriginal('event_properties') |
51
|
|
|
); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function scopeAfter(Builder $query, int $storedEventId) |
55
|
|
|
{ |
56
|
|
|
$query->where('id', '>', $storedEventId); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function scopePrevious(Builder $query, self $storedEvent): ?self |
60
|
|
|
{ |
61
|
|
|
static::query() |
62
|
|
|
->where('event_class', $storedEvent->event_class) |
63
|
|
|
->latest() |
64
|
|
|
->first(); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function previousInStream(): ?self |
68
|
|
|
{ |
69
|
|
|
return static::query() |
|
|
|
|
70
|
|
|
->where('stream_name', $this->stream_name) |
71
|
|
|
->where('stream_id', $this->stream_id) |
72
|
|
|
->where('id', '<', $this->id) |
73
|
|
|
->orderBy('id', 'desc') |
74
|
|
|
->first(); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function groupProjectorStatusBy(): array |
78
|
|
|
{ |
79
|
|
|
return []; |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.