Completed
Push — master ( 9ea9f1...27c34d )
by Restu
13:04
created

Request::rootURL()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
namespace JayaCode\Framework\Core\Http;
3
4
use Symfony\Component\HttpFoundation\Request as BaseRequest;
5
6
class Request extends BaseRequest
7
{
8
9
    /**
10
     * Get the current path info for the request.
11
     *
12
     * @return string
13
     */
14
    public function path()
15
    {
16
        $pattern = trim($this->getPathInfo(), '/');
17
        return $pattern == '' ? '/' : $pattern;
18
    }
19
20
    /**
21
     * Get the request method.
22
     *
23
     * @return string
24
     */
25
    public function method()
26
    {
27
        return $this->getMethod();
28
    }
29
30
    /**
31
     * Get the root URL
32
     *
33
     * @return string
34
     */
35
    public function rootURL()
36
    {
37
        return rtrim($this->getSchemeAndHttpHost().$this->getBaseUrl(), '/');
38
    }
39
40
    /**
41
     * Return true if server HTTP_REFERER isset
42
     *
43
     * @return bool
44
     */
45
    public function hasRefererURL()
46
    {
47
        return $this->server->has("HTTP_REFERER");
48
    }
49
50
    /**
51
     * Return server HTTP_REFERER
52
     *
53
     * @return string
54
     */
55
    public function refererURL()
56
    {
57
        return $this->server->get("HTTP_REFERER");
58
    }
59
60
    /**
61
     * Return IP address client
62
     *
63
     * @return string
64
     */
65
    public function ip()
66
    {
67
        return $this->getClientIp();
68
    }
69
70
    /**
71
     * Return IP address client
72
     *
73
     * @return array
74
     */
75
    public function ipAll()
76
    {
77
        return $this->getClientIps();
78
    }
79
80
    /**
81
     * @param array $query
82
     * @param array $request
83
     * @param array $attributes
84
     * @param array $cookies
85
     * @param array $files
86
     * @param array $server
87
     * @param null $content
88
     * @return array|mixed|static
89
     */
90
    private static function createRequestFromFactory(
0 ignored issues
show
Bug introduced by
Consider using a different method name as you override a private method of the parent class.

Overwriting private methods is generally fine as long as you also use private visibility. It might still be preferable for understandability to use a different method name.

Loading history...
91
        array $query = array(),
92
        array $request = array(),
93
        array $attributes = array(),
94
        array $cookies = array(),
95
        array $files = array(),
96
        array $server = array(),
97
        $content = null
98
    ) {
99
    
100
        if (self::$requestFactory) {
101
            $request = call_user_func(
102
                self::$requestFactory,
103
                $query,
104
                $request,
105
                $attributes,
106
                $cookies,
107
                $files,
108
                $server,
109
                $content
110
            );
111
112
            if (!$request instanceof self) {
113
                $message = 'The Request factory must return an instance of Symfony\Component\HttpFoundation\Request.';
114
                throw new \LogicException($message);
115
            }
116
117
            return $request;
118
        }
119
120
        return new static($query, $request, $attributes, $cookies, $files, $server, $content);
121
    }
122
123
    /**
124
     * Creates a new request with values from PHP's super globals.
125
     *
126
     * @return Request A new request
127
     */
128
    public static function createFromGlobals()
0 ignored issues
show
Coding Style introduced by
createFromGlobals 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...
Coding Style introduced by
createFromGlobals 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
createFromGlobals 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
createFromGlobals 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...
Coding Style introduced by
createFromGlobals 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...
129
    {
130
        // With the php's bug #66606, the php's built-in web server
131
        // stores the Content-Type and Content-Length header values in
132
        // HTTP_CONTENT_TYPE and HTTP_CONTENT_LENGTH fields.
133
        $server = $_SERVER;
134
        if ('cli-server' === PHP_SAPI) {
135
            if (array_key_exists('HTTP_CONTENT_LENGTH', $_SERVER)) {
136
                $server['CONTENT_LENGTH'] = $_SERVER['HTTP_CONTENT_LENGTH'];
137
            }
138
            if (array_key_exists('HTTP_CONTENT_TYPE', $_SERVER)) {
139
                $server['CONTENT_TYPE'] = $_SERVER['HTTP_CONTENT_TYPE'];
140
            }
141
        }
142
143
        $request = self::createRequestFromFactory($_GET, $_POST, array(), $_COOKIE, $_FILES, $server);
144
145
        if (0 === strpos($request->headers->get('CONTENT_TYPE'), 'application/x-www-form-urlencoded')
146
            && in_array(strtoupper($request->server->get('REQUEST_METHOD', 'GET')), array('PUT', 'DELETE', 'PATCH'))
147
        ) {
148
            parse_str($request->getContent(), $data);
149
            $request->request = new ParameterBag($data);
0 ignored issues
show
Documentation Bug introduced by
It seems like new \JayaCode\Framework\...ttp\ParameterBag($data) of type object<JayaCode\Framework\Core\Http\ParameterBag> is incompatible with the declared type object<Symfony\Component...oundation\ParameterBag> of property $request.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
150
        }
151
152
        return $request;
153
    }
154
}
155