Completed
Push — master ( 089cf3...6aa8c0 )
by Tomas
04:05
created

InputParam::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

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