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'); |
|
|
|
|
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
protected function getStoredEventRepository(): string |
57
|
|
|
{ |
58
|
|
|
return $this->storedEventRepository ?? config('event-projector.stored_event_repository'); |
|
|
|
|
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) |
|
|
|
|
78
|
|
|
->each(function (StoredEvent $storedEvent) { |
79
|
|
|
$this->apply($storedEvent->event); |
|
|
|
|
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
|
|
|
|
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: