Completed
Push — master ( f62f21...f72c97 )
by Freek
05:09
created

BackupCollection   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 4
Bugs 0 Features 1
Metric Value
wmc 4
c 4
b 0
f 1
lcom 1
cbo 3
dl 0
loc 54
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A createFromFiles() 0 14 1
A newest() 0 4 1
A oldest() 0 8 1
A size() 0 7 1
1
<?php
2
3
namespace Spatie\Backup\BackupDestination;
4
5
use Illuminate\Contracts\Filesystem\Filesystem;
6
use Illuminate\Support\Collection;
7
8
class BackupCollection extends Collection
9
{
10
    /**
11
     * @param \Illuminate\Contracts\Filesystem\Filesystem $disk
12
     * @param array                                       $files
13
     *
14
     * @return \Spatie\Backup\BackupDestination\BackupCollection
15
     */
16
    public static function createFromFiles(Filesystem $disk, array $files)
17
    {
18
        return (new static($files))
19
            ->filter(function ($path) {
20
                return pathinfo($path, PATHINFO_EXTENSION) === 'zip';
21
            })
22
            ->map(function ($path) use ($disk) {
23
                return new Backup($disk, $path);
24
            })
25
            ->sortByDesc(function (Backup $backup) {
26
                return $backup->date()->timestamp;
27
            })
28
            ->values();
29
    }
30
31
    /**
32
     * @return \Spatie\Backup\BackupDestination\Backup|null
33
     */
34
    public function newest()
35
    {
36
        return $this->first();
37
    }
38
39
    /**
40
     * @return \Spatie\Backup\BackupDestination\Backup|null
41
     */
42
    public function oldest()
43
    {
44
        return $this
45
            ->filter(function (Backup $backup) {
46
                return $backup->exists();
47
            })
48
            ->last();
49
    }
50
51
    /**
52
     * @return int
53
     */
54
    public function size()
55
    {
56
        return $this
57
            ->reduce(function ($totalSize, Backup $backup) {
58
                return $totalSize + $backup->size();
59
            }, 0);
60
    }
61
}
62