Completed
Push — master ( b33654...45ef8a )
by Freek
11s
created

BackupPath::hasZipExtension()   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
7
class BackupPath
8
{
9
    /** @var array */
10
    protected static $allowedMimeTypes = [
11
        'application/zip',
12
        'application/x-zip',
13
        'application/x-gzip',
14
    ];
15
16
    /**
17
     * @param \Illuminate\Contracts\Filesystem\Filesystem|null $disk
18
     * @param string $path
19
     *
20
     * @return bool
21
     */
22
    public function isBackupFile($disk, string $path) : bool
23
    {
24
        return $this->hasZipExtension($path) ?: $this->hasAllowedMimeType($disk, $path);
25
    }
26
27
    /**
28
     * @param string $path
29
     *
30
     * @return bool
31
     */
32
    protected function hasZipExtension($path)
33
    {
34
        return pathinfo($path, PATHINFO_EXTENSION) === 'zip';
35
    }
36
37
    /**
38
     * @param \Illuminate\Contracts\Filesystem\Filesystem|null $disk
39
     * @param string $path
40
     *
41
     * @return bool
42
     */
43
    protected function hasAllowedMimeType($disk, $path)
44
    {
45
        return in_array($this->mimeType($disk, $path), self::$allowedMimeTypes);
46
    }
47
48
    /**
49
     * @param \Illuminate\Contracts\Filesystem\Filesystem|null $disk
50
     * @param string $path
51
     *
52
     * @return string|false
53
     */
54
    protected function mimeType($disk, $path)
55
    {
56
        try {
57
            if ($disk && method_exists($disk, 'mimeType')) {
58
                return $disk->mimeType($path) ?: false;
59
            }
60
        } catch (Exception $exception) {
61
            // Some drivers throw exceptions when checking mime types, we'll
62
            // just fallback to `false`.
63
        }
64
65
        return false;
66
    }
67
}
68