1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\EventProjector\Snapshots; |
4
|
|
|
|
5
|
|
|
use Carbon\Carbon; |
6
|
|
|
use Spatie\EventProjector\EventProjectionist; |
7
|
|
|
use Illuminate\Contracts\Filesystem\Filesystem; |
8
|
|
|
use Spatie\EventProjector\Exceptions\InvalidSnapshot; |
9
|
|
|
use Spatie\EventProjector\Models\StoredEvent; |
10
|
|
|
|
11
|
|
|
class Snapshot |
12
|
|
|
{ |
13
|
|
|
/** @var \Spatie\EventProjector\EventProjectionist */ |
14
|
|
|
protected $eventProjectionist; |
15
|
|
|
|
16
|
|
|
/** @var \Illuminate\Contracts\Filesystem\Filesystem */ |
17
|
|
|
protected $disk; |
18
|
|
|
|
19
|
|
|
/** @var string */ |
20
|
|
|
protected $fileName; |
21
|
|
|
|
22
|
|
|
public function __construct( |
23
|
|
|
EventProjectionist $eventProjectionist, |
24
|
|
|
array $config, |
|
|
|
|
25
|
|
|
Filesystem $disk, |
26
|
|
|
string $fileName) |
27
|
|
|
{ |
28
|
|
|
$this->eventProjectionist = $eventProjectionist; |
29
|
|
|
|
30
|
|
|
$this->disk = $disk; |
31
|
|
|
|
32
|
|
|
$this->fileName = $fileName; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function isValid(): bool |
36
|
|
|
{ |
37
|
|
|
return $this->projectorName() !== ''; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function fileName(): string |
41
|
|
|
{ |
42
|
|
|
return $this->fileName; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function lastProcessedEventId(): int |
46
|
|
|
{ |
47
|
|
|
return $this->fileNameParts()['lastProcessedEventId']; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function lastProcessedEvent(): StoredEvent |
51
|
|
|
{ |
52
|
|
|
$storedEventModelClass = $this->config['stored_event_model']; |
|
|
|
|
53
|
|
|
|
54
|
|
|
$storedEvent = $storedEventModelClass::query() |
55
|
|
|
->where('last_processed_event_id', $this->lastProcessedEventId()) |
56
|
|
|
->first(); |
57
|
|
|
|
58
|
|
|
if (! $storedEvent) { |
59
|
|
|
throw InvalidSnapshot::lastProcessedEventDoesNotExist($this); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
return $storedEvent; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function projectorName(): string |
66
|
|
|
{ |
67
|
|
|
return $this->fileNameParts()['projectorName']; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function projector(): Snapshottable |
71
|
|
|
{ |
72
|
|
|
$projectorName = $this->projectorName(); |
73
|
|
|
|
74
|
|
|
if (! $projector = $this->eventProjectionist->getProjector($projectorName)) { |
75
|
|
|
throw InvalidSnapshot::projectorDoesNotExist($this); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
return $projector; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function name(): string |
82
|
|
|
{ |
83
|
|
|
return $this->fileNameParts()['name']; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param string|resource $contents |
88
|
|
|
*/ |
89
|
|
|
public function write($contents) |
90
|
|
|
{ |
91
|
|
|
$this->disk->put($this->fileName, $contents); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function read(): ?string |
95
|
|
|
{ |
96
|
|
|
return $this->disk->get($this->fileName); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
public function readStream() |
100
|
|
|
{ |
101
|
|
|
return $this->disk->readStream($this->fileName); |
|
|
|
|
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function restore(): self |
105
|
|
|
{ |
106
|
|
|
$storedEvent = $this->lastProcessedEvent(); |
107
|
|
|
|
108
|
|
|
$this->projector()->restoreSnapshot($this); |
109
|
|
|
|
110
|
|
|
$this->config['projector_status_model']::rememberLastProcessedEvent($storedEvent); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
public function delete() |
114
|
|
|
{ |
115
|
|
|
$this->disk->delete($this->fileName); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
public function createdAt(): Carbon |
119
|
|
|
{ |
120
|
|
|
$timestamp = $this->disk->lastModified($this->fileName); |
121
|
|
|
|
122
|
|
|
return Carbon::createFromTimestamp($timestamp); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
protected function fileNameParts(): array |
126
|
|
|
{ |
127
|
|
|
$baseName = pathinfo($this->fileName, PATHINFO_FILENAME); |
128
|
|
|
|
129
|
|
|
$nameParts = explode('---', $baseName); |
130
|
|
|
|
131
|
|
|
$projectorName = str_replace('+', '\\', $nameParts[1] ?? ''); |
132
|
|
|
|
133
|
|
|
$lastProcessedEventId = $nameParts[2] ?? 0; |
134
|
|
|
|
135
|
|
|
$name = $nameParts[3] ?? ''; |
136
|
|
|
|
137
|
|
|
return compact('projectorName', 'lastProcessedEventId', 'name'); |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.