|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Spatie\EventProjector\Snapshots; |
|
4
|
|
|
|
|
5
|
|
|
use Exception; |
|
6
|
|
|
use Illuminate\Contracts\Filesystem\Filesystem; |
|
7
|
|
|
use Spatie\EventProjector\EventProjectionist; |
|
8
|
|
|
use Spatie\EventProjector\Exceptions\CouldNotCreateSnapshot; |
|
9
|
|
|
|
|
10
|
|
|
class SnapshotFactory |
|
11
|
|
|
{ |
|
12
|
|
|
/** @var \Spatie\EventProjector\EventProjectionist */ |
|
13
|
|
|
protected $eventProjectionist; |
|
14
|
|
|
|
|
15
|
|
|
/** @var \Spatie\EventProjector\Snapshots\Filesystem */ |
|
16
|
|
|
protected $disk; |
|
17
|
|
|
|
|
18
|
|
|
public function __construct(EventProjectionist $eventProjectionist, Filesystem $disk) |
|
19
|
|
|
{ |
|
20
|
|
|
$this->eventProjectionist = $eventProjectionist; |
|
21
|
|
|
|
|
22
|
|
|
$this->disk = $disk; |
|
|
|
|
|
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
public function createForProjector(Snapshottable $projector, string $name = ''): Snapshot |
|
26
|
|
|
{ |
|
27
|
|
|
$lastEventId = $projector->getLastProcessedEventId(); |
|
28
|
|
|
|
|
29
|
|
|
$fileName = "Snapshot---{$projector->getName()}---{$lastEventId}---{$name}.txt"; |
|
30
|
|
|
|
|
31
|
|
|
$snapshot = new Snapshot( |
|
32
|
|
|
$this->eventProjectionist, |
|
33
|
|
|
$this->disk, |
|
34
|
|
|
$fileName |
|
35
|
|
|
); |
|
36
|
|
|
|
|
37
|
|
|
try { |
|
38
|
|
|
$projector->writeToSnapshot($snapshot); |
|
39
|
|
|
} catch (Exception $exception) { |
|
40
|
|
|
try { |
|
41
|
|
|
$snapshot->delete(); |
|
42
|
|
|
} catch (Exception $exception) { |
|
|
|
|
|
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
throw CouldNotCreateSnapshot::projectorThrewExceptionDuringWritingToSnapshot($projector, $exception); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
if (!$this->disk->exists($fileName)) { |
|
49
|
|
|
throw CouldNotCreateSnapshot::projectorDidNotWriteAnythingToSnapshot($projector); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
if (!static::snapshotIsValid($lastEventId)) { |
|
53
|
|
|
$snapshot->delete(); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
return $snapshot; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
public function createForFile(Filesystem $disk, string $fileName): Snapshot |
|
60
|
|
|
{ |
|
61
|
|
|
return new Snapshot($this->eventProjectionist, $disk, $fileName); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
protected static function snapshotIsValid(int $lastEventId): bool |
|
|
|
|
|
|
65
|
|
|
{ |
|
66
|
|
|
return true; |
|
67
|
|
|
} |
|
68
|
|
|
} |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..