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

AggregateRoot::retrieve()   A

Complexity

Conditions 1
Paths 1

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 1
nc 1
nop 1
1
<?php
2
3
namespace Spatie\EventProjector;
4
5
use Illuminate\Support\Arr;
6
use Illuminate\Support\Str;
7
use Spatie\EventProjector\Models\StoredEvent;
8
9
abstract class AggregateRoot
10
{
11
    /** @var string */
12
    private $aggregateUuid;
13
14
    /** @var array */
15
    private $recordedEvents = [];
16
17
    public static function retrieve(string $uuid): AggregateRoot
18
    {
19
        $aggregateRoot = (new static());
20
21
        $aggregateRoot->aggregateUuid = $uuid;
22
23
        return $aggregateRoot->reconstituteFromEvents();
24
    }
25
26
    public function recordThat(ShouldBeStored $domainEvent): AggregateRoot
27
    {
28
        $this->recordedEvents[] = $domainEvent;
29
30
        $this->apply($domainEvent);
31
32
        return $this;
33
    }
34
35
    public function persist(): AggregateRoot
36
    {
37
        $storedEvents = call_user_func(
38
            [$this->getStoredEventRepository(), 'persistMany'],
39
            $this->getAndClearRecordedEvents(),
40
            $this->aggregateUuid,
41
            $this->getStoredEventModel()
42
        );
43
44
        $storedEvents->each(function (StoredEvent $storedEventData) {
45
            $storedEventData->handle();
46
        });
47
48
        return $this;
49
    }
50
51
    protected function getStoredEventModel(): string
52
    {
53
        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...
54
    }
55
56
    protected function getStoredEventRepository(): string
57
    {
58
        return $this->storedEventRepository ?? config('event-projector.stored_event_repository');
0 ignored issues
show
Bug introduced by
The property storedEventRepository 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...
59
    }
60
61
    public function getRecordedEvents(): array
62
    {
63
        return $this->recordedEvents;
64
    }
65
66
    private function getAndClearRecordedEvents(): array
67
    {
68
        $recordedEvents = $this->recordedEvents;
69
70
        $this->recordedEvents = [];
71
72
        return $recordedEvents;
73
    }
74
75
    private function reconstituteFromEvents(): AggregateRoot
76
    {
77
        $this->getStoredEventRepository()::retrieveAll($this->aggregateUuid)
0 ignored issues
show
Bug introduced by
The method retrieveAll cannot be called on $this->getStoredEventRepository() (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...
78
            ->each(function (StoredEvent $storedEvent) {
79
                $this->apply($storedEvent->event);
0 ignored issues
show
Bug introduced by
It seems like $storedEvent->event can be null; however, apply() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
80
            });
81
82
        return $this;
83
    }
84
85
    private function apply(ShouldBeStored $event): void
86
    {
87
        $classBaseName = class_basename($event);
88
89
        $camelCasedBaseName = ucfirst(Str::camel($classBaseName));
90
91
        $applyingMethodName = "apply{$camelCasedBaseName}";
92
93
        if (method_exists($this, $applyingMethodName)) {
94
            $this->$applyingMethodName($event);
95
        }
96
    }
97
98
    /**
99
     * @param \Spatie\EventProjector\ShouldBeStored|\Spatie\EventProjector\ShouldBeStored[] $events
100
     *
101
     * @return $this
102
     */
103
    public static function fake($events = []): FakeAggregateRoot
104
    {
105
        $events = Arr::wrap($events);
106
107
        return (new FakeAggregateRoot(app(static::class)))->given($events);
108
    }
109
}
110