SnapshotRepository   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Importance

Changes 4
Bugs 1 Features 0
Metric Value
wmc 4
eloc 15
c 4
b 1
f 0
dl 0
loc 33
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A getAll() 0 17 2
A findByName() 0 4 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