Completed
Push — master ( 514e5e...49f70a )
by Freek
14s queued 10s
created

AggregateRoot   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

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

9 Methods

Rating   Name   Duplication   Size   Complexity  
A retrieve() 0 8 1
A recordThat() 0 8 1
A persist() 0 10 1
A getStoredEventModel() 0 4 1
A getRecordedEvents() 0 4 1
A getAndClearRecordedEvents() 0 8 1
A reconstituteFromEvents() 0 8 1
A apply() 0 12 2
A fake() 0 4 1
1
<?php
2
3
namespace Spatie\EventProjector;
4
5
use Illuminate\Support\Str;
6
use Spatie\EventProjector\Models\StoredEvent;
7
8
abstract class AggregateRoot
9
{
10
    /** @var string */
11
    private $aggregateUuid;
12
13
    /** @var array */
14
    private $recordedEvents = [];
15
16
    public static function retrieve(string $uuid): AggregateRoot
17
    {
18
        $aggregateRoot = (new static());
19
20
        $aggregateRoot->aggregateUuid = $uuid;
21
22
        return $aggregateRoot->reconstituteFromEvents();
23
    }
24
25
    public function recordThat(ShouldBeStored $domainEvent): AggregateRoot
26
    {
27
        $this->recordedEvents[] = $domainEvent;
28
29
        $this->apply($domainEvent);
30
31
        return $this;
32
    }
33
34
    public function persist(): AggregateRoot
35
    {
36
        call_user_func(
37
            [$this->getStoredEventModel(), 'storeMany'],
38
            $this->getAndClearRecordedEvents(),
39
            $this->aggregateUuid
40
        );
41
42
        return $this;
43
    }
44
45
    protected function getStoredEventModel(): string
46
    {
47
        return $this->storedEventModel ?? config('event-projector.stored_event_model');
0 ignored issues
show
Bug introduced by
The property storedEventModel does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
48
    }
49
50
    public function getRecordedEvents(): array
51
    {
52
        return $this->recordedEvents;
53
    }
54
55
    private function getAndClearRecordedEvents(): array
56
    {
57
        $recordedEvents = $this->recordedEvents;
58
59
        $this->recordedEvents = [];
60
61
        return $recordedEvents;
62
    }
63
64
    private function reconstituteFromEvents(): AggregateRoot
65
    {
66
        $this->getStoredEventModel()::uuid($this->aggregateUuid)->each(function (StoredEvent $storedEvent) {
0 ignored issues
show
Bug introduced by
The method uuid cannot be called on $this->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...
67
            $this->apply($storedEvent->event);
68
        });
69
70
        return $this;
71
    }
72
73
    private function apply(ShouldBeStored $event): void
74
    {
75
        $classBaseName = class_basename($event);
76
77
        $camelCasedBaseName = ucfirst(Str::camel($classBaseName));
78
79
        $applyingMethodName = "apply{$camelCasedBaseName}";
80
81
        if (method_exists($this, $applyingMethodName)) {
82
            $this->$applyingMethodName($event);
83
        }
84
    }
85
86
    public static function fake(array $givenEvents = []): FakeAggregateRoot
87
    {
88
        return (new FakeAggregateRoot(app(static::class)))->given($givenEvents);
89
    }
90
}
91