Passed
Push — master ( 12c411...515d4c )
by Pierre
02:31
created

Uploader::setFileInfos()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 21
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 6.7441

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 18
c 1
b 0
f 0
nc 6
nop 0
dl 0
loc 21
ccs 8
cts 18
cp 0.4444
crap 6.7441
rs 9.6666
1
<?php
2
3
namespace App\Tools\File;
4
5
use App\Container;
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 7
    public function __construct()
103
    {
104 7
        $this->file = $_FILES;
105 7
        $this->error = false;
106 7
        $this->errorMsg = '';
107 7
        $this->setFileInfos();
108 7
        $this->setErrorMessage();
109
    }
110
111
    /**
112
     * return error
113
     *
114
     * @return boolean
115
     */
116 2
    public function getError(): bool
117
    {
118 2
        return $this->error;
119
    }
120
121
    /**
122
     * set target path
123
     *
124
     * @param string $path
125
     * @return Uploader
126
     */
127 1
    public function setTargetPath(string $path): Uploader
128
    {
129 1
        $this->targetPath = $path;
130 1
        $this->setFileInfos();
131 1
        return $this;
132
    }
133
134
    /**
135
     * move file from tmp path to target
136
     *
137
     * @return Uploader
138
     */
139 1
    public function process(): Uploader
140
    {
141 1
        if (false === $this->error) {
142
            $destFilename = $this->targetPath . $this->filename;
143
            $result = move_uploaded_file($this->filetmpname, $destFilename);
144
            if (false === $result) {
145
                $this->error = true;
146
                $this->errorCode = UPLOAD_ERR_CANT_WRITE;
147
                $this->setErrorMessage();
148
            }
149
        }
150 1
        return $this;
151
    }
152
153
    /**
154
     * get upload file infos
155
     *
156
     * @return array
157
     */
158 1
    public function getInfos(): array
159
    {
160
        return [
161 1
            'error' => $this->error,
162 1
            'errorCode' => $this->errorCode,
163 1
            'errorMsg' => $this->errorMsg,
164
            'datas' => [
165 1
                'name' => $this->filename,
166 1
                'ext' => $this->fileext,
167 1
                'tmp' => $this->filetmpname,
168 1
                'target' => $this->filetarget,
169 1
                'type' => $this->filetype,
170 1
                'size' => $this->filesize
171
            ],
172
        ];
173
    }
174
175
    /**
176
     * set error message from error code
177
     *
178
     * @return Uploader
179
     */
180 1
    protected function setErrorMessage(): Uploader
181
    {
182 1
        if ($this->error === false) {
183
            return $this;
184
        }
185 1
        $maxSize = ini_get('upload_max_filesize');
186 1
        switch ($this->errorCode) {
187 1
            case UPLOAD_ERR_INI_SIZE:
188
                $this->errorMsg = self::UPLOAD_ERR_INI_SIZE . $maxSize . '.';
189
                break;
190 1
            case UPLOAD_ERR_FORM_SIZE:
191
                $this->errorMsg = self::UPLOAD_ERR_FORM_SIZE . $maxSize . '.';
192
                break;
193 1
            case UPLOAD_ERR_NO_TMP_DIR:
194
                $this->errorMsg = self::UPLOAD_ERR_NO_TMP_DIR;
195
                break;
196 1
            case UPLOAD_ERR_CANT_WRITE:
197
                $this->errorMsg = self::UPLOAD_ERR_CANT_WRITE;
198
                break;
199 1
            case UPLOAD_ERR_EXTENSION:
200
                $this->errorMsg = self::UPLOAD_ERR_EXTENSION;
201
                break;
202 1
            case UPLOAD_ERR_PARTIAL:
203
                $this->errorMsg = self::UPLOAD_ERR_PARTIAL;
204
                break;
205 1
            case UPLOAD_ERR_NO_FILE:
206 1
                $this->errorMsg = self::UPLOAD_ERR_NO_FILE;
207 1
                break;
208
            default:
209
                $this->errorMsg = 'Unknown Error';
210
                break;
211
        }
212 1
        return $this;
213
    }
214
215
    /**
216
     * set files infos
217
     *
218
     */
219 1
    protected function setFileInfos(): Uploader
220
    {
221 1
        $isValid = isset($this->file[self::FIELD])
222 1
            && !empty($this->file[self::FIELD]);
223 1
        $this->error = (false === $isValid);
224 1
        if ($isValid) {
225
            $fileInfo = $this->file[self::FIELD];
226
            $this->filename = $fileInfo['name'];
227
            $basename = basename($this->filename);
228
            $this->fileext = (!empty($basename)) ? substr($basename, -4) : '';
229
            $this->filetmpname = $fileInfo['tmp_name'];
230
            $this->filetarget = $this->targetPath . $basename;
231
            $this->errorCode = $fileInfo['error'];
232
            $this->filetype = $fileInfo['type'];
233
            $this->filesize = $fileInfo['size'];
234
            unset($fileInfo);
235
        } else {
236 1
            $this->errorCode = UPLOAD_ERR_NO_FILE;
237 1
            $this->setErrorMessage();
238
        }
239 1
        return $this;
240
    }
241
}
242