Completed
Push — master ( b447af...e5edfd )
by Freek
01:50
created

File   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 42
rs 10
c 0
b 0
f 0
wmc 9
lcom 1
cbo 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A isZipFile() 0 8 2
A hasZipExtension() 0 4 1
A hasAllowedMimeType() 0 4 1
B mimeType() 0 13 5
1
<?php
2
3
namespace Spatie\Backup\Helpers;
4
5
use Exception;
6
use Illuminate\Contracts\Filesystem\Filesystem;
7
8
class File
9
{
10
    /** @var array */
11
    protected static $allowedMimeTypes = [
12
        'application/zip',
13
        'application/x-zip',
14
        'application/x-gzip',
15
    ];
16
17
    public function isZipFile(?Filesystem $disk, string $path) : bool
18
    {
19
        if ($this->hasZipExtension($path)) {
20
            return true;
21
        }
22
23
        return $this->hasAllowedMimeType($disk, $path);
24
    }
25
26
    protected function hasZipExtension(string $path): bool
27
    {
28
        return pathinfo($path, PATHINFO_EXTENSION) === 'zip';
29
    }
30
31
    protected function hasAllowedMimeType(?Filesystem $disk, string $path)
32
    {
33
        return in_array($this->mimeType($disk, $path), self::$allowedMimeTypes);
34
    }
35
36
    protected function mimeType(?Filesystem $disk, string $path)
37
    {
38
        try {
39
            if ($disk && method_exists($disk, 'mimeType')) {
40
                return $disk->mimeType($path) ?: false;
41
            }
42
        } catch (Exception $exception) {
43
            // Some drivers throw exceptions when checking mime types, we'll
44
            // just fallback to `false`.
45
        }
46
47
        return false;
48
    }
49
}
50