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

StoredEvent   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 26
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A createForEvent() 0 9 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 $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