Completed
Push — master ( 9a6f5b...ef0055 )
by Pierre
02:26
created

Uploader::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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