Passed
Push — master ( 893d1e...31005e )
by Kirill
03:35
created

FileChecker   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
eloc 22
c 1
b 0
f 0
dl 0
loc 89
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A exists() 0 3 1
A extension() 0 15 3
A size() 0 7 2
A uploaded() 0 3 1
1
<?php
2
3
/**
4
 * Spiral Framework.
5
 *
6
 * @license   MIT
7
 * @author    Anton Titov (Wolfy-J)
8
 */
9
10
declare(strict_types=1);
11
12
namespace Spiral\Validation\Checker;
13
14
use Psr\Http\Message\UploadedFileInterface;
15
use Spiral\Core\Container\SingletonInterface;
16
use Spiral\Files\FilesInterface;
17
use Spiral\Validation\AbstractChecker;
18
use Spiral\Validation\Checker\Traits\FileTrait;
19
20
/**
21
 * @inherit-messages
22
 */
23
final class FileChecker extends AbstractChecker implements SingletonInterface
24
{
25
    use FileTrait;
26
27
    /**
28
     * {@inheritdoc}
29
     */
30
    public const MESSAGES = [
31
        'exists'    => '[[File does not exists.]]',
32
        'uploaded'  => '[[File not received, please try again.]]',
33
        'size'      => '[[File exceeds the maximum file size of {1}KB.]]',
34
        'extension' => '[[File has an invalid file format.]]',
35
    ];
36
37
    /**
38
     * {@inheritdoc}
39
     */
40
    public const ALLOW_EMPTY_VALUES = ['exists', 'uploaded'];
41
42
    /**
43
     * @param FilesInterface $files
44
     */
45
    public function __construct(FilesInterface $files)
46
    {
47
        $this->files = $files;
48
    }
49
50
    /**
51
     * Check if file exist.
52
     *
53
     * @param mixed $file
54
     * @return bool
55
     */
56
    public function exists($file): bool
57
    {
58
        return !empty($this->resolveFilename($file));
59
    }
60
61
    /**
62
     * Check if file been uploaded.
63
     *
64
     * @param mixed $file Local file or uploaded file array.
65
     * @return bool
66
     */
67
    public function uploaded($file): bool
68
    {
69
        return $this->isUploaded($file);
70
    }
71
72
    /**
73
     * Check if file size less that specified value in KB.
74
     *
75
     * @param mixed $file Local file or uploaded file array.
76
     * @param int   $size Size in KBytes.
77
     * @return bool
78
     */
79
    public function size($file, int $size): bool
80
    {
81
        if (empty($filename = $this->resolveFilename($file))) {
82
            return false;
83
        }
84
85
        return $this->files->size($filename) <= ($size * 1024);
86
    }
87
88
    /**
89
     * Check if file extension in whitelist. Client name of uploaded file will be used!
90
     * It is recommended to use external validation like media type based on file mimetype or
91
     * ensure that resource is properly converted.
92
     *
93
     * @param mixed        $file
94
     * @param array|string $extensions
95
     * @return bool
96
     */
97
    public function extension($file, $extensions): bool
98
    {
99
        if (!is_array($extensions)) {
100
            $extensions = array_slice(func_get_args(), 1);
101
        }
102
103
        if ($file instanceof UploadedFileInterface) {
104
            return in_array(
105
                $this->files->extension($file->getClientFilename()),
0 ignored issues
show
Bug introduced by
It seems like $file->getClientFilename() can also be of type null; however, parameter $filename of Spiral\Files\FilesInterface::extension() 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

105
                $this->files->extension(/** @scrutinizer ignore-type */ $file->getClientFilename()),
Loading history...
106
                $extensions,
107
                true
108
            );
109
        }
110
111
        return in_array($this->files->extension($file), $extensions, true);
112
    }
113
}
114