|
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 rememberReceivedEvent(StoredEvent $storedEvent) |
|
36
|
|
|
{ |
|
37
|
|
|
$this->getStatus($storedEvent)->rememberLastProcessedEvent($storedEvent, $this); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
public function hasReceivedAllPriorEvents(StoredEvent $storedEvent): bool |
|
41
|
|
|
{ |
|
42
|
|
|
if (! $this->trackEventsByStreamNameAndId()) { |
|
43
|
|
|
return $storedEvent->id === $this->getStatus()->last_processed_event_id + 1; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
$previousEvent = $storedEvent->previousInStream(); |
|
47
|
|
|
$previousEventId = optional($previousEvent)->id ?? 0; |
|
48
|
|
|
|
|
49
|
|
|
$lastProcessedEventId = (int) $this->getStatus($storedEvent)->last_processed_event_id ?? 0; |
|
50
|
|
|
|
|
51
|
|
|
return $previousEventId === $lastProcessedEventId; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
public function hasReceivedAllEvents(): bool |
|
55
|
|
|
{ |
|
56
|
|
|
return ProjectorStatus::hasReceivedAllEvents($this); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
public function getLastProcessedEventId(): int |
|
60
|
|
|
{ |
|
61
|
|
|
return $this->getStatus()->last_processed_event_id ?? 0; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
public function lastEventProcessedAt(): Carbon |
|
65
|
|
|
{ |
|
66
|
|
|
return $this->getStatus()->updated_at; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
public function reset() |
|
70
|
|
|
{ |
|
71
|
|
|
if (! method_exists($this, 'resetState')) { |
|
72
|
|
|
throw CouldNotResetProjector::doesNotHaveResetStateMethod($this); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
$this->resetState(); |
|
|
|
|
|
|
76
|
|
|
|
|
77
|
|
|
$this->getAllStatuses()->each->delete(); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
public function shouldBeCalledImmediately(): bool |
|
81
|
|
|
{ |
|
82
|
|
|
return ! $this instanceof QueuedProjector; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
protected function getStatus(StoredEvent $storedEvent = null): ProjectorStatus |
|
86
|
|
|
{ |
|
87
|
|
|
return ProjectorStatus::getForProjector($this, $storedEvent); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
protected function getAllStatuses(): Collection |
|
91
|
|
|
{ |
|
92
|
|
|
return ProjectorStatus::getAllForProjector($this); |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|
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: