Completed
Pull Request — master (#183)
by Rias
01:48
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 3
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\StoredEventData;
9
use Spatie\EventProjector\EventSerializers\EventSerializer;
10
11
class EloquentStoredEventRepository implements StoredEventRepository
12
{
13
    public static function retrieveAll(string $uuid = null, int $startingFrom = null): Collection
14
    {
15
        $query = self::getStoredEventModel()::query();
0 ignored issues
show
Bug introduced by
The method query cannot be called on self::getStoredEventModel() (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
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 (StoredEvent $storedEvent) {
26
            return $storedEvent->toStoredEventData();
27
        });
28
    }
29
30
    public static function persist(ShouldBeStored $event, string $uuid = null, string $model = null): StoredEventData
31
    {
32
        /** @var StoredEvent $storedEvent */
33
        $storedEvent = self::getStoredEventModel($model)::make();
0 ignored issues
show
Bug introduced by
The method make cannot be called on self::getStoredEventModel($model) (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
34
35
        $storedEvent->setRawAttributes([
36
            'event_properties' => app(EventSerializer::class)->serialize(clone $event),
37
            'aggregate_uuid' => $uuid,
38
            'event_class' => self::getEventClass(get_class($event)),
39
            'meta_data' => json_encode([]),
40
            'created_at' => Carbon::now(),
41
        ]);
42
43
        $storedEvent->save();
44
45
        return $storedEvent->toStoredEventData();
46
    }
47
48
    public static function persistMany(array $events, string $uuid = null, string $model = null): Collection
49
    {
50
        $storedEvents = [];
51
52
        foreach ($events as $event) {
53
            $storedEvents[] = self::persist($event, $uuid, $model);
54
        }
55
56
        return collect($storedEvents);
57
    }
58
59
    public static function update(StoredEventData $storedEventData): StoredEventData
60
    {
61
        $storedEvent = self::getStoredEventModel()::find($storedEventData->id);
0 ignored issues
show
Bug introduced by
The method find cannot be called on self::getStoredEventModel() (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
62
63
        $storedEvent->update($storedEventData->toArray());
64
65
        return $storedEvent->toStoredEventData();
66
    }
67
68 View Code Duplication
    protected static 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
    private static function getStoredEventModel(string $model = null): string
80
    {
81
        return $model ?? config('event-projector.stored_event_model');
82
    }
83
}
84