Completed
Push — master ( 060c67...cb2641 )
by Tomas
02:33
created

InputParam::processMultiFileUploads()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 10
ccs 8
cts 8
cp 1
rs 9.4285
cc 3
eloc 6
nc 3
nop 1
crap 3
1
<?php
2
3
namespace Tomaj\NetteApi\Params;
4
5
use Exception;
6
7
class InputParam implements ParamInterface
8
{
9
    const TYPE_POST   = 'POST';
10
    const TYPE_GET    = 'GET';
11
    const TYPE_FILE   = 'FILE';
12
13
    const OPTIONAL = false;
14
    const REQUIRED = true;
15
16
    /**
17
     * @var string
18
     */
19
    private $type;
20
21
    /**
22
     * @var string
23
     */
24
    private $key;
25
26
    /**
27
     * @var bool
28
     */
29
    private $required;
30
31
    /**
32
     * @var array|null
33
     */
34
    private $availableValues;
35
36
    /**
37
     * @var bool
38
     */
39
    private $multi;
40
41 36
    public function __construct($type, $key, $required = self::OPTIONAL, $availableValues = null, $multi = false)
42
    {
43 36
        $this->type = $type;
44 36
        $this->key = $key;
45 36
        $this->required = $required;
46 36
        $this->availableValues = $availableValues;
47 36
        $this->multi = $multi;
48 36
    }
49
50
    /**
51
     * @return string
52
     */
53 9
    public function getType()
54
    {
55 9
        return $this->type;
56
    }
57
58
    /**
59
     * @return string
60
     */
61 18
    public function getKey()
62
    {
63 18
        return $this->key;
64
    }
65
66
    /**
67
     * @return boolean
68
     */
69 6
    public function isRequired()
70
    {
71 6
        return $this->required;
72
    }
73
74 6
    public function getAvailableValues()
75
    {
76 6
        return $this->availableValues;
77
    }
78
79
    /**
80
     * @return bool
81
     */
82 18
    public function isMulti()
83
    {
84 18
        return $this->multi;
85
    }
86
87
    /**
88
     * Check if actual value from environment is valid
89
     *
90
     * @return bool
91
     *
92
     * @throws Exception if actual InputParam has unsupported type
93
     */
94 15
    public function isValid()
95
    {
96 15
        $value = $this->getValue();
97 15
        if ($this->availableValues !== null) {
98 6
            if (is_array($this->availableValues)) {
99 3
                return in_array($value, $this->availableValues);
100
            }
101 3
        }
102
103 15
        if ($this->required) {
104 15
            if ($value === null || $value == '') {
105 6
                return false;
106
            }
107 9
            if (is_string($this->availableValues)) {
108 3
                return $value == $this->availableValues;
109
            }
110 6
        }
111 6
        return true;
112
    }
113
114
    /**
115
     * Process environment variables like POST|GET|etc.. and return actual value
116
     *
117
     * @return mixed
118
     *
119
     * @throws Exception if actual InputParam has unsupported type
120
     */
121 27
    public function getValue()
0 ignored issues
show
Coding Style introduced by
getValue uses the super-global variable $_GET 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...
Coding Style introduced by
getValue uses the super-global variable $_POST 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...
Coding Style introduced by
getValue 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...
122
    {
123 27 View Code Duplication
        if ($this->type == self::TYPE_GET) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
124 12
            if (!filter_has_var(INPUT_GET, $this->key) && isset($_GET[$this->key])) {
125 6
                return $_GET[$this->key];
126
            }
127 6
            if ($this->isMulti()) {
128
                return filter_input(INPUT_GET, $this->key, FILTER_DEFAULT, FILTER_REQUIRE_ARRAY);
129
            }
130 6
            return filter_input(INPUT_GET, $this->key);
131
        }
132 24 View Code Duplication
        if ($this->type == self::TYPE_POST) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
133 12
            if (!filter_has_var(INPUT_POST, $this->key) && isset($_POST[$this->key])) {
134 6
                return $_POST[$this->key];
135
            }
136 9
            if ($this->isMulti()) {
137
                return filter_input(INPUT_POST, $this->key, FILTER_DEFAULT, FILTER_REQUIRE_ARRAY);
138
            }
139 9
            return filter_input(INPUT_POST, $this->key);
140
        }
141 12
        if ($this->type == self::TYPE_FILE) {
142 9
            if (isset($_FILES[$this->key])) {
143 6
                if ($this->isMulti()) {
144 3
                    return $this->processMultiFileUploads($_FILES[$this->key]);
145
                } else {
146 3
                    return $_FILES[$this->key];
147
                }
148
            }
149 3
            return null;
150
        }
151
152 3
        throw new Exception('Invalid type');
153
    }
154
155 3
    private function processMultiFileUploads($files)
156
    {
157 3
        $result = [];
158 3
        foreach ($files as $key => $values) {
159 3
            foreach ($values as $index => $value) {
160 3
                $result[$index][$key] = $value;
161 3
            }
162 3
        }
163 3
        return $result;
164
    }
165
}
166