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