FileUploadException::__construct()   B
last analyzed

Complexity

Conditions 6
Paths 4

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
dl 0
loc 17
rs 8.8571
c 2
b 0
f 0
cc 6
eloc 9
nc 4
nop 1
1
<?php
2
/**
3
 * Author: Nil Portugués Calderó <[email protected]>
4
 * Date: 9/25/14
5
 * Time: 10:53 PM
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace NilPortugues\Validator\Validation\FileUpload;
12
13
/**
14
 * Class FileUploadException
15
 * @package NilPortugues\Validator\Validation\FileUploadAttribute
16
 */
17
class FileUploadException extends \Exception
18
{
19
    /**
20
     * @var array
21
     */
22
    private $errorMessages = [
23
        UPLOAD_ERR_INI_SIZE   => "FileUpload::UPLOAD_ERR_INI_SIZE",
24
        UPLOAD_ERR_FORM_SIZE  => "FileUpload::UPLOAD_ERR_FORM_SIZE",
25
        UPLOAD_ERR_PARTIAL    => "FileUpload::UPLOAD_ERR_PARTIAL",
26
        UPLOAD_ERR_NO_FILE    => "FileUpload::UPLOAD_ERR_NO_FILE",
27
        UPLOAD_ERR_NO_TMP_DIR => "FileUpload::UPLOAD_ERR_NO_TMP_DIR",
28
        UPLOAD_ERR_CANT_WRITE => "FileUpload::UPLOAD_ERR_CANT_WRITE",
29
        UPLOAD_ERR_EXTENSION  => "FileUpload::UPLOAD_ERR_EXTENSION",
30
    ];
31
32
    /**
33
     * @param string $uploadName
34
     */
35
    public function __construct($uploadName)
0 ignored issues
show
Coding Style introduced by
__construct uses the super-global variable $_FILES which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
36
    {
37
        $error = 4;
38
39
        if (!empty($_FILES[$uploadName]['error']) && \is_int($_FILES[$uploadName]['error'])) {
40
            $error = $_FILES[$uploadName]['error'];
41
        }
42
43
        if (!empty($_FILES[$uploadName]['error'])
44
            && \is_array($_FILES[$uploadName]['error'])
45
            && \is_int($_FILES[$uploadName]['error'][0])
46
        ) {
47
            $error = \reset($_FILES[$uploadName]['error']);
48
        }
49
50
        $this->message = $this->errorMessages[$error];
51
    }
52
}
53