Completed
Push — master ( f6c461...60f15f )
by Freek
14s queued 11s
created

EloquentStoredEventRepository::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
3
namespace Spatie\EventProjector;
4
5
use Carbon\Carbon;
6
use Illuminate\Support\LazyCollection;
7
use Spatie\EventProjector\Models\EloquentStoredEvent;
8
use Spatie\EventProjector\EventSerializers\EventSerializer;
9
use Spatie\EventProjector\Exceptions\InvalidEloquentStoredEventModel;
10
11
class EloquentStoredEventRepository implements StoredEventRepository
12
{
13
    protected $storedEventModel;
14
15
    public function __construct()
16
    {
17
        $this->storedEventModel = config('event-projector.stored_event_model', EloquentStoredEvent::class);
18
19
        if (! new $this->storedEventModel instanceof EloquentStoredEvent) {
20
            throw new InvalidEloquentStoredEventModel("The class {$this->storedEventModel} must extend EloquentStoredEvent");
21
        }
22
    }
23
24 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...
25
    {
26
        /** @var \Illuminate\Database\Query\Builder $query */
27
        $query = $this->storedEventModel::query();
28
29
        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...
30
            $query->uuid($uuid);
31
        }
32
33
        return $query->orderBy('id')->cursor()->map(function (EloquentStoredEvent $storedEvent) {
34
            return $storedEvent->toStoredEvent();
35
        });
36
    }
37
38 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...
39
    {
40
        /** @var \Illuminate\Database\Query\Builder $query */
41
        $query = $this->storedEventModel::query()->startingFrom($startingFrom);
42
43
        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...
44
            $query->uuid($uuid);
45
        }
46
47
        return $query->orderBy('id')->cursor()->map(function (EloquentStoredEvent $storedEvent) {
48
            return $storedEvent->toStoredEvent();
49
        });
50
    }
51
52
    public function persist(ShouldBeStored $event, string $uuid = null): StoredEvent
53
    {
54
        /** @var EloquentStoredEvent $eloquentStoredEvent */
55
        $eloquentStoredEvent = new $this->storedEventModel();
56
57
        $eloquentStoredEvent->setRawAttributes([
58
            'event_properties' => app(EventSerializer::class)->serialize(clone $event),
59
            'aggregate_uuid' => $uuid,
60
            'event_class' => self::getEventClass(get_class($event)),
61
            'meta_data' => json_encode([]),
62
            'created_at' => Carbon::now(),
63
        ]);
64
65
        $eloquentStoredEvent->save();
66
67
        return $eloquentStoredEvent->toStoredEvent();
68
    }
69
70
    public function persistMany(array $events, string $uuid = null): LazyCollection
71
    {
72
        $storedEvents = [];
73
74
        foreach ($events as $event) {
75
            $storedEvents[] = self::persist($event, $uuid);
76
        }
77
78
        return new LazyCollection($storedEvents);
79
    }
80
81
    public function update(StoredEvent $storedEvent): StoredEvent
82
    {
83
        /** @var EloquentStoredEvent $eloquentStoredEvent */
84
        $eloquentStoredEvent = $this->storedEventModel::find($storedEvent->id);
85
86
        $eloquentStoredEvent->update($storedEvent->toArray());
87
88
        return $eloquentStoredEvent->toStoredEvent();
89
    }
90
91 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...
92
    {
93
        $map = config('event-projector.event_class_map', []);
94
95
        if (! empty($map) && in_array($class, $map)) {
96
            return array_search($class, $map, true);
97
        }
98
99
        return $class;
100
    }
101
}
102