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'); |
|
|
|
|
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) { |
|
|
|
|
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
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: