Completed
Push — master ( a0d1c0...57787d )
by Freek
01:36
created

StoredEvent::getMaxId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Spatie\EventProjector\Models;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Spatie\EventProjector\EventSerializers\EventSerializer;
7
use Spatie\EventProjector\ShouldBeStored;
8
9
class StoredEvent extends Model
10
{
11
    public $guarded = [];
12
13
    public $timestamps = false;
14
15
    public $casts = [
16
        'event_properties' => 'array',
17
    ];
18
19
    public static function createForEvent(ShouldBeStored $event): self
20
    {
21
        $storedEvent = new static();
22
        $storedEvent->event_class = get_class($event);
23
        $storedEvent->attributes['event_properties'] = app(EventSerializer::class)->serialize(clone $event);
24
        $storedEvent->created_at = now();
25
        $storedEvent->save();
26
27
        return $storedEvent;
28
    }
29
30
    public static function getMaxId(): int
31
    {
32
        return DB::table((new static())->getTable())->max('id') ?? 0;
33
    }
34
35
    public function getEventAttribute(): ShouldBeStored
36
    {
37
        return app(EventSerializer::class)->deserialize(
38
           $this->event_class,
39
           $this->getOriginal('event_properties')
40
       );
41
    }
42
}
43