RequestParser::getServerKey()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 2
eloc 5
nc 2
nop 1
1
<?php
2
namespace CloudFramework\Tool;
3
4
final class RequestParser
5
{
6
    /**
7
     * Catch all request headers
8
     * @return array
9
     */
10
    public static function getRequestHeaders()
11
    {
12
        $headers = array();
13
        if(function_exists("getallheaders")) {
14
            foreach (getallheaders() as $key => $value) {
15
                $headers[strtolower($key)] = $value;
16
            }
17
        }
18
        return $headers;
19
    }
20
21
    /**
22
     * Catch all post data
23
     * @return array
24
     */
25
    public static function getPostParams()
0 ignored issues
show
Coding Style introduced by
getPostParams 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...
26
    {
27
        $data = array();
28
        try {
29
            $rawData = file_get_contents('php://input');
30
            $data = json_decode($rawData, JSON_UNESCAPED_UNICODE);
31
            if (null === $data) {
32
                parse_str($rawData, $data);
33
            }
34
        } catch (\Exception $t) {
35
            syslog(LOG_ERR, $t->getMessage());
36
        }
37
        $data = array_merge($data, $_POST);
38
        return $data;
39
    }
40
41
    /**
42
     * Catch query string data
43
     * @return array
44
     */
45
    public static function getQueryString()
0 ignored issues
show
Coding Style introduced by
getQueryString uses the super-global variable $_SERVER 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...
46
    {
47
        $query = array();
48
        try {
49
            parse_str($_SERVER['QUERY_STRING'], $query);
50
        } catch(\Exception $t) {
51
            syslog(LOG_ERR, $t->getMessage());
52
        }
53
        return $query;
54
    }
55
56
    /**
57
     * Gets post param
58
     * @param $key
59
     * @return mixed|null
60
     */
61
    public static function getPostParam($key)
62
    {
63
        $post = self::getPostParams();
64
        return array_key_exists($key, $post) ? $post[$key] : null;
65
    }
66
67
    /**
68
     * Get specific header
69
     * @param string $key
70
     * @return string|null
71
     */
72
    public static function getHeader($key)
73
    {
74
        $key = strtolower(trim($key));
75
        $headers = self::getRequestHeaders();
76
        return (array_key_exists($key, $headers)) ? $headers[$key] : null;
77
    }
78
79
    /**
80
     * Get query param
81
     * @param $key
82
     * @return array|null
83
     */
84
    public static function getQueryParam($key)
85
    {
86
        $query = self::getQueryString();
87
        return array_key_exists($key, $query) ? $query[$key] : null;
88
    }
89
90
    /**
91
     * Get $_SERVER value
92
     * @param string $key
93
     * @return null|mixed
94
     */
95
    public static function getServerKey($key)
0 ignored issues
show
Coding Style introduced by
getServerKey uses the super-global variable $_SERVER 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...
96
    {
97
        $value = null;
98
        if (array_key_exists($key, $_SERVER)) {
99
            $value = $_SERVER[$key];
100
        }
101
        return $value;
102
    }
103
104
    /**
105
     * Get $_FILES value
106
     * @param string $key
107
     * @return array
108
     */
109
    public static function getUploadedFile($key)
0 ignored issues
show
Coding Style introduced by
getUploadedFile 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...
110
    {
111
        $file = array();
112
        if (array_key_exists($key, $_FILES)) {
113
            $file = $_FILES[$key];
114
        }
115
        return $file;
116
    }
117
}