Test Failed
Pull Request — master (#1200)
by
unknown
07:15
created

LfmUploadValidator::hasAllowedExtension()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 9
rs 10
1
<?php
2
3
namespace UniSharp\LaravelFilemanager;
4
5
use Symfony\Component\HttpFoundation\File\UploadedFile;
6
use UniSharp\LaravelFilemanager\Exceptions\DuplicateFileNameException;
7
use UniSharp\LaravelFilemanager\Exceptions\EmptyFileException;
8
use UniSharp\LaravelFilemanager\Exceptions\ExcutableFileException;
9
use UniSharp\LaravelFilemanager\Exceptions\FileFailedToUploadException;
10
use UniSharp\LaravelFilemanager\Exceptions\FileSizeExceedConfigurationMaximumException;
11
use UniSharp\LaravelFilemanager\Exceptions\FileSizeExceedIniMaximumException;
12
use UniSharp\LaravelFilemanager\Exceptions\InvalidExtensionException;
13
use UniSharp\LaravelFilemanager\Exceptions\InvalidMimeTypeException;
14
use UniSharp\LaravelFilemanager\LfmPath;
15
16
class LfmUploadValidator
17
{
18
    private $file;
19
20
    public function __construct(UploadedFile $file)
21
    {
22
        // if (! $file instanceof UploadedFile) {
23
        //     throw new \Exception(trans(self::PACKAGE_NAME . '::lfm.error-instance'));
24
        // }
25
26
        $this->file = $file;
27
    }
28
29
    // public function hasContent()
30
    // {
31
    //     if (empty($this->file)) {
32
    //         throw new EmptyFileException();
33
    //     }
34
35
    //     return $this;
36
    // }
37
38
    public function sizeLowerThanIniMaximum()
39
    {
40
        if ($this->file->getError() == UPLOAD_ERR_INI_SIZE) {
41
            throw new FileSizeExceedIniMaximumException();
42
        }
43
44
        return $this;
45
    }
46
47
    public function uploadWasSuccessful()
48
    {
49
        if ($this->file->getError() != UPLOAD_ERR_OK) {
50
            throw new FileFailedToUploadException($this->file->getError());
51
        }
52
53
        return $this;
54
    }
55
56
    public function nameIsNotDuplicate($new_file_name, LfmPath $lfm_path)
57
    {
58
        if ($lfm_path->setName($new_file_name)->exists()) {
0 ignored issues
show
Bug introduced by
The method exists() does not exist on UniSharp\LaravelFilemanager\LfmPath. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

58
        if ($lfm_path->setName($new_file_name)->/** @scrutinizer ignore-call */ exists()) {
Loading history...
59
            throw new DuplicateFileNameException();
60
        }
61
62
        return $this;
63
    }
64
65
    public function isNotExcutable($excutable_mimetypes)
66
    {
67
        $mimetype = $this->file->getMimeType();
68
69
        if (in_array($mimetype, $excutable_mimetypes)) {
70
            throw new ExcutableFileException();
71
        }
72
73
        return $this;
74
    }
75
76
    function hasAllowedExtension($allowed_extensions)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
77
    {
78
        $extension = $this->file->getClientOriginalExtension();
79
80
        if (!in_array($extension, $allowed_extensions)) {
81
            throw new InvalidExtensionException($extension);
0 ignored issues
show
Unused Code introduced by
The call to UniSharp\LaravelFilemana...xception::__construct() has too many arguments starting with $extension. ( Ignorable by Annotation )

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

81
            throw /** @scrutinizer ignore-call */ new InvalidExtensionException($extension);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
82
        }
83
84
        return $this;
85
    }
86
87
    public function mimeTypeIsValid($available_mime_types)
88
    {
89
        $mimetype = $this->file->getMimeType();
90
91
        if (false === in_array($mimetype, $available_mime_types)) {
92
            throw new InvalidMimeTypeException($mimetype);
93
        }
94
95
        return $this;
96
    }
97
98
    public function sizeIsLowerThanConfiguredMaximum($max_size_in_kb)
99
    {
100
        // size to kb unit is needed
101
        $file_size_in_kb = $this->file->getSize() / 1000;
102
103
        if ($file_size_in_kb > $max_size_in_kb) {
104
            throw new FileSizeExceedConfigurationMaximumException($file_size_in_kb);
105
        }
106
107
        return $this;
108
    }
109
}
110