Passed
Push — master ( 42b488...e2797f )
by Lee
01:54
created

Upload::isValidPSR7Upload()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 0
dl 0
loc 11
ccs 7
cts 7
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Lavibi\Popoya;
4
5
use Psr\Http\Message\UploadedFileInterface;
6
7
class Upload extends AbstractValidator
8
{
9
    /**
10
     * Error code : PHP upload error code.
11
     */
12
    const PHP_INI_SIZE = 'ini_size';
13
    const PHP_FORM_SIZE = 'form_size';
14
    const PHP_PARTIAL = 'partial';
15
    const PHP_NO_FILE = 'no_file';
16
    const PHP_NO_TMP_DIR = 'no_tmp_dir';
17
    const PHP_CANT_WRITE = 'cant_write';
18
    const PHP_EXTENSION = 'err_extension';
19
    const UNKNOWN_PHP_ERROR = 'unknown_php_error';
20
21
    /**
22
     * Error code : Unknown error
23
     */
24
    const NO_UPLOADED_FILE = 'no_uploaded_file';
25
26
    /**
27
     * Error code : Not valid type
28
     */
29
    const NOT_VALID_TYPE = 'not_valid_type';
30
31
32
    const NOT_UPLOAD_DATA = 'not_upload_data';
33
34
    /**
35
     * @var array|UploadedFileInterface|mixed
36
     */
37
    protected $value;
38
39
    /**
40
     * Error messages
41
     *
42
     * @var array
43
     */
44
    protected $messages = [
45
        self::PHP_INI_SIZE => 'Uploaded file exceeds the defined PHP INI size.',
46
        self::PHP_FORM_SIZE => 'Uploaded file exceeds the defined form size.',
47
        self::PHP_PARTIAL => 'Uploaded file was only partially uploaded.',
48
        self::PHP_NO_FILE => 'Uploaded file was not uploaded.',
49
        self::PHP_NO_TMP_DIR => 'Missing a temporary folder.',
50
        self::PHP_CANT_WRITE => 'Failed to write uploaded file to disk.',
51
        self::PHP_EXTENSION => 'Uploaded file was stopped by extension.',
52
        self::UNKNOWN_PHP_ERROR => 'Unknown PHP error.',
53
        self::NO_UPLOADED_FILE => 'No uploaded file.',
54
//        self::NOT_VALID_TYPE => 'Uploaded file has not valid type.',
55
        self::NOT_UPLOAD_DATA => 'Value is not valid upload data.'
56
    ];
57
58
    /**
59
     * Set allow file type (image/gif, image/png ...)
60
     *
61
     * @param string $type
62
     *
63
     * @return $this
64
     */
65
//    public function allowType($type)
66
//    {
67
//        $this->options['type'][] = (string) $type;
68
//
69
//        return $this;
70
//    }
71
72
    /**
73
     * Validate.
74
     *
75
     * @param mixed $value
76
     *
77
     * @return boolean
78
     */
79 6
    public function isValid($value)
80
    {
81 6
        $this->value = $value;
82
83 6
        if ($value instanceof UploadedFileInterface) {
84 2
            return $this->isValidPSR7Upload();
85
        }
86
87 4
        if (is_array($value) && isset($value['tmp_name']) && isset($value['error'])) {
88 3
            return $this->isValidUploadFile();
89
        }
90
91 1
        $this->setError(static::NOT_UPLOAD_DATA);
92 1
        return false;
93
    }
94
95
    /**
96
     * Validate uploaded file.
97
     *
98
     * @return boolean
99
     */
100 3
    protected function isValidUploadFile()
101
    {
102 3
        $error = $this->value['error'];
103 3
        $path = $this->value['tmp_name'];
104
105 3
        if ($code = $this->isErrorUpload($error)) {
106 1
            $this->setError($code);
107 1
            return false;
108
        }
109
110 2
        if (!is_uploaded_file($path)) {
111 1
            $this->setError(static::NO_UPLOADED_FILE);
112 1
            return false;
113
        }
114
115 1
        $this->standardValue = $this->value;
116 1
        return true;
117
    }
118
119 2
    protected function isValidPSR7Upload()
120
    {
121 2
        $error = $this->value->getError();
122
123 2
        if ($code = $this->isErrorUpload($error)) {
124 1
            $this->setError($code);
125 1
            return false;
126
        }
127
128 1
        $this->standardValue = $this->value;
129 1
        return true;
130
    }
131
132 5
    protected function isErrorUpload($error)
133
    {
134
        $case = [
135 5
            UPLOAD_ERR_INI_SIZE => self::PHP_INI_SIZE,
136 5
            UPLOAD_ERR_FORM_SIZE => self::PHP_FORM_SIZE,
137 5
            UPLOAD_ERR_PARTIAL => self::PHP_PARTIAL,
138 5
            UPLOAD_ERR_NO_FILE => self::PHP_NO_FILE,
139 5
            UPLOAD_ERR_NO_TMP_DIR => self::PHP_NO_TMP_DIR,
140 5
            UPLOAD_ERR_CANT_WRITE => self::PHP_CANT_WRITE,
141 5
            UPLOAD_ERR_EXTENSION => self::PHP_EXTENSION
142
        ];
143
144 5
        if (isset($case[$error])) {
145 2
            return $case[$error];
146
        }
147
148 5
        if ($error !== 0) {
149 2
            return static::UNKNOWN_PHP_ERROR;
150
        }
151
152 3
        return false;
153
    }
154
155
//    protected function isValidFileType($type)
156
//    {
157
//        // Allow all mines
158
//        if ($this->options['type'] === []) {
159
//            return true;
160
//        }
161
//
162
//        // Not valid mine
163
//        if (!in_array($type, $this->options['type'])) {
164
//            $this->setError(self::NOT_VALID_TYPE);
165
//            return false;
166
//        }
167
//
168
//        return true;
169
//    }
170
}
171