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

StoredEvent   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
lcom 1
cbo 1
dl 0
loc 34
rs 10
c 1
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A createForEvent() 0 10 1
A getMaxId() 0 4 1
A getEventAttribute() 0 7 1
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