Completed
Push — master ( 5827e9...0da941 )
by Rias
01:59 queued 11s
created

EloquentStoredEvent::getMetaDataAttribute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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