Completed
Push — master ( a36da0...f71e62 )
by Tomas
11s
created

InputParam::processMultiFileUploads()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 1
cts 1
cp 1
rs 9.4285
c 0
b 0
f 0
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_PUT       = 'PUT';
12
    const TYPE_FILE      = 'FILE';
13
    const TYPE_COOKIE    = 'COOKIE';
14
    const TYPE_POST_RAW  = 'POST_RAW';
15
16
    const OPTIONAL = false;
17
    const REQUIRED = true;
18
19
    /**
20
     * @var string
21
     */
22
    private $type;
23
24
    /**
25
     * @var string
26
     */
27
    private $key;
28
29
    /**
30
     * @var bool
31
     */
32
    private $required;
33
34
    /**
35
     * @var array|null
36
     */
37
    private $availableValues;
38
39
    /**
40
     * @var bool
41
     */
42
    private $multi;
43 45
44
    public function __construct($type, $key, $required = self::OPTIONAL, $availableValues = null, $multi = false)
45 45
    {
46 45
        $this->type = $type;
47 45
        $this->key = $key;
48 45
        $this->required = $required;
49 45
        $this->availableValues = $availableValues;
50 45
        $this->multi = $multi;
51
    }
52
53
    /**
54
     * @return string
55 9
     */
56
    public function getType()
57 9
    {
58
        return $this->type;
59
    }
60
61
    /**
62
     * @return string
63 21
     */
64
    public function getKey()
65 21
    {
66
        return $this->key;
67
    }
68
69
    /**
70
     * @return boolean
71 6
     */
72
    public function isRequired()
73 6
    {
74
        return $this->required;
75
    }
76 6
77
    public function getAvailableValues()
78 6
    {
79
        return $this->availableValues;
80
    }
81
82
    /**
83
     * @return bool
84 21
     */
85
    public function isMulti()
86 21
    {
87
        return $this->multi;
88
    }
89
90
    /**
91
     * Check if actual value from environment is valid
92
     *
93
     * @return bool
94
     *
95
     * @throws Exception if actual InputParam has unsupported type
96 18
     */
97
    public function isValid()
98 18
    {
99 6
        if ($this->required == self::OPTIONAL) {
100
            return true;
101
        }
102 18
103 18
        $value = $this->getValue();
104 9
        if ($this->availableValues !== null) {
105 6
            if (is_array($this->availableValues)) {
106
                return empty(array_diff(($this->isMulti() ? $value : [$value]), $this->availableValues));
107 2
            }
108
        }
109 15
110 15
        if ($this->required) {
111 6
            if ($value === null || $value == '') {
112
                return false;
113 9
            }
114 3
            if (is_string($this->availableValues)) {
115
                return $value == $this->availableValues;
116 4
            }
117 6
        }
118
        return true;
119
    }
120
121
    /**
122
     * Process environment variables like POST|GET|etc.. and return actual value
123
     *
124
     * @return mixed
125
     *
126
     * @throws Exception if actual InputParam has unsupported type
127 36
     */
128
    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...
Coding Style introduced by
getValue uses the super-global variable $_COOKIE 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...
129 36
    {
130 15 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...
131 6
            if (!filter_has_var(INPUT_GET, $this->key) && isset($_GET[$this->key])) {
132
                return $_GET[$this->key];
133 9
            }
134
            if ($this->isMulti()) {
135
                return filter_input(INPUT_GET, $this->key, FILTER_DEFAULT, FILTER_REQUIRE_ARRAY);
136 9
            }
137
            return filter_input(INPUT_GET, $this->key);
138 30
        }
139 12 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...
140 6
            if (!filter_has_var(INPUT_POST, $this->key) && isset($_POST[$this->key])) {
141
                return $_POST[$this->key];
142 9
            }
143
            if ($this->isMulti()) {
144
                return filter_input(INPUT_POST, $this->key, FILTER_DEFAULT, FILTER_REQUIRE_ARRAY);
145 9
            }
146
            return filter_input(INPUT_POST, $this->key);
147 18
        }
148 9
        if ($this->type == self::TYPE_FILE) {
149 6
            if (isset($_FILES[$this->key])) {
150 3
                if ($this->isMulti()) {
151
                    return $this->processMultiFileUploads($_FILES[$this->key]);
152 3
                } else {
153
                    return $_FILES[$this->key];
154
                }
155 3
            }
156
            return null;
157 9
        }
158 3
        if ($this->type == self::TYPE_COOKIE) {
159 3
            if (isset($_COOKIE[$this->key])) {
160
                return $_COOKIE[$this->key];
161
            }
162 6
        }
163 3
        if ($this->type == self::TYPE_POST_RAW) {
164
            return file_get_contents("php://input");
165
        }
166 3
        if ($this->type == self::TYPE_PUT) {
167
            parse_str(file_get_contents("php://input"), $params);
168
            if (isset($params[$this->key])) {
169 3
                return $params[$this->key];
170
            }
171 3
            return '';
172 3
        }
173 3
174 3
        throw new Exception('Invalid type');
175 2
    }
176 2
177 3
    private function processMultiFileUploads($files)
178
    {
179
        $result = [];
180
        foreach ($files as $key => $values) {
181
            foreach ($values as $index => $value) {
182
                $result[$index][$key] = $value;
183
            }
184
        }
185
        return $result;
186
    }
187
}
188