UploadError   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
dl 0
loc 31
rs 10
c 1
b 0
f 0
wmc 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A validate() 0 5 1
A getErrorMessage() 0 13 1
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 UploadError.php
39
 *
40
 *  The PHP upload constants UPLOAD_ERR_* validation rule class
41
 *
42
 *  @package    Platine\Upload\Validator
43
 *  @author Platine Developers Team
44
 *  @copyright  Copyright (c) 2020
45
 *  @license    http://opensource.org/licenses/MIT  MIT License
46
 *  @link   https://www.platine-php.com
47
 *  @version 1.0.0
48
 *  @filesource
49
 */
50
51
declare(strict_types=1);
52
53
namespace Platine\Upload\Validator\Rule;
54
55
use Platine\Upload\File\File;
56
use Platine\Upload\Validator\RuleInterface;
57
58
/**
59
 * @class UploadError
60
 * @package Platine\Upload\Validator\Rule
61
 */
62
class UploadError implements RuleInterface
63
{
64
    /**
65
     * {@inheritdoc}
66
     * @see RuleInterface
67
     */
68
    public function validate(File $file): bool
69
    {
70
        return in_array(
71
            $file->getError(),
72
            [UPLOAD_ERR_OK, UPLOAD_ERR_NO_FILE]
73
        );
74
    }
75
76
    /**
77
     * {@inheritdoc}
78
     * @see RuleInterface
79
     */
80
    public function getErrorMessage(File $file): string
81
    {
82
        $errorMaps = [
83
            1 => 'The uploaded file exceeds the upload_max_filesize directive in php.ini',
84
            2 => 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form',
85
            3 => 'The uploaded file was only partially uploaded',
86
            4 => 'No file was uploaded',
87
            6 => 'Missing a temporary folder',
88
            7 => 'Failed to write file to disk.',
89
            8 => 'A PHP extension stopped the file upload.',
90
        ];
91
92
        return $errorMaps[$file->getError()] ?? '';
93
    }
94
}
95