Passed
Push — master ( 05da8b...b115dc )
by Stream
04:22
created

LfmUploadValidator::sizeLowerThanIniMaximum()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 3
c 1
b 0
f 1
nc 2
nop 0
dl 0
loc 7
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\InvalidMimeTypeException;
13
use UniSharp\LaravelFilemanager\LfmPath;
14
15
class LfmUploadValidator
16
{
17
    private $file;
18
19
    public function __construct(UploadedFile $file)
20
    {
21
        // if (! $file instanceof UploadedFile) {
22
        //     throw new \Exception(trans(self::PACKAGE_NAME . '::lfm.error-instance'));
23
        // }
24
25
        $this->file = $file;
26
    }
27
28
    // public function hasContent()
29
    // {
30
    //     if (empty($this->file)) {
31
    //         throw new EmptyFileException();
32
    //     }
33
34
    //     return $this;
35
    // }
36
37
    public function sizeLowerThanIniMaximum()
38
    {
39
        if ($this->file->getError() == UPLOAD_ERR_INI_SIZE) {
40
            throw new FileSizeExceedIniMaximumException();
41
        }
42
43
        return $this;
44
    }
45
46
    public function uploadWasSuccessful()
47
    {
48
        if ($this->file->getError() != UPLOAD_ERR_OK) {
49
            throw new FileFailedToUploadException($this->file->getError());
50
        }
51
52
        return $this;
53
    }
54
55
    public function nameIsNotDuplicate($new_file_name, LfmPath $lfm_path)
56
    {
57
        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

57
        if ($lfm_path->setName($new_file_name)->/** @scrutinizer ignore-call */ exists()) {
Loading history...
58
            throw new DuplicateFileNameException();
59
        }
60
61
        return $this;
62
    }
63
64
    public function isNotExcutable()
65
    {
66
        $mimetype = $this->file->getMimeType();
67
68
        $excutable = ['text/x-php'];
69
70
        if (in_array($mimetype, $excutable)) {
71
            throw new ExcutableFileException();
72
        }
73
74
        return $this;
75
    }
76
77
    public function mimeTypeIsValid($available_mime_types)
78
    {
79
        $mimetype = $this->file->getMimeType();
80
81
        if (false === in_array($mimetype, $available_mime_types)) {
82
            throw new InvalidMimeTypeException($mimetype);
83
        }
84
85
        return $this;
86
    }
87
88
    public function sizeIsLowerThanConfiguredMaximum($max_size_in_kb)
89
    {
90
        // size to kb unit is needed
91
        $file_size_in_kb = $this->file->getSize() / 1000;
92
93
        if ($file_size_in_kb > $max_size_in_kb) {
94
            throw new FileSizeExceedConfigurationMaximumException($file_size_in_kb);
95
        }
96
97
        return $this;
98
    }
99
}
100