Passed
Push — master ( 56e406...e8d866 )
by Pierre
02:35 queued 11s
created

Uploader::setTargetPath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 5
ccs 0
cts 4
cp 0
crap 2
rs 10
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
     * container
22
     *
23
     * @var Container
24
     */
25
    protected $container;
26
27
    /**
28
     * file
29
     *
30
     * @var array
31
     */
32
    protected $file;
33
34
    /**
35
     * target path
36
     *
37
     * @var string
38
     */
39
    protected $targetPath = 'assets/upload/';
40
41
    /**
42
     * error flag
43
     *
44
     * @var Boolean
45
     */
46
    protected $error;
47
48
    /**
49
     * error code
50
     *
51
     * @var Int
52
     */
53
    protected $errorCode;
54
55
    /**
56
     * error message
57
     *
58
     * @var String
59
     */
60
    protected $errorMsg;
61
62
    /**
63
     * file name
64
     *
65
     * @var String
66
     */
67
    protected $filename;
68
69
    /**
70
     * file extension
71
     *
72
     * @var String
73
     */
74
    protected $fileext;
75
76
    /**
77
     * tmp file name
78
     *
79
     * @var String
80
     */
81
    protected $filetmpname;
82
83
    /**
84
     * file target name
85
     *
86
     * @var String
87
     */
88
    protected $filetarget;
89
90
    /**
91
     * file type
92
     *
93
     * @var String
94
     */
95
    protected $filetype;
96
97
    /**
98
     * file size
99
     *
100
     * @var Int
101
     */
102
    protected $filesize;
103
104
105
    /**
106
     * instanciate
107
     *
108
     * @param Container $container
109
     */
110
    public function __construct(Container $container)
111
    {
112
        $this->container = $container;
113
        $this->file = $_FILES;
114
        $this->error = false;
115
        $this->errorMsg = '';
116
        $this->setFileInfos();
117
        $this->setErrorMessage();
118
    }
119
120
    /**
121
     * return error
122
     *
123
     * @return boolean
124
     */
125
    public function getError(): bool
126
    {
127
        return $this->error;
128
    }
129
130
    /**
131
     * set target path
132
     *
133
     * @param string $path
134
     * @return Uploader
135
     */
136
    public function setTargetPath(string $path): Uploader
137
    {
138
        $this->targetPath = $path;
139
        $this->setFileInfos();
140
        return $this;
141
    }
142
143
    /**
144
     * move file from tmp path to target
145
     *
146
     * @return Uploader
147
     */
148
    public function process(): Uploader
149
    {
150
        if (false === $this->error) {
151
            $destFilename = $this->targetPath . $this->filename;
152
            $result = move_uploaded_file($this->filetmpname, $destFilename);
153
            if (false === $result) {
154
                $this->error = true;
155
                $this->errorCode = UPLOAD_ERR_CANT_WRITE;
156
                $this->setErrorMessage();
157
            }
158
        }
159
        return $this;
160
    }
161
162
    /**
163
     * get upload file infos
164
     *
165
     * @return array
166
     */
167
    public function getInfos(): array
168
    {
169
        return [
170
            'error' => $this->error,
171
            'errorCode' => $this->errorCode,
172
            'errorMsg' => $this->errorMsg,
173
            'datas' => [
174
                'name' => $this->filename,
175
                'ext' => $this->fileext,
176
                'tmp' => $this->filetmpname,
177
                'target' => $this->filetarget,
178
                'type' => $this->filetype,
179
                'size' => $this->filesize
180
            ],
181
        ];
182
    }
183
184
    /**
185
     * set error message from error code
186
     *
187
     * @return Uploader
188
     */
189
    protected function setErrorMessage(): Uploader
190
    {
191
        if ($this->error === false) {
192
            return $this;
193
        }
194
        $maxSize = ini_get('upload_max_filesize');
195
        switch ($this->errorCode) {
196
            case UPLOAD_ERR_INI_SIZE:
197
                $this->errorMsg = self::UPLOAD_ERR_INI_SIZE . $maxSize . '.';
198
                break;
199
            case UPLOAD_ERR_FORM_SIZE:
200
                $this->errorMsg = self::UPLOAD_ERR_FORM_SIZE . $maxSize . '.';
201
                break;
202
            case UPLOAD_ERR_NO_TMP_DIR:
203
                $this->errorMsg = self::UPLOAD_ERR_NO_TMP_DIR;
204
                break;
205
            case UPLOAD_ERR_CANT_WRITE:
206
                $this->errorMsg = self::UPLOAD_ERR_CANT_WRITE;
207
                break;
208
            case UPLOAD_ERR_EXTENSION:
209
                $this->errorMsg = self::UPLOAD_ERR_EXTENSION;
210
                break;
211
            case UPLOAD_ERR_PARTIAL:
212
                $this->errorMsg = self::UPLOAD_ERR_PARTIAL;
213
                break;
214
            case UPLOAD_ERR_NO_FILE:
215
                $this->errorMsg = self::UPLOAD_ERR_NO_FILE;
216
                break;
217
            default:
218
                $this->errorMsg = 'Unknown Error';
219
                break;
220
        }
221
        return $this;
222
    }
223
224
    /**
225
     * set files infos
226
     *
227
     */
228
    protected function setFileInfos()
229
    {
230
        $isValid = isset($this->file[self::FIELD])
231
            && !empty($this->file[self::FIELD]);
232
        $this->error = (false === $isValid);
233
        if ($isValid) {
234
            $fileInfo = $this->file[self::FIELD];
235
            $this->filename = $fileInfo['name'];
236
            $basename = basename($this->filename);
237
            $this->fileext = (!empty($basename)) ? substr($basename, -4) : '';
238
            $this->filetmpname = $fileInfo['tmp_name'];
239
            $this->filetarget = $this->targetPath . $basename;
240
            $this->errorCode = $fileInfo['error'];
241
            $this->filetype = $fileInfo['type'];
242
            $this->filesize = $fileInfo['size'];
243
            unset($fileInfo);
244
        } else {
245
            $this->errorCode = UPLOAD_ERR_NO_FILE;
246
            $this->setErrorMessage();
247
        }
248
        return $this;
249
    }
250
}
251