1 | <?php |
||
8 | class SnapshotRepository |
||
9 | { |
||
10 | protected Disk $disk; |
||
|
|||
11 | |||
12 | public function __construct(Disk $disk) |
||
13 | { |
||
14 | $this->disk = $disk; |
||
15 | } |
||
16 | |||
17 | public function getAll(): Collection |
||
18 | { |
||
19 | return collect($this->disk->allFiles()) |
||
20 | ->filter(function (string $fileName) { |
||
21 | $pathinfo = pathinfo($fileName); |
||
22 | |||
23 | if ($pathinfo['extension'] === 'gz') { |
||
24 | $fileName = $pathinfo['filename']; |
||
25 | } |
||
26 | |||
27 | return pathinfo($fileName, PATHINFO_EXTENSION) === 'sql'; |
||
28 | }) |
||
29 | ->map(function (string $fileName) { |
||
30 | return new Snapshot($this->disk, $fileName); |
||
31 | }) |
||
32 | ->sortByDesc(function (Snapshot $snapshot) { |
||
33 | return $snapshot->createdAt()->toDateTimeString(); |
||
34 | }); |
||
35 | } |
||
36 | |||
37 | public function findByName(string $name) |
||
38 | { |
||
39 | return $this->getAll()->first(function (Snapshot $snapshot) use ($name) { |
||
40 | return $snapshot->name === $name; |
||
41 | }); |
||
42 | } |
||
43 | } |
||
44 |