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

StoredEvent::scopeStartingFrom()   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 2
1
<?php
2
3
namespace Spatie\EventProjector\Models;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Illuminate\Database\Eloquent\Builder;
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
        'meta_data' => 'array',
18
    ];
19
20
    public function toStoredEventData(): StoredEventData
21
    {
22
        return new StoredEventData([
23
            'id' => $this->id,
24
            'event_properties' => $this->event_properties,
25
            'aggregate_uuid' => $this->aggregate_uuid,
26
            'event_class' => $this->event_class,
27
            'meta_data' => $this->meta_data,
28
            'created_at' => $this->created_at,
29
        ]);
30
    }
31
32
    public function getEventAttribute(): ShouldBeStored
33
    {
34
        return $this->toStoredEventData()->event;
35
    }
36
37
    public function scopeStartingFrom(Builder $query, int $storedEventId): void
38
    {
39
        $query->where('id', '>=', $storedEventId);
40
    }
41
42
    public function scopeUuid(Builder $query, string $uuid): void
43
    {
44
        $query->where('aggregate_uuid', $uuid);
45
    }
46
}
47