1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\EventProjector\Snapshots; |
4
|
|
|
|
5
|
|
|
use Illuminate\Contracts\Filesystem\Filesystem; |
6
|
|
|
use Spatie\EventProjector\EventProjectionist; |
7
|
|
|
use Spatie\EventProjector\Projectors\Projector; |
8
|
|
|
|
9
|
|
|
class Snapshot |
10
|
|
|
{ |
11
|
|
|
/** @var \Spatie\EventProjector\EventProjectionist */ |
12
|
|
|
protected $eventProjectionist; |
13
|
|
|
|
14
|
|
|
/** @var \Illuminate\Contracts\Filesystem\Filesystem */ |
15
|
|
|
protected $disk; |
16
|
|
|
|
17
|
|
|
/** @var string */ |
18
|
|
|
protected $fileName; |
19
|
|
|
|
20
|
|
|
public static function createForProjector(Projector $projector, string $name = ''): self |
21
|
|
|
{ |
22
|
|
|
$lastEventId = $projector->getLastProcessedEventId(); |
23
|
|
|
|
24
|
|
|
$name = "{$projector->getName()}---{$lastEventId}---{$name}.txt"; |
25
|
|
|
|
26
|
|
|
return app(static::class)->setFileName($name); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public static function createForFile(Filesystem $disk, string $fileName): Snapshot |
30
|
|
|
{ |
31
|
|
|
return (new static($disk,$fileName)); |
|
|
|
|
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function __construct(EventProjectionist $eventProjectionist, Filesystem $disk, string $fileName) |
35
|
|
|
{ |
36
|
|
|
$this->eventProjectionist = $eventProjectionist; |
37
|
|
|
|
38
|
|
|
$this->disk = $disk; |
39
|
|
|
|
40
|
|
|
$this->fileName = $fileName; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function getLastProcessedEventId(): int |
44
|
|
|
{ |
45
|
|
|
return $this->getNameParts()['lastProcessedEventId']; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function getProjectorName(): string |
49
|
|
|
{ |
50
|
|
|
return $this->getNameParts()['projectorName']; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function getProjector(): Projector |
54
|
|
|
{ |
55
|
|
|
$projectorName = $this->getProjectorName(); |
56
|
|
|
|
57
|
|
|
return $this->eventProjectionist->getProjector($projectorName); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function getName(): string |
61
|
|
|
{ |
62
|
|
|
return $this->getNameParts()['name']; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param string|resource $contents |
67
|
|
|
*/ |
68
|
|
|
public function write($contents) |
69
|
|
|
{ |
70
|
|
|
$this->disk->put($this->fileName, $contents); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function read(): ?string |
74
|
|
|
{ |
75
|
|
|
return $this->disk->get($this->fileName); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function readStream() |
79
|
|
|
{ |
80
|
|
|
return $this->disk->readStream($this->fileName); |
|
|
|
|
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
protected function getNameParts(): array |
84
|
|
|
{ |
85
|
|
|
$baseName = pathinfo($this->fileName, PATHINFO_FILENAME); |
86
|
|
|
|
87
|
|
|
$nameParts = explode('---', $baseName); |
88
|
|
|
|
89
|
|
|
$projectorName = $nameParts[0]; |
90
|
|
|
|
91
|
|
|
$lastProcessedEventId = (int) $nameParts[1]; |
92
|
|
|
|
93
|
|
|
$name = $nameParts[2] ?? ''; |
94
|
|
|
|
95
|
|
|
return compact('projectorName', 'lastProcessedEventId', 'name'); |
96
|
|
|
} |
97
|
|
|
} |
This check looks for function calls that miss required arguments.