Completed
Pull Request — master (#25)
by Michal
15:14 queued 12:43
created

InputParam::getKey()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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