Completed
Pull Request — master (#661)
by Tim
01:45
created

BackupCollection::pathExtensionRepresentsBackup()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Spatie\Backup\BackupDestination;
4
5
use Exception;
6
use Illuminate\Support\Collection;
7
8
class BackupCollection extends Collection
9
{
10
    /** @var null|int */
11
    protected $sizeCache = null;
12
13
    /** @var array */
14
    protected static $allowedMimeTypes = [
15
        'application/zip', 'application/x-zip', 'application/x-gzip',
16
    ];
17
18
    /**
19
     * @param \Illuminate\Contracts\Filesystem\Filesystem|null $disk
20
     * @param array                                            $files
21
     *
22
     * @return \Spatie\Backup\BackupDestination\BackupCollection
23
     */
24
    public static function createFromFiles($disk, array $files): self
25
    {
26
        return (new static($files))
27
            ->filter(function ($path) use ($disk) {
28
                return static::pathRepresentsBackup($disk, $path);
29
            })
30
            ->map(function ($path) use ($disk) {
31
                return new Backup($disk, $path);
0 ignored issues
show
Bug introduced by
It seems like $disk defined by parameter $disk on line 24 can be null; however, Spatie\Backup\BackupDest...n\Backup::__construct() does not accept null, maybe add an additional type check?

It seems like you allow that null is being passed for a parameter, however the function which is called does not seem to accept null.

We recommend to add an additional type check (or disallow null for the parameter):

function notNullable(stdClass $x) { }

// Unsafe
function withoutCheck(stdClass $x = null) {
    notNullable($x);
}

// Safe - Alternative 1: Adding Additional Type-Check
function withCheck(stdClass $x = null) {
    if ($x instanceof stdClass) {
        notNullable($x);
    }
}

// Safe - Alternative 2: Changing Parameter
function withNonNullableParam(stdClass $x) {
    notNullable($x);
}
Loading history...
32
            })
33
            ->sortByDesc(function (Backup $backup) {
34
                return $backup->date()->timestamp;
35
            })
36
            ->values();
37
    }
38
39
    /**
40
     * @param \Illuminate\Contracts\Filesystem\Filesystem|null $disk
41
     * @param string $path
42
     *
43
     * @return bool
44
     */
45
    protected static function pathRepresentsBackup($disk, $path)
46
    {
47
        return static::pathMimeTypeRepresetsBackup($disk, $path)
48
            ?: static::pathExtensionRepresentsBackup($path);
49
    }
50
51
    /**
52
     * @param string $path
53
     *
54
     * @return bool
55
     */
56
    protected static function pathExtensionRepresentsBackup($path)
57
    {
58
        return pathinfo($path, PATHINFO_EXTENSION) === 'zip';
59
    }
60
61
    /**
62
     * @param \Illuminate\Contracts\Filesystem\Filesystem|null $disk
63
     * @param string $path
64
     *
65
     * @return bool
66
     */
67
    protected static function pathMimeTypeRepresetsBackup($disk, $path)
68
    {
69
        // Some drivers throw exceptions when checking mime types.
70
        try {
71
            return in_array(static::pathMimeType($disk, $path), self::$allowedMimeTypes);
72
        } catch (Exception $e) {
73
            return false;
74
        }
75
    }
76
77
    protected static function pathMimeType($disk, $path)
78
    {
79
        return ($disk && method_exists($disk, 'mimeType')) ? $disk->mimeType($path) : null;
80
    }
81
82
    /**
83
     * @return \Spatie\Backup\BackupDestination\Backup|null
84
     */
85
    public function newest()
86
    {
87
        return $this->first();
88
    }
89
90
    /**
91
     * @return \Spatie\Backup\BackupDestination\Backup|null
92
     */
93
    public function oldest()
94
    {
95
        return $this
96
            ->filter->exists()
97
            ->last();
98
    }
99
100
    public function size(): int
101
    {
102
        if ($this->sizeCache !== null) {
103
            return $this->sizeCache;
104
        }
105
106
        return $this->sizeCache = $this->sum->size();
107
    }
108
}
109