GuessLaruploadFileTypeAction   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 34
c 2
b 0
f 0
dl 0
loc 78
rs 10
wmc 13

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 1 1
A make() 0 3 1
A isImage() 0 3 1
A mimeToType() 0 19 6
A isDocument() 0 19 1
A isCompressed() 0 10 1
A calc() 0 9 2
1
<?php
2
3
namespace Mostafaznv\Larupload\Actions;
4
5
use Illuminate\Http\UploadedFile;
6
use Mostafaznv\Larupload\Enums\LaruploadFileType;
7
8
class GuessLaruploadFileTypeAction
9
{
10
    public function __construct(private readonly UploadedFile $file) {}
11
12
    public static function make(UploadedFile $file): self
13
    {
14
        return new self($file);
15
    }
16
17
18
    public function calc(): ?LaruploadFileType
19
    {
20
        if ($this->file->isValid()) {
21
            $mime = $this->file->getMimeType();
22
23
            return $this->mimeToType($mime);
24
        }
25
26
        return null;
27
    }
28
29
    public function isImage(): bool
30
    {
31
        return $this->mimeToType($this->file->getMimeType()) === LaruploadFileType::IMAGE;
32
    }
33
34
    private function mimeToType(string $mime = null): LaruploadFileType
35
    {
36
        if (str_contains($mime, 'image/')) {
0 ignored issues
show
Bug introduced by
It seems like $mime can also be of type null; however, parameter $haystack of str_contains() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

36
        if (str_contains(/** @scrutinizer ignore-type */ $mime, 'image/')) {
Loading history...
37
            return LaruploadFileType::IMAGE;
38
        }
39
        else if (str_contains($mime, 'video/')) {
40
            return LaruploadFileType::VIDEO;
41
        }
42
        else if (str_contains($mime, 'audio/')) {
43
            return LaruploadFileType::AUDIO;
44
        }
45
        else if ($this->isDocument($mime)) {
0 ignored issues
show
Bug introduced by
It seems like $mime can also be of type null; however, parameter $mime of Mostafaznv\Larupload\Act...ypeAction::isDocument() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

45
        else if ($this->isDocument(/** @scrutinizer ignore-type */ $mime)) {
Loading history...
46
            return LaruploadFileType::DOCUMENT;
47
        }
48
        else if ($this->isCompressed($mime)) {
0 ignored issues
show
Bug introduced by
It seems like $mime can also be of type null; however, parameter $mime of Mostafaznv\Larupload\Act...eAction::isCompressed() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

48
        else if ($this->isCompressed(/** @scrutinizer ignore-type */ $mime)) {
Loading history...
49
            return LaruploadFileType::COMPRESSED;
50
        }
51
52
        return LaruploadFileType::FILE;
53
    }
54
55
    private function isDocument(string $mime): bool {
56
        $mimeTypes = [
57
            // pdf, epub, csv, tsv, txt
58
            'application/pdf', 'application/epub+zip', 'text/csv', 'text/tab-separated-values', 'text/plain',
59
            // doc, docx
60
            'application/msword', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
61
            // ppt, pptx
62
            'application/vnd.ms-powerpoint', 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
63
            // xls, xlsx
64
            'application/vnd.ms-excel', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
65
            // odt, ods, odp
66
            'application/vnd.oasis.opendocument.spreadsheet', 'application/vnd.oasis.opendocument.presentation', 'application/vnd.oasis.opendocument.text',
67
            // rtf
68
            'application/rtf',
69
            // html, xml
70
            'application/xhtml+xml', 'text/html', 'text/xml',
71
        ];
72
73
        return in_array($mime, $mimeTypes);
74
    }
75
76
    private function isCompressed(string $mime): bool {
77
        $mimeTypes = [
78
            'application/zip', 'application/x-compressed', 'application/x-zip-compressed', 'multipart/x-zip',
79
            'application/x-rar-compressed', 'application/x-7z-compressed', 'application/x-tar',
80
            'application/gzip', 'application/x-bzip2', 'application/x-xz', 'application/x-lzip', 'application/x-lz4',
81
            'application/zstd', 'application/x-compress', 'application/gzip', 'application/x-bzip-compressed-tar',
82
            'application/x-bzip2-compressed-tar'
83
        ];
84
85
        return in_array($mime, $mimeTypes);
86
    }
87
}
88