GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Passed
Push — develop ( 7ff7bf...48165e )
by nguereza
02:06
created

Upload::__construct()   A

Complexity

Conditions 6
Paths 7

Size

Total Lines 29
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 14
nc 7
nop 3
dl 0
loc 29
rs 9.2222
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Platine Upload
5
 *
6
 * Platine Upload provides a flexible file uploads with extensible
7
 * validation and storage strategies.
8
 *
9
 * This content is released under the MIT License (MIT)
10
 *
11
 * Copyright (c) 2020 Platine Upload
12
 *
13
 * @author      Josh Lockhart <[email protected]>
14
 * @copyright   2012 Josh Lockhart
15
 * @link        http://www.joshlockhart.com
16
 * @version     2.0.0
17
 *
18
 * Permission is hereby granted, free of charge, to any person obtaining a copy
19
 * of this software and associated documentation files (the "Software"), to deal
20
 * in the Software without restriction, including without limitation the rights
21
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
22
 * copies of the Software, and to permit persons to whom the Software is
23
 * furnished to do so, subject to the following conditions:
24
 *
25
 * The above copyright notice and this permission notice shall be included in all
26
 * copies or substantial portions of the Software.
27
 *
28
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
29
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
30
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
31
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
32
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
33
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
34
 * SOFTWARE.
35
 */
36
37
/**
38
 *  @file Upload.php
39
 *
40
 *  The main Upload class
41
 *
42
 *  @package    Platine\Upload
43
 *  @author Platine Developers Team
44
 *  @copyright  Copyright (c) 2020
45
 *  @license    http://opensource.org/licenses/MIT  MIT License
46
 *  @link   http://www.iacademy.cf
47
 *  @version 1.0.0
48
 *  @filesource
49
 */
50
51
declare(strict_types=1);
52
53
namespace Platine\Upload;
54
55
use Platine\Http\UploadedFile;
56
use Platine\Upload\File\File;
57
use Platine\Upload\File\UploadFileInfo;
58
use Platine\Upload\Storage\StorageInterface;
59
use Platine\Upload\Util\Helper;
60
use Platine\Upload\Validator\Rule\UploadError;
61
use Platine\Upload\Validator\RuleInterface;
62
use Platine\Upload\Validator\Validator;
63
use RuntimeException;
64
65
/**
66
 * Class Upload
67
 * @package Platine\Upload
68
 */
69
class Upload
70
{
0 ignored issues
show
Coding Style introduced by
Opening brace must not be followed by a blank line
Loading history...
71
72
    /**
73
     * Upload Storage
74
     * @var StorageInterface
75
     */
76
    protected StorageInterface $storage;
77
78
    /**
79
     * The upload validation object
80
     * @var Validator
81
     */
82
    protected Validator $validator;
83
84
    /**
85
     * The list of uploaded file
86
     * @var array<int, File>
87
     */
88
    protected array $files = [];
89
90
    /**
91
     * The validations errors messages
92
     * @var array<int, string>
93
     */
94
    protected array $errors = [];
95
96
    /**
97
     * Set custom file name
98
     * @var string
99
     */
100
    protected string $filename;
101
102
    /**
103
     * The uploaded file information
104
     * @var UploadFileInfo|array<int, UploadFileInfo>|bool
105
     */
106
    protected $uploadInfo = false;
107
108
    /**
109
     * Create new instance
110
     * @param string $key the file key to use
111
     * @param StorageInterface $storage
112
     * @param Validator|null $validator
113
     */
114
    public function __construct(
115
        string $key,
116
        StorageInterface $storage,
117
        ?Validator $validator = null
118
    ) {
119
        if ((bool) ini_get('file_uploads') === false) {
120
            throw new RuntimeException('File uploads are disabled in your PHP.ini file');
121
        }
122
123
        $this->storage = $storage;
124
        $this->validator = $validator ? $validator : new Validator();
125
126
        $uploadedFiles = UploadedFile::createFromGlobals();
127
128
        $files = Helper::normalizeFiles($uploadedFiles);
129
130
        if (array_key_exists($key, $files)) {
131
            $fileInfo = $files[$key];
132
            if (is_array($fileInfo)) {
133
                foreach ($fileInfo as $file) {
134
                    $this->files[] = $file;
135
                }
136
            } else {
137
                $this->files[] = $fileInfo;
138
            }
139
        }
140
141
        //add default validation rule
142
        $this->addDefaultValidations();
143
    }
144
    
145
    /**
146
    * Whether the file is uploaded
147
    */
148
    public function isUploaded(): bool
149
    {
150
        return count($this->files) > 0 
151
				&& $this->files[0]->getMimeType() !== 'application/x-empty';
152
    }
153
154
    /**
155
     * Set custom filename
156
     * @param string $name
157
     * @return $this
158
     */
159
    public function setFilename(string $name): self
160
    {
161
        foreach ($this->files as $file) {
162
            $file->setName($name);
163
        }
164
165
        return $this;
166
    }
167
168
    /**
169
     * Shortcut to Validator::addRule
170
     * @param RuleInterface $rule
171
     * @return $this
172
     */
173
    public function addValidation(RuleInterface $rule): self
174
    {
175
        $this->validator->addRule($rule);
176
177
        return $this;
178
    }
179
180
    /**
181
     * Add validations array
182
     * @param array<int, RuleInterface> $rules
183
     * @return self
184
     */
185
    public function addValidations(array $rules): self
186
    {
187
        $this->validator->addRules($rules);
188
189
        return $this;
190
    }
191
192
    /**
193
     * Check whether the file to upload is valid
194
     * @return bool
195
     */
196
    public function isValid(): bool
197
    {
198
        foreach ($this->files as $file) {
199
            $this->validateFile($file);
200
        }
201
202
        return empty($this->errors);
203
    }
204
205
    /**
206
     * Process upload
207
     * @return bool
208
     */
209
    public function process(): bool
210
    {
211
        if (!$this->isValid()) {
212
            return false;
213
        }
214
215
        $result = [];
216
217
        foreach ($this->files as $file) {
218
            if ($file->getError() === UPLOAD_ERR_OK) {
219
                $result[] = $this->storage->upload($file);
220
            }
221
        }
222
223
        if (count($result) === 1) {
224
            $this->uploadInfo = $result[0];
225
        } else {
226
            $this->uploadInfo = $result;
227
        }
228
229
        return true;
230
    }
231
232
    /**
233
     * Return the validation errors
234
     * @return array<int, string>
235
     */
236
    public function getErrors(): array
237
    {
238
        return $this->errors;
239
    }
240
241
    /**
242
     * Return the uploaded file information
243
     * @return UploadFileInfo|array<int, UploadFileInfo>|bool
244
     */
245
    public function getInfo()
246
    {
247
        return $this->uploadInfo;
248
    }
249
250
    /**
251
     * Validate the uploaded file
252
     * @param File $file
253
     * return void
254
     */
255
    protected function validateFile(File $file): void
256
    {
257
        foreach ($this->validator->getRules() as $rule) {
258
            /** @var RuleInterface $rule */
259
            $isValid = $rule->validate($file);
260
            if (!$isValid) {
261
                $this->errors[] = $rule->getErrorMessage($file);
262
                break;
263
            }
264
        }
265
    }
266
267
    /**
268
     * Add default rules validations
269
     * @return void
270
     */
271
    protected function addDefaultValidations(): void
272
    {
273
        $this->validator->addRules([
274
            new UploadError()
275
        ]);
276
    }
277
}
278