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 streamNamesToTrack(): array |
26
|
|
|
{ |
27
|
|
|
return array_wrap($this->trackStream ?? []); |
|
|
|
|
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public function trackEventsByStreamNameAndId(): bool |
31
|
|
|
{ |
32
|
|
|
return count($this->streamNamesToTrack()) === 0; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function handlesStreamOfStoredEvent(StoredEvent $storedEvent): bool |
36
|
|
|
{ |
37
|
|
|
$trackedStreamNames = $this->streamNamesToTrack(); |
38
|
|
|
|
39
|
|
|
/* |
|
|
|
|
40
|
|
|
if ($trackedStreamNames === []) { |
41
|
|
|
return true; |
42
|
|
|
} |
43
|
|
|
*/ |
44
|
|
|
|
45
|
|
|
$event = $storedEvent->event; |
46
|
|
|
|
47
|
|
|
$streamNameOfEvent = method_exists($event, 'getStreamName') |
48
|
|
|
? $event->getStreamName() |
49
|
|
|
: 'main'; |
50
|
|
|
|
51
|
|
|
if (in_array('*', $trackedStreamNames)) { |
52
|
|
|
return true; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
return in_array($streamNameOfEvent, $trackedStreamNames); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function rememberReceivedEvent(StoredEvent $storedEvent) |
59
|
|
|
{ |
60
|
|
|
$this->getStatus($storedEvent)->rememberLastProcessedEvent($storedEvent, $this); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function hasReceivedAllPriorEvents(StoredEvent $storedEvent): bool |
64
|
|
|
{ |
65
|
|
|
if (! $this->trackEventsByStreamNameAndId()) { |
66
|
|
|
return $storedEvent->id === $this->getStatus()->last_processed_event_id + 1; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
$previousEvent = $storedEvent->previousInStream(); |
70
|
|
|
$previousEventId = optional($previousEvent)->id ?? 0; |
71
|
|
|
|
72
|
|
|
$lastProcessedEventId = (int) $this->getStatus($storedEvent)->last_processed_event_id ?? 0; |
73
|
|
|
|
74
|
|
|
return $previousEventId === $lastProcessedEventId; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function hasReceivedAllEvents(): bool |
78
|
|
|
{ |
79
|
|
|
return ProjectorStatus::hasReceivedAllEvents($this); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function getLastProcessedEventId(): int |
83
|
|
|
{ |
84
|
|
|
return $this->getStatus()->last_processed_event_id ?? 0; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public function lastEventProcessedAt(): Carbon |
88
|
|
|
{ |
89
|
|
|
return $this->getStatus()->updated_at; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function reset() |
93
|
|
|
{ |
94
|
|
|
if (! method_exists($this, 'resetState')) { |
95
|
|
|
throw CouldNotResetProjector::doesNotHaveResetStateMethod($this); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
$this->resetState(); |
|
|
|
|
99
|
|
|
|
100
|
|
|
$this->getAllStatuses()->each->delete(); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function shouldBeCalledImmediately(): bool |
104
|
|
|
{ |
105
|
|
|
return ! $this instanceof QueuedProjector; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
protected function getStatus(StoredEvent $storedEvent = null): ProjectorStatus |
109
|
|
|
{ |
110
|
|
|
return ProjectorStatus::getForProjector($this, $storedEvent); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
protected function getAllStatuses(): Collection |
114
|
|
|
{ |
115
|
|
|
return ProjectorStatus::getAllForProjector($this); |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
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: