Completed
Pull Request — master (#183)
by Rias
07:41
created

EloquentStoredEventRepository   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 70
Duplicated Lines 14.29 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 2
dl 10
loc 70
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A retrieveAll() 0 16 3
A persist() 0 16 1
A persistMany() 0 10 2
A update() 0 9 1
A getEventClass() 10 10 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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