Uploader::setErrorMessage()   B
last analyzed

Complexity

Conditions 9
Paths 9

Size

Total Lines 33
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 29
CRAP Score 9

Importance

Changes 0
Metric Value
cc 9
eloc 29
nc 9
nop 0
dl 0
loc 33
ccs 29
cts 29
cp 1
crap 9
rs 8.0555
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Component\File;
6
7
class Uploader
8
{
9
10
    const FIELD = 'file';
11
    const UPLOAD_ERR_INI_SIZE = 'La taille du fichier dépasse celle autorisée , upload_max_filesize = ';
12
    const UPLOAD_ERR_FORM_SIZE = 'La taille du fichier est trops importante.';
13
    const UPLOAD_ERR_NO_TMP_DIR = 'Le paramétrage du répertoire temporaire est incorrecte.';
14
    const UPLOAD_ERR_CANT_WRITE = 'Échec de l\'écriture du fichier sur le disque.';
15
    const UPLOAD_ERR_EXTENSION = 'Ce type de fichier n\'est pas autorisé.';
16
    const UPLOAD_ERR_PARTIAL = 'Le fichier n\'a été que partiellement téléchargé.';
17
    const UPLOAD_ERR_NO_FILE = 'Aucun fichier n\'a été téléchargé.';
18
    const UPLOAD_ERR_UNKOWN = 'Erreur inconnue.';
19
20
    /**
21
     * file
22
     *
23
     * @var array
24
     */
25
    protected $file;
26
27
    /**
28
     * target path
29
     *
30
     * @var string
31
     */
32
    protected $targetPath = 'assets/upload/';
33
34
    /**
35
     * error flag
36
     *
37
     * @var Boolean
38
     */
39
    protected $error;
40
41
    /**
42
     * error code
43
     *
44
     * @var Int
45
     */
46
    protected $errorCode;
47
48
    /**
49
     * error message
50
     *
51
     * @var String
52
     */
53
    protected $errorMsg;
54
55
    /**
56
     * file name
57
     *
58
     * @var String
59
     */
60
    protected $filename;
61
62
    /**
63
     * file extension
64
     *
65
     * @var String
66
     */
67
    protected $fileext;
68
69
    /**
70
     * tmp file name
71
     *
72
     * @var String
73
     */
74
    protected $filetmpname;
75
76
    /**
77
     * file target name
78
     *
79
     * @var String
80
     */
81
    protected $filetarget;
82
83
    /**
84
     * file type
85
     *
86
     * @var String
87
     */
88
    protected $filetype;
89
90
    /**
91
     * file size
92
     *
93
     * @var Int
94
     */
95
    protected $filesize;
96
97
98
    /**
99
     * instanciate
100
     *
101
     */
102 26
    public function __construct()
103
    {
104 26
        $this->setErrorCode(UPLOAD_ERR_NO_FILE);
105 26
        $this->setFile();
106
    }
107
108
    /**
109
     * return error
110
     *
111
     * @return boolean
112
     */
113 2
    public function isError(): bool
114
    {
115 2
        return $this->error;
116
    }
117
118
    /**
119
     * set target path
120
     *
121
     * @param string $path
122
     * @return Uploader
123
     */
124 1
    public function setTargetPath(string $path): Uploader
125
    {
126 1
        $this->targetPath = $path;
127 1
        $this->setFileInfos();
128 1
        return $this;
129
    }
130
131
    /**
132
     * move file from tmp path to target
133
     *
134
     * @return Uploader
135
     */
136 1
    public function process(): Uploader
137
    {
138 1
        if (false === $this->error) {
139 1
            $destFilename = $this->targetPath . $this->filename;
140 1
            $result = move_uploaded_file($this->filetmpname, $destFilename);
141 1
            if (false === $result) {
142 1
                $this->setErrorCode(UPLOAD_ERR_CANT_WRITE);
143
            }
144
        }
145 1
        return $this;
146
    }
147
148
    /**
149
     * get upload file infos
150
     *
151
     * @return array
152
     */
153 10
    public function getInfos(): array
154
    {
155
        return [
156 10
            'error' => $this->error,
157 10
            'errorCode' => $this->errorCode,
158 10
            'errorMsg' => $this->errorMsg,
159
            'datas' => [
160 10
                'name' => $this->filename,
161 10
                'ext' => $this->fileext,
162 10
                'tmp' => $this->filetmpname,
163 10
                'target' => $this->filetarget,
164 10
                'type' => $this->filetype,
165 10
                'size' => $this->filesize
166
            ],
167
        ];
168
    }
169
170
    /**
171
     * set file either from global $_FILES or $files
172
     *
173
     * @return Uploader
174
     */
175 3
    protected function setFile(array $files = []): Uploader
176
    {
177 3
        $this->file = (!empty($files)) ? $files : $_FILES;
178 3
        $this->setFileInfos();
179 3
        return $this;
180
    }
181
182
    /**
183
     * set error code
184
     *
185
     * @param integer $errorCode
186
     * @return Uploader
187
     */
188 9
    protected function setErrorCode(int $errorCode): Uploader
189
    {
190 9
        $this->errorCode = $errorCode;
191 9
        $this->error = ($errorCode != 0);
192 9
        $this->setErrorMessage();
193 9
        return $this;
194
    }
195
196
    /**
197
     * set error message from error code
198
     *
199
     * @return Uploader
200
     */
201 10
    protected function setErrorMessage(): Uploader
202
    {
203 10
        if ($this->error === false) {
204 1
            $this->errorMsg = '';
205 1
            return $this;
206
        }
207 10
        switch ($this->errorCode) {
208 10
            case UPLOAD_ERR_INI_SIZE:
209 1
                $this->errorMsg = self::UPLOAD_ERR_INI_SIZE;
210 1
                break;
211 10
            case UPLOAD_ERR_FORM_SIZE:
212 1
                $this->errorMsg = self::UPLOAD_ERR_FORM_SIZE;
213 1
                break;
214 10
            case UPLOAD_ERR_NO_TMP_DIR:
215 1
                $this->errorMsg = self::UPLOAD_ERR_NO_TMP_DIR;
216 1
                break;
217 10
            case UPLOAD_ERR_CANT_WRITE:
218 1
                $this->errorMsg = self::UPLOAD_ERR_CANT_WRITE;
219 1
                break;
220 10
            case UPLOAD_ERR_EXTENSION:
221 1
                $this->errorMsg = self::UPLOAD_ERR_EXTENSION;
222 1
                break;
223 10
            case UPLOAD_ERR_PARTIAL:
224 1
                $this->errorMsg = self::UPLOAD_ERR_PARTIAL;
225 1
                break;
226 10
            case UPLOAD_ERR_NO_FILE:
227 10
                $this->errorMsg = self::UPLOAD_ERR_NO_FILE;
228 10
                break;
229
            default:
230 1
                $this->errorMsg = self::UPLOAD_ERR_UNKOWN;
231 1
                break;
232
        }
233 10
        return $this;
234
    }
235
236
    /**
237
     * set files infos
238
     *
239
     */
240 1
    protected function setFileInfos(): Uploader
241
    {
242 1
        $isValid = isset($this->file[self::FIELD])
243 1
            && !empty($this->file[self::FIELD]);
244 1
        if ($isValid) {
245 1
            $this->setErrorCode(0);
246 1
            $fileInfo = $this->file[self::FIELD];
247 1
            $this->filename = $fileInfo['name'];
248 1
            $basename = basename($this->filename);
249 1
            $this->fileext = (!empty($basename)) ? substr($basename, -4) : '';
250 1
            $this->filetmpname = $fileInfo['tmp_name'];
251 1
            $this->filetarget = $this->targetPath . $basename;
252 1
            $this->errorCode = $fileInfo['error'];
253 1
            $this->filetype = $fileInfo['type'];
254 1
            $this->filesize = $fileInfo['size'];
255 1
            unset($fileInfo);
256
        } else {
257 1
            $this->setErrorCode(UPLOAD_ERR_NO_FILE);
258
        }
259 1
        return $this;
260
    }
261
}
262