Completed
Push — master ( 54781a...616953 )
by Tomas
02:20
created

InputParam   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 108
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 5
Bugs 0 Features 2
Metric Value
wmc 22
c 5
b 0
f 2
lcom 1
cbo 0
dl 0
loc 108
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A getType() 0 4 1
A getKey() 0 4 1
A isRequired() 0 4 1
A getAvailableValues() 0 4 1
A isMulti() 0 4 1
B isValid() 0 19 7
C getValue() 0 22 9
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
    private $type;
17
18
    private $key;
19
20
    private $required;
21
22
    private $availableValues;
23
24
    private $multi;
25
26
    public function __construct($type, $key, $required = self::OPTIONAL, $availableValues = null, $multi = false)
27
    {
28
        $this->type = $type;
29
        $this->key = $key;
30
        $this->required = $required;
31
        $this->availableValues = $availableValues;
32
        $this->multi = $multi;
33
    }
34
35
    /**
36
     * @return string
37
     */
38
    public function getType()
39
    {
40
        return $this->type;
41
    }
42
43
    /**
44
     * @return string
45
     */
46
    public function getKey()
47
    {
48
        return $this->key;
49
    }
50
51
    /**
52
     * @return boolean
53
     */
54
    public function isRequired()
55
    {
56
        return $this->required;
57
    }
58
59
    public function getAvailableValues()
60
    {
61
        return $this->availableValues;
62
    }
63
64
    /**
65
     * @return bool
66
     */
67
    public function isMulti()
68
    {
69
        return $this->multi;
70
    }
71
72
    public function isValid()
73
    {
74
        $value = $this->getValue();
75
        if ($this->availableValues != null) {
76
            if (is_array($this->availableValues)) {
77
                return in_array($value, $this->availableValues);
78
            }
79
        }
80
81
        if ($this->required) {
82
            if ($value == null || $value = '') {
83
                return false;
84
            }
85
            if (is_string($this->availableValues)) {
86
                return $value == $this->availableValues;
87
            }
88
        }
89
        return true;
90
    }
91
92
    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...
93
    {
94
        if ($this->type == self::TYPE_GET) {
95
            if (!filter_has_var(INPUT_GET, $this->key) && isset($_GET[$this->key])) {
96
                return $_GET[$this->key];
97
            }
98
            return filter_input(INPUT_GET, $this->key);
99
        }
100
        if ($this->type == self::TYPE_POST) {
101
            if (!filter_has_var(INPUT_POST, $this->key) && isset($_POST[$this->key])) {
102
                return $_POST[$this->key];
103
            }
104
            return filter_input(INPUT_POST, $this->key);
105
        }
106
        if ($this->type == self::TYPE_FILE) {
107
           if (isset($_FILES[$this->key])) {
108
               return $_FILES[$this->key];
109
           }
110
        }
111
        
112
        throw new Exception("Invalid type");
113
    }
114
}
115