Completed
Push — master ( 90e53b...61613f )
by Fran
03:01
created

RequestParser::getQueryParam()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
ccs 0
cts 3
cp 0
rs 9.4286
cc 2
eloc 3
nc 2
nop 1
crap 6
1
<?php
2
namespace CloudFramework\Core\Tool;
3
4
use CloudFramework\Helpers\MagicClass;
5
use CloudFramework\Patterns\Singleton;
6
7
final class RequestParser
8
{
9
    /**
10
     * Catch all request headers
11
     * @return array
12
     */
13
    public static function getRequestHeaders()
14
    {
15
        $headers = array();
16
        if(function_exists("getallheaders")) {
17
            foreach (getallheaders() as $key => $value) {
18
                $headers[strtolower($key)] = $value;
19
            }
20
        }
21
        return $headers;
22
    }
23
24
    /**
25
     * Catch all post data
26
     * @return array
27
     */
28
    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...
29
    {
30
        $data = array();
31
        try {
32
            $rawData = file_get_contents('php://input');
33
            $data = json_decode($rawData, JSON_UNESCAPED_UNICODE);
34
            if (null === $data) {
35
                parse_str($rawData, $data);
36
            }
37
        } catch (\Throwable $t) {
0 ignored issues
show
Bug introduced by
The class Throwable does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
38
            syslog(LOG_ERR, $t->getMessage());
39
        }
40
        $data = array_merge($data, $_POST);
41
        return $data;
42
    }
43
44
    /**
45
     * Catch query string data
46
     * @return array
47
     */
48
    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...
49
    {
50
        $query = array();
51
        try {
52
            parse_str($_SERVER['QUERY_STRING'], $query);
53
        } catch(\Throwable $t) {
0 ignored issues
show
Bug introduced by
The class Throwable does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
54
            syslog(LOG_ERR, $t->getMessage());
55
        }
56
        return $query;
57
    }
58
59
    /**
60
     * Gets post param
61
     * @param $key
62
     * @return mixed|null
63
     */
64
    public static function getPostParam($key)
65
    {
66
        $post = self::getPostParams();
67
        return array_key_exists($key, $post) ? $post[$key] : null;
68
    }
69
70
    /**
71
     * Get specific header
72
     * @param string $key
73
     * @return string|null
74
     */
75
    public static function getHeader($key)
76
    {
77
        $key = strtolower(trim($key));
78
        $headers = self::getRequestHeaders();
79
        return (array_key_exists($key, $headers)) ? $headers[$key] : null;
80
    }
81
82
    /**
83
     * Get query param
84
     * @param $key
85
     * @return array|null
86
     */
87
    public static function getQueryParam($key)
88
    {
89
        $query = self::getQueryString();
90
        return array_key_exists($key, $query) ? $query[$key] : null;
91
    }
92
93
    /**
94
     * Get $_SERVER value
95
     * @param string $key
96
     * @return null|mixed
97
     */
98
    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...
99
    {
100
        $value = null;
101
        if (array_key_exists($key, $_SERVER)) {
102
            $value = $_SERVER[$key];
103
        }
104
        return $value;
105
    }
106
107
    /**
108
     * Get $_FILES value
109
     * @param string $key
110
     * @return array
111
     */
112
    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...
113
    {
114
        $file = array();
115
        if (array_key_exists($key, $_FILES)) {
116
            $file = $_FILES[$key];
117
        }
118
        return $file;
119
    }
120
}