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
|
|
|
$streamFullNames = collect($this->streamEventsBy($storedEvent)) |
28
|
|
|
->map(function ($streamValue, $streamName) { |
29
|
|
|
return "{$streamName}-{$streamValue}"; |
30
|
|
|
}) |
31
|
|
|
->toArray(); |
32
|
|
|
|
33
|
|
|
if (count($streamFullNames) === 0) { |
34
|
|
|
$streamFullNames = ['main']; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
foreach ($streamFullNames as $streamName) { |
38
|
|
|
$this->getStatus($streamName)->rememberLastProcessedEvent($storedEvent, $this); |
39
|
|
|
} |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function hasReceivedAllPriorEvents(StoredEvent $storedEvent): bool |
43
|
|
|
{ |
44
|
|
|
$streams = collect($this->streamEventsBy($storedEvent)); |
45
|
|
|
|
46
|
|
|
if ($streams->isEmpty()) { |
47
|
|
|
$lastStoredEvent = StoredEvent::query() |
48
|
|
|
->whereIn('event_class', $this->handlesEventClassNames()) |
49
|
|
|
->where('id', '<', $storedEvent->id) |
50
|
|
|
->orderBy('id', 'desc') |
51
|
|
|
->first(); |
52
|
|
|
|
53
|
|
|
$lastStoredEventId = (int) optional($lastStoredEvent)->id ?? 0; |
54
|
|
|
|
55
|
|
|
$lastProcessedEventId = (int) $this->getStatus()->last_processed_event_id ?? 0; |
56
|
|
|
|
57
|
|
|
return $lastStoredEventId === $lastProcessedEventId; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
foreach ($streams as $streamName => $streamValue) { |
61
|
|
|
$streamFullName = "{$streamName}-{$streamValue}"; |
62
|
|
|
$whereJsonClause = str_replace('.', '->', $streamName); |
63
|
|
|
|
64
|
|
|
$lastStoredEvent = StoredEvent::query() |
65
|
|
|
->whereIn('event_class', $this->handlesEventClassNames()) |
66
|
|
|
->where('id', '<', $storedEvent->id) |
67
|
|
|
->where("event_properties->{$whereJsonClause}", $streamValue) |
68
|
|
|
->orderBy('id', 'desc') |
69
|
|
|
->first(); |
70
|
|
|
|
71
|
|
|
$lastStoredEventId = (int) optional($lastStoredEvent)->id ?? 0; |
72
|
|
|
|
73
|
|
|
$lastProcessedEventId = (int) $this->getStatus($streamFullName)->last_processed_event_id ?? 0; |
74
|
|
|
|
75
|
|
|
if ($lastStoredEventId !== $lastProcessedEventId) { |
76
|
|
|
return false; |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return true; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function hasReceivedAllEvents(): bool |
84
|
|
|
{ |
85
|
|
|
return ProjectorStatus::hasReceivedAllEvents($this); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function getLastProcessedEventId(): int |
89
|
|
|
{ |
90
|
|
|
return $this->getStatus()->last_processed_event_id ?? 0; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function lastEventProcessedAt(): Carbon |
94
|
|
|
{ |
95
|
|
|
return $this->getStatus()->updated_at; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function reset() |
99
|
|
|
{ |
100
|
|
|
if (! method_exists($this, 'resetState')) { |
101
|
|
|
throw CouldNotResetProjector::doesNotHaveResetStateMethod($this); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
$this->resetState(); |
|
|
|
|
105
|
|
|
|
106
|
|
|
$this->getAllStatuses()->each->delete(); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public function shouldBeCalledImmediately(): bool |
110
|
|
|
{ |
111
|
|
|
return ! $this instanceof QueuedProjector; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
public function streamEventsBy(StoredEvent $storedEvent): array |
|
|
|
|
115
|
|
|
{ |
116
|
|
|
return []; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
protected function getStatus(string $stream = 'main'): ProjectorStatus |
120
|
|
|
{ |
121
|
|
|
return ProjectorStatus::getForProjector($this, $stream); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
protected function getAllStatuses(): Collection |
125
|
|
|
{ |
126
|
|
|
return ProjectorStatus::getAllForProjector($this); |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|
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: