Completed
Push — master ( b6e546...9a6f5b )
by Pierre
02:45
created

Uploader   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 253
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 97
dl 0
loc 253
ccs 0
cts 81
cp 0
rs 10
c 0
b 0
f 0
wmc 23

9 Methods

Rating   Name   Duplication   Size   Complexity  
A process() 0 10 3
A getInfos() 0 13 1
A __construct() 0 4 1
A setErrorCode() 0 6 1
A isError() 0 3 1
A setFile() 0 5 2
A setFileInfos() 0 20 4
B setErrorMessage() 0 33 9
A setTargetPath() 0 5 1
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
    public function __construct()
101
    {
102
        $this->setErrorCode(UPLOAD_ERR_NO_FILE);
103
        $this->setFile();
104
    }
105
106
    /**
107
     * return error
108
     *
109
     * @return boolean
110
     */
111
    public function isError(): bool
112
    {
113
        return $this->error;
114
    }
115
116
    /**
117
     * set target path
118
     *
119
     * @param string $path
120
     * @return Uploader
121
     */
122
    public function setTargetPath(string $path): Uploader
123
    {
124
        $this->targetPath = $path;
125
        $this->setFileInfos();
126
        return $this;
127
    }
128
129
    /**
130
     * move file from tmp path to target
131
     *
132
     * @return Uploader
133
     */
134
    public function process(): Uploader
135
    {
136
        if (false === $this->error) {
137
            $destFilename = $this->targetPath . $this->filename;
138
            $result = move_uploaded_file($this->filetmpname, $destFilename);
139
            if (false === $result) {
140
                $this->setErrorCode(UPLOAD_ERR_CANT_WRITE);
141
            }
142
        }
143
        return $this;
144
    }
145
146
    /**
147
     * get upload file infos
148
     *
149
     * @return array
150
     */
151
    public function getInfos(): array
152
    {
153
        return [
154
            'error' => $this->error,
155
            'errorCode' => $this->errorCode,
156
            'errorMsg' => $this->errorMsg,
157
            'datas' => [
158
                'name' => $this->filename,
159
                'ext' => $this->fileext,
160
                'tmp' => $this->filetmpname,
161
                'target' => $this->filetarget,
162
                'type' => $this->filetype,
163
                'size' => $this->filesize
164
            ],
165
        ];
166
    }
167
168
    /**
169
     * set file either from global $_FILES or $files
170
     *
171
     * @return Uploader
172
     */
173
    protected function setFile(array $files = []): Uploader
174
    {
175
        $this->file = (!empty($files)) ? $files : $_FILES;
176
        $this->setFileInfos();
177
        return $this;
178
    }
179
180
    /**
181
     * set error code
182
     *
183
     * @param integer $errorCode
184
     * @return Uploader
185
     */
186
    protected function setErrorCode(int $errorCode): Uploader
187
    {
188
        $this->errorCode = $errorCode;
189
        $this->error = ($errorCode != 0);
190
        $this->setErrorMessage();
191
        return $this;
192
    }
193
194
    /**
195
     * set error message from error code
196
     *
197
     * @return Uploader
198
     */
199
    protected function setErrorMessage(): Uploader
200
    {
201
        if ($this->error === false) {
202
            $this->errorMsg = '';
203
            return $this;
204
        }
205
        switch ($this->errorCode) {
206
            case UPLOAD_ERR_INI_SIZE:
207
                $this->errorMsg = self::UPLOAD_ERR_INI_SIZE;
208
                break;
209
            case UPLOAD_ERR_FORM_SIZE:
210
                $this->errorMsg = self::UPLOAD_ERR_FORM_SIZE;
211
                break;
212
            case UPLOAD_ERR_NO_TMP_DIR:
213
                $this->errorMsg = self::UPLOAD_ERR_NO_TMP_DIR;
214
                break;
215
            case UPLOAD_ERR_CANT_WRITE:
216
                $this->errorMsg = self::UPLOAD_ERR_CANT_WRITE;
217
                break;
218
            case UPLOAD_ERR_EXTENSION:
219
                $this->errorMsg = self::UPLOAD_ERR_EXTENSION;
220
                break;
221
            case UPLOAD_ERR_PARTIAL:
222
                $this->errorMsg = self::UPLOAD_ERR_PARTIAL;
223
                break;
224
            case UPLOAD_ERR_NO_FILE:
225
                $this->errorMsg = self::UPLOAD_ERR_NO_FILE;
226
                break;
227
            default:
228
                $this->errorMsg = self::UPLOAD_ERR_UNKOWN;
229
                break;
230
        }
231
        return $this;
232
    }
233
234
    /**
235
     * set files infos
236
     *
237
     */
238
    protected function setFileInfos(): Uploader
239
    {
240
        $isValid = isset($this->file[self::FIELD])
241
            && !empty($this->file[self::FIELD]);
242
        if ($isValid) {
243
            $this->setErrorCode(0);
244
            $fileInfo = $this->file[self::FIELD];
245
            $this->filename = $fileInfo['name'];
246
            $basename = basename($this->filename);
247
            $this->fileext = (!empty($basename)) ? substr($basename, -4) : '';
248
            $this->filetmpname = $fileInfo['tmp_name'];
249
            $this->filetarget = $this->targetPath . $basename;
250
            $this->errorCode = $fileInfo['error'];
251
            $this->filetype = $fileInfo['type'];
252
            $this->filesize = $fileInfo['size'];
253
            unset($fileInfo);
254
        } else {
255
            $this->setErrorCode(UPLOAD_ERR_NO_FILE);
256
        }
257
        return $this;
258
    }
259
}
260