1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\EventProjector\Models; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Collection; |
6
|
|
|
use Illuminate\Database\Eloquent\Model; |
7
|
|
|
use Spatie\EventProjector\Projectors\Projector; |
8
|
|
|
use Spatie\EventProjector\Facades\EventProjectionist; |
9
|
|
|
|
10
|
|
|
class ProjectorStatus extends Model |
11
|
|
|
{ |
12
|
|
|
public $guarded = []; |
13
|
|
|
|
14
|
|
|
public $casts = [ |
15
|
|
|
'has_received_all_events' => 'boolean', |
16
|
|
|
]; |
17
|
|
|
|
18
|
|
|
public static function getForProjector(Projector $projector, string $stream = 'main'): ProjectorStatus |
19
|
|
|
{ |
20
|
|
|
return self::firstOrCreate([ |
21
|
|
|
'projector_name' => $projector->getName(), |
22
|
|
|
'stream' => $stream, |
23
|
|
|
]); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
public static function getAllForProjector(Projector $projector): Collection |
27
|
|
|
{ |
28
|
|
|
return static::where('projector_name', $projector->getName())->get(); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function rememberLastProcessedEvent(StoredEvent $storedEvent): ProjectorStatus |
32
|
|
|
{ |
33
|
|
|
$this->last_processed_event_id = $storedEvent->id; |
34
|
|
|
$this->save(); |
35
|
|
|
|
36
|
|
|
return $this; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public static function hasReceivedAllEvents(Projector $projector): bool |
40
|
|
|
{ |
41
|
|
|
$highestEventId = (int) self::query() |
42
|
|
|
->where('projector_name', $projector->getName()) |
43
|
|
|
->max('last_processed_event_id') ?? 0; |
44
|
|
|
|
45
|
|
|
return $highestEventId === StoredEvent::getMaxId(); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function getProjector(): Projector |
49
|
|
|
{ |
50
|
|
|
return EventProjectionist::getProjector($this->projector_name); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function markAsReceivedAllEvents(): self |
54
|
|
|
{ |
55
|
|
|
$this->has_received_all_events = true; |
56
|
|
|
|
57
|
|
|
$this->save(); |
58
|
|
|
|
59
|
|
|
return $this; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function markasAsNotReceivedAllEvents(): self |
63
|
|
|
{ |
64
|
|
|
$this->has_received_all_events = false; |
65
|
|
|
|
66
|
|
|
$this->save(); |
67
|
|
|
|
68
|
|
|
return $this; |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|