Completed
Pull Request — master (#183)
by Rias
10:08
created

EloquentStoredEvent   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 3
dl 0
loc 40
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getEventAttribute() 0 4 1
A scopeStartingFrom() 0 4 1
A scopeUuid() 0 4 1
A toStoredEvent() 0 11 1
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 EloquentStoredEvent extends Model
10
{
11
    public $guarded = [];
12
13
    public $timestamps = false;
14
15
    protected $table = 'stored_events';
16
17
    public $casts = [
18
        'event_properties' => 'array',
19
        'meta_data' => 'array',
20
    ];
21
22
    public function toStoredEvent(): StoredEvent
23
    {
24
        return new StoredEvent([
25
            'id' => $this->id,
26
            'event_properties' => $this->event_properties,
27
            'aggregate_uuid' => $this->aggregate_uuid,
28
            'event_class' => $this->event_class,
29
            'meta_data' => $this->meta_data,
30
            'created_at' => $this->created_at,
31
        ]);
32
    }
33
34
    public function getEventAttribute(): ShouldBeStored
35
    {
36
        return $this->toStoredEvent()->event;
37
    }
38
39
    public function scopeStartingFrom(Builder $query, int $storedEventId): void
40
    {
41
        $query->where('id', '>=', $storedEventId);
42
    }
43
44
    public function scopeUuid(Builder $query, string $uuid): void
45
    {
46
        $query->where('aggregate_uuid', $uuid);
47
    }
48
}
49