Completed
Push — master ( 8bfaf6...ca95b3 )
by Freek
07:02
created

StoredEvent::getEventAttribute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 7
c 1
b 0
f 0
rs 9.4285
cc 1
eloc 4
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 $casts = [
14
        'event_properties' => 'array',
15
    ];
16
17
    public static function createForEvent(ShouldBeStored $event): self
18
    {
19
        $storedEvent = new static();
20
        $storedEvent->event_class = get_class($event);
21
        $storedEvent->attributes['event_properties'] = app(EventSerializer::class)->serialize(clone $event);
22
        $storedEvent->save();
23
24
        return $storedEvent;
25
    }
26
27
    public function getEventAttribute(): ShouldBeStored
28
    {
29
        return app(EventSerializer::class)->deserialize(
30
           $this->event_class,
31
           $this->getOriginal('event_properties')
32
       );
33
    }
34
}
35