1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\EventProjector\Projectors; |
4
|
|
|
|
5
|
|
|
use Carbon\Carbon; |
6
|
|
|
use Illuminate\Support\Collection; |
7
|
|
|
use Spatie\EventProjector\Models\StoredEvent; |
8
|
|
|
use Spatie\EventProjector\Models\ProjectorStatus; |
9
|
|
|
use Spatie\EventProjector\EventHandlers\HandlesEvents; |
10
|
|
|
use Spatie\EventProjector\Exceptions\CouldNotResetProjector; |
11
|
|
|
|
12
|
|
|
trait ProjectsEvents |
13
|
|
|
{ |
14
|
|
|
use HandlesEvents; |
15
|
|
|
|
16
|
|
|
public function getName(): string |
17
|
|
|
{ |
18
|
|
|
if (isset($this->name)) { |
19
|
|
|
return $this->name; |
|
|
|
|
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
return get_class($this); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
public function rememberReceivedEvent(StoredEvent $storedEvent) |
26
|
|
|
{ |
27
|
|
|
foreach ($this->getEventStreamFullNames($storedEvent) as $streamName) { |
28
|
|
|
$status = $this->getStatus($streamName); |
29
|
|
|
|
30
|
|
|
$status->rememberLastProcessedEvent($storedEvent, $this); |
|
|
|
|
31
|
|
|
|
32
|
|
|
$streams = $this->getEventStreams($storedEvent); |
33
|
|
|
|
34
|
|
|
if ($streams->isEmpty()) { |
35
|
|
|
$lastStoredEvent = $this->getStoredEventClass()::query() |
|
|
|
|
36
|
|
|
->whereIn('event_class', $this->handles()) |
37
|
|
|
->orderBy('id', 'desc') |
38
|
|
|
->first(); |
39
|
|
|
|
40
|
|
|
$lastStoredEventId = (int) optional($lastStoredEvent)->id ?? 0; |
41
|
|
|
|
42
|
|
|
$status = $this->getStatus(); |
43
|
|
|
|
44
|
|
|
$status->last_processed_event_id === $lastStoredEventId |
|
|
|
|
45
|
|
|
? $status->markAsReceivedAllEvents() |
46
|
|
|
: $status->markasAsNotReceivedAllEvents(); |
47
|
|
|
|
48
|
|
|
return; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
foreach ($streams as $streamName => $streamValue) { |
52
|
|
|
$streamFullName = "{$streamName}-{$streamValue}"; |
53
|
|
|
$whereJsonClause = str_replace('.', '->', $streamName); |
54
|
|
|
|
55
|
|
|
$lastStoredEvent = $this->getStoredEventClass()::query() |
|
|
|
|
56
|
|
|
->whereIn('event_class', $this->handles()) |
57
|
|
|
->where("event_properties->{$whereJsonClause}", $streamValue) |
58
|
|
|
->orderBy('id', 'desc') |
59
|
|
|
->first(); |
60
|
|
|
|
61
|
|
|
$lastStoredEventId = (int) optional($lastStoredEvent)->id ?? 0; |
62
|
|
|
|
63
|
|
|
$status = $this->getStatus($streamFullName); |
64
|
|
|
|
65
|
|
|
$status->last_processed_event_id === $lastStoredEventId |
|
|
|
|
66
|
|
|
? $status->markAsReceivedAllEvents() |
67
|
|
|
: $status->markasAsNotReceivedAllEvents(); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function hasAlreadyReceivedEvent(StoredEvent $storedEvent): bool |
73
|
|
|
{ |
74
|
|
|
foreach ($this->getEventStreamFullNames($storedEvent) as $streamFullName) { |
75
|
|
|
$status = $this->getStatus($streamFullName); |
76
|
|
|
|
77
|
|
|
$lastProcessedEventId = (int) optional($status)->last_processed_event_id ?? 0; |
78
|
|
|
|
79
|
|
|
if ($storedEvent->id <= $lastProcessedEventId) { |
|
|
|
|
80
|
|
|
return true; |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return false; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public function hasReceivedAllPriorEvents(StoredEvent $storedEvent): bool |
88
|
|
|
{ |
89
|
|
|
$streams = $this->getEventStreams($storedEvent); |
90
|
|
|
|
91
|
|
|
if ($streams->isEmpty()) { |
92
|
|
|
$lastStoredEvent = $this->getStoredEventClass()::query() |
|
|
|
|
93
|
|
|
->whereIn('event_class', $this->handles()) |
94
|
|
|
->where('id', '<', $storedEvent->id) |
|
|
|
|
95
|
|
|
->orderBy('id', 'desc') |
96
|
|
|
->first(); |
97
|
|
|
|
98
|
|
|
$lastStoredEventId = (int) optional($lastStoredEvent)->id ?? 0; |
99
|
|
|
|
100
|
|
|
$status = $this->getStatus(); |
101
|
|
|
|
102
|
|
|
$lastProcessedEventId = (int) $status->last_processed_event_id ?? 0; |
|
|
|
|
103
|
|
|
|
104
|
|
|
if ($lastStoredEventId !== $lastProcessedEventId) { |
105
|
|
|
return false; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
return true; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
foreach ($streams as $streamName => $streamValue) { |
112
|
|
|
$streamFullName = "{$streamName}-{$streamValue}"; |
113
|
|
|
$whereJsonClause = str_replace('.', '->', $streamName); |
114
|
|
|
|
115
|
|
|
$lastStoredEvent = $this->getStoredEventClass()::query() |
|
|
|
|
116
|
|
|
->whereIn('event_class', $this->handles()) |
117
|
|
|
->where('id', '<', $storedEvent->id) |
|
|
|
|
118
|
|
|
->where("event_properties->{$whereJsonClause}", $streamValue) |
119
|
|
|
->orderBy('id', 'desc') |
120
|
|
|
->first(); |
121
|
|
|
|
122
|
|
|
$lastStoredEventId = (int) optional($lastStoredEvent)->id ?? 0; |
123
|
|
|
|
124
|
|
|
$status = $this->getStatus($streamFullName); |
125
|
|
|
$lastProcessedEventId = (int) $status->last_processed_event_id ?? 0; |
|
|
|
|
126
|
|
|
|
127
|
|
|
if ($lastStoredEventId !== $lastProcessedEventId) { |
128
|
|
|
return false; |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
return true; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
public function hasReceivedAllEvents(): bool |
136
|
|
|
{ |
137
|
|
|
return $this->getProjectorStatusClass()::hasReceivedAllEvents($this); |
|
|
|
|
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
public function markAsNotUpToDate(StoredEvent $storedEvent) |
141
|
|
|
{ |
142
|
|
|
foreach ($this->getEventStreamFullNames($storedEvent) as $streamName) { |
143
|
|
|
$status = $this->getStatus($streamName); |
144
|
|
|
|
145
|
|
|
$status->markasAsNotReceivedAllEvents(); |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
public function getLastProcessedEventId(): int |
150
|
|
|
{ |
151
|
|
|
return $this->getStatus()->last_processed_event_id ?? 0; |
|
|
|
|
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
public function lastEventProcessedAt(): Carbon |
155
|
|
|
{ |
156
|
|
|
return $this->getStatus()->updated_at; |
|
|
|
|
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
public function reset() |
160
|
|
|
{ |
161
|
|
|
if (! method_exists($this, 'resetState')) { |
162
|
|
|
throw CouldNotResetProjector::doesNotHaveResetStateMethod($this); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
$this->resetState(); |
|
|
|
|
166
|
|
|
|
167
|
|
|
$this->getAllStatuses()->each->delete(); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
public function shouldBeCalledImmediately(): bool |
171
|
|
|
{ |
172
|
|
|
return ! $this instanceof QueuedProjector; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
protected function getEventStreams(StoredEvent $storedEvent): Collection |
176
|
|
|
{ |
177
|
|
|
$streams = method_exists($this, 'streamEventsBy') |
178
|
|
|
? $this->streamEventsBy($storedEvent) |
|
|
|
|
179
|
|
|
: []; |
180
|
|
|
|
181
|
|
|
return collect(array_wrap($streams)) |
182
|
|
|
->mapWithKeys(function ($streamValue, $streamName) use ($storedEvent) { |
183
|
|
|
if (is_numeric($streamName)) { |
184
|
|
|
$streamName = $streamValue; |
185
|
|
|
|
186
|
|
|
$streamValue = array_get($storedEvent->event_properties, $streamName); |
|
|
|
|
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
return [$streamName => $streamValue]; |
190
|
|
|
}); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
protected function getEventStreamFullNames(StoredEvent $storedEvent): array |
194
|
|
|
{ |
195
|
|
|
$streamFullNames = $this->getEventStreams($storedEvent) |
196
|
|
|
->map(function ($streamValue, $streamName) { |
197
|
|
|
return "{$streamName}-{$streamValue}"; |
198
|
|
|
}) |
199
|
|
|
->toArray(); |
200
|
|
|
|
201
|
|
|
if (count($streamFullNames) === 0) { |
202
|
|
|
$streamFullNames = ['main']; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
return $streamFullNames; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
protected function getStatus(string $stream = 'main'): ProjectorStatus |
209
|
|
|
{ |
210
|
|
|
return $this->getProjectorStatusClass()::getForProjector($this, $stream); |
|
|
|
|
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
protected function getAllStatuses(): Collection |
214
|
|
|
{ |
215
|
|
|
return $this->getProjectorStatusClass()::getAllForProjector($this); |
|
|
|
|
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
protected function getStoredEventClass(): string |
219
|
|
|
{ |
220
|
|
|
return config('event-projector.stored_event_model'); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
protected function getProjectorStatusClass(): string |
224
|
|
|
{ |
225
|
|
|
return config('event-projector.projector_status_model'); |
226
|
|
|
} |
227
|
|
|
} |
228
|
|
|
|
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: