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

EloquentStoredEventRepository::retrieveAll()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.7333
c 0
b 0
f 0
cc 3
nc 4
nop 2
1
<?php
2
3
namespace Spatie\EventProjector;
4
5
use Carbon\Carbon;
6
use Illuminate\Support\Collection;
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
    public function retrieveAll(string $uuid = null, int $startingFrom = null): Collection
14
    {
15
        $query = EloquentStoredEvent::query();
16
17
        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...
18
            $query->uuid($uuid);
19
        }
20
21
        if ($startingFrom) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $startingFrom of type null|integer is loosely compared to true; this is ambiguous if the integer can be zero. 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 integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
22
            $query->startingFrom($startingFrom);
23
        }
24
25
        return $query->get()->map(function (EloquentStoredEvent $storedEvent) {
26
            return $storedEvent->toStoredEvent();
27
        });
28
    }
29
30
    public function persist(ShouldBeStored $event, string $uuid = null): StoredEvent
31
    {
32
        $eloquentStoredEvent = new EloquentStoredEvent();
33
34
        $eloquentStoredEvent->setRawAttributes([
35
            'event_properties' => app(EventSerializer::class)->serialize(clone $event),
36
            'aggregate_uuid' => $uuid,
37
            'event_class' => self::getEventClass(get_class($event)),
38
            'meta_data' => json_encode([]),
39
            'created_at' => Carbon::now(),
40
        ]);
41
42
        $eloquentStoredEvent->save();
43
44
        return $eloquentStoredEvent->toStoredEvent();
45
    }
46
47
    public function persistMany(array $events, string $uuid = null): Collection
48
    {
49
        $storedEvents = [];
50
51
        foreach ($events as $event) {
52
            $storedEvents[] = self::persist($event, $uuid);
53
        }
54
55
        return collect($storedEvents);
56
    }
57
58
    public function update(StoredEvent $storedEvent): StoredEvent
59
    {
60
        /** @var EloquentStoredEvent $storedEvent */
61
        $eloquentStoredEvent = EloquentStoredEvent::find($storedEvent->id);
62
63
        $eloquentStoredEvent->update($storedEvent->toArray());
64
65
        return $eloquentStoredEvent->toStoredEvent();
66
    }
67
68 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...
69
    {
70
        $map = config('event-projector.event_class_map', []);
71
72
        if (! empty($map) && in_array($class, $map)) {
73
            return array_search($class, $map, true);
74
        }
75
76
        return $class;
77
    }
78
}
79