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