SnapshotRepository::findByName()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Spatie\DbSnapshots;
4
5
use Illuminate\Contracts\Filesystem\Filesystem as Disk;
6
use Illuminate\Support\Collection;
7
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