Completed
Push — master ( 9e0b65...876221 )
by Freek
08:47 queued 05:37
created

StoredEvent::previousInStream()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 8
nc 1
nop 0
1
<?php
2
3
namespace Spatie\EventProjector\Models;
4
5
use Illuminate\Support\Facades\DB;
6
use Illuminate\Database\Eloquent\Model;
7
use Illuminate\Database\Eloquent\Builder;
8
use Spatie\EventProjector\ShouldBeStored;
9
use Spatie\SchemalessAttributes\SchemalessAttributes;
10
use Spatie\EventProjector\EventSerializers\EventSerializer;
11
12
class StoredEvent extends Model
13
{
14
    public $guarded = [];
15
16
    public $timestamps = false;
17
18
    public $casts = [
19
        'event_properties' => 'array',
20
        'meta_data' => 'array',
21
    ];
22
23
    public static function createForEvent(ShouldBeStored $event): self
24
    {
25
        $storedEvent = new static();
26
        $storedEvent->event_class = get_class($event);
27
        $storedEvent->attributes['event_properties'] = app(EventSerializer::class)->serialize(clone $event);
28
        $storedEvent->created_at = now();
29
30
        if (method_exists($event, 'getStreamName')) {
31
            $storedEvent->stream_name = $event->getStreamName();
0 ignored issues
show
Bug introduced by
The method getStreamName() does not seem to exist on object<Spatie\EventProjector\ShouldBeStored>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
32
        }
33
34
        if (method_exists($event, 'getStreamId')) {
35
            $storedEvent->stream_id = $event->getStreamId();
0 ignored issues
show
Bug introduced by
The method getStreamId() does not seem to exist on object<Spatie\EventProjector\ShouldBeStored>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
36
        }
37
38
        $storedEvent->save();
39
40
        return $storedEvent;
41
    }
42
43
    public static function getMaxId(): int
44
    {
45
        return DB::table((new static())->getTable())->max('id') ?? 0;
46
    }
47
48
    public static function last(): ?StoredEvent
49
    {
50
        return static::find(self::getMaxId());
51
    }
52
53
    public function getEventAttribute(): ShouldBeStored
54
    {
55
        return app(EventSerializer::class)->deserialize(
56
           $this->event_class,
57
           $this->getOriginal('event_properties')
58
       );
59
    }
60
61
    public function getMetaDataAttribute(): SchemalessAttributes
62
    {
63
        return SchemalessAttributes::createForModel($this, 'meta_data');
64
    }
65
66
    public function scopeWithMetaDataAttributes(): Builder
67
    {
68
        return SchemalessAttributes::scopeWithSchemalessAttributes('meta_data');
69
    }
70
71
    public function scopeAfter(Builder $query, int $storedEventId)
72
    {
73
        $query->where('id', '>', $storedEventId);
74
    }
75
76
    public function scopePrevious(Builder $query, StoredEvent $storedEvent): ?StoredEvent
77
    {
78
        static::query()
79
            ->where('event_class', $storedEvent->event_class)
80
            ->latest()
81
            ->first();
82
    }
83
84
    public function previousInStream(): ?StoredEvent
85
    {
86
        return static::query()
0 ignored issues
show
Bug introduced by
The method orderBy() does not exist on Illuminate\Database\Eloquent\Builder. Did you maybe mean enforceOrderBy()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
87
            ->where('event_class', $this->event_class)
88
            ->where('stream_name', $this->stream_name)
89
            ->where('stream_id', $this->stream_id)
90
            ->where('id','<', $this->id)
91
            ->orderBy('id', 'desc')
92
            ->first();
93
    }
94
}
95