Completed
Pull Request — master (#183)
by Rias
01:19
created

StoredEvent::toStoredEventData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Spatie\EventProjector\Models;
4
5
use Illuminate\Support\Arr;
6
use Illuminate\Database\Eloquent\Model;
7
use Illuminate\Database\Eloquent\Builder;
8
use Spatie\EventProjector\ShouldBeStored;
9
10
class StoredEvent extends Model
11
{
12
    public $guarded = [];
13
14
    public $timestamps = false;
15
16
    public $casts = [
17
        'event_properties' => 'array',
18
        'meta_data' => 'array',
19
    ];
20
21
    public function toStoredEventData(): StoredEventData
22
    {
23
        return new StoredEventData([
24
            'id' => $this->id,
25
            'event_properties' => $this->event_properties,
26
            'aggregate_uuid' => $this->aggregate_uuid,
27
            'event_class' => $this->event_class,
28
            'meta_data' => $this->meta_data,
29
            'created_at' => $this->created_at,
30
        ]);
31
    }
32
33
    public function getEventAttribute(): ShouldBeStored
34
    {
35
        return $this->toStoredEventData()->event;
36
    }
37
38
    public function scopeStartingFrom(Builder $query, int $storedEventId): void
39
    {
40
        $query->where('id', '>=', $storedEventId);
41
    }
42
43
    public function scopeUuid(Builder $query, string $uuid): void
44
    {
45
        $query->where('aggregate_uuid', $uuid);
46
    }
47
}
48