Completed
Push — master ( c1932e...0de009 )
by Freek
27s queued 11s
created

EloquentStoredEventRepository::persist()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 9.7
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
namespace Spatie\EventProjector;
4
5
use Carbon\Carbon;
6
use Illuminate\Support\LazyCollection;
7
use Spatie\EventProjector\Models\StoredEvent;
8
use Spatie\EventProjector\Models\EloquentStoredEvent;
9
use Spatie\EventProjector\EventSerializers\EventSerializer;
10
11
class EloquentStoredEventRepository implements StoredEventRepository
12
{
13
    protected $storedEventModel = EloquentStoredEvent::class;
14
15 View Code Duplication
    public function retrieveAll(string $uuid = null): LazyCollection
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
16
    {
17
        /** @var \Illuminate\Database\Query\Builder $query */
18
        $query = $this->storedEventModel::query();
19
20
        if ($uuid) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $uuid of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
21
            $query->uuid($uuid);
22
        }
23
24
        return $query->cursor()->map(function (EloquentStoredEvent $storedEvent) {
25
            return $storedEvent->toStoredEvent();
26
        });
27
    }
28
29 View Code Duplication
    public function retrieveAllStartingFrom(int $startingFrom, string $uuid = null): LazyCollection
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
30
    {
31
        /** @var \Illuminate\Database\Query\Builder $query */
32
        $query = $this->storedEventModel::query()->startingFrom($startingFrom);
33
34
        if ($uuid) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $uuid of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
35
            $query->uuid($uuid);
36
        }
37
38
        return $query->cursor()->map(function (EloquentStoredEvent $storedEvent) {
39
            return $storedEvent->toStoredEvent();
40
        });
41
    }
42
43
    public function persist(ShouldBeStored $event, string $uuid = null): StoredEvent
44
    {
45
        /** @var EloquentStoredEvent $eloquentStoredEvent */
46
        $eloquentStoredEvent = new $this->storedEventModel();
47
48
        $eloquentStoredEvent->setRawAttributes([
49
            'event_properties' => app(EventSerializer::class)->serialize(clone $event),
50
            'aggregate_uuid' => $uuid,
51
            'event_class' => self::getEventClass(get_class($event)),
52
            'meta_data' => json_encode([]),
53
            'created_at' => Carbon::now(),
54
        ]);
55
56
        $eloquentStoredEvent->save();
57
58
        return $eloquentStoredEvent->toStoredEvent();
59
    }
60
61
    public function persistMany(array $events, string $uuid = null): LazyCollection
62
    {
63
        $storedEvents = [];
64
65
        foreach ($events as $event) {
66
            $storedEvents[] = self::persist($event, $uuid);
67
        }
68
69
        return new LazyCollection($storedEvents);
70
    }
71
72
    public function update(StoredEvent $storedEvent): StoredEvent
73
    {
74
        /** @var EloquentStoredEvent $eloquentStoredEvent */
75
        $eloquentStoredEvent = $this->storedEventModel::find($storedEvent->id);
76
77
        $eloquentStoredEvent->update($storedEvent->toArray());
78
79
        return $eloquentStoredEvent->toStoredEvent();
80
    }
81
82 View Code Duplication
    private function getEventClass(string $class): string
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
83
    {
84
        $map = config('event-projector.event_class_map', []);
85
86
        if (! empty($map) && in_array($class, $map)) {
87
            return array_search($class, $map, true);
88
        }
89
90
        return $class;
91
    }
92
}
93