BackupCollection::newest()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Spatie\Backup\BackupDestination;
4
5
use Illuminate\Contracts\Filesystem\Filesystem;
6
use Illuminate\Support\Collection;
7
use Spatie\Backup\Helpers\File;
8
9
class BackupCollection extends Collection
10
{
11
    /** @var null|float */
12
    protected $sizeCache = null;
13
14
    public static function createFromFiles(?FileSystem $disk, array $files): self
15
    {
16
        return (new static($files))
17
            ->filter(function ($path) use ($disk) {
18
                return (new File)->isZipFile($disk, $path);
19
            })
20
            ->map(function ($path) use ($disk) {
21
                return new Backup($disk, $path);
22
            })
23
            ->sortByDesc(function (Backup $backup) {
24
                return $backup->date()->timestamp;
25
            })
26
            ->values();
27
    }
28
29
    public function newest(): ?Backup
30
    {
31
        return $this->first();
32
    }
33
34
    public function oldest(): ?Backup
35
    {
36
        return $this
37
            ->filter->exists()
38
            ->last();
39
    }
40
41
    public function size(): float
42
    {
43
        if ($this->sizeCache !== null) {
44
            return $this->sizeCache;
45
        }
46
47
        return $this->sizeCache = $this->sum->size();
48
    }
49
}
50