Passed
Push — master ( e8f5c9...c45c7a )
by refat
06:21 queued 02:17
created

Request   A

Complexity

Total Complexity 29

Size/Duplication

Total Lines 159
Duplicated Lines 0 %

Importance

Changes 5
Bugs 0 Features 0
Metric Value
eloc 53
c 5
b 0
f 0
dl 0
loc 159
rs 10
wmc 29

16 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A get() 0 14 2
A posts() 0 3 1
A link() 0 3 1
A file() 0 12 2
A referer() 0 3 1
A files() 0 3 1
A setPost() 0 3 1
A prepareUrl() 0 18 3
A url() 0 3 1
A baseUrl() 0 3 1
A server() 0 3 1
A post() 0 14 2
B isSecure() 0 12 7
A method() 0 3 1
A cleanUrl() 0 17 3
1
<?php
2
3
namespace System\Http;
4
5
use System\Application;
6
7
class Request
8
{
9
  private $app;
10
11
  private $url;
12
13
  private $baseUrl;
14
15
  private $files = [];
16
17
  private $link;
18
19
  public function __construct(Application $app)
20
  {
21
    $this->app = $app;
22
  }
23
24
  public function prepareUrl()
25
  {
26
    $script = dirname($this->server('SCRIPT_NAME'));
27
28
    $requestUri = $this->server('REQUEST_URI');
29
30
    if (strpos($requestUri, '?')) {
31
32
      list($requestUri, $queryString) = explode('?', $requestUri);
33
    }
34
35
    $this->url = $this->cleanUrl($script, $requestUri);
36
37
    $REQUEST_PROTOCOL = $this->isSecure() ? 'https' : 'http';
38
39
    $this->link = $REQUEST_PROTOCOL . '://' . $this->server('HTTP_HOST');
40
41
    $this->baseUrl = $this->link . $requestUri;
42
  }
43
44
  private function cleanUrl($script, $requestUri)
45
  {
46
    if (!in_array($script, ['/', '\\'])) {
47
48
      $url = preg_replace('#^' . $script . '#', '', $requestUri);
49
50
    } else {
51
52
      $url =  $requestUri;
53
    }
54
55
    if ($url !== '/') {
56
57
      $url = rtrim($url, '/');
58
    }
59
60
    return $url;
61
  }
62
63
  private function isSecure()
64
  {
65
    if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') {
66
67
      return true;
68
69
    } elseif ((!empty($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https') || (!empty($_SERVER['HTTP_X_FORWARDED_SSL']) && $_SERVER['HTTP_X_FORWARDED_SSL'] == 'on')) {
70
71
      return true;
72
    }
73
74
    return false;
75
  }
76
77
  public function get($key)
78
  {
79
    $value = array_get($_GET, $key);
80
81
    if (is_array($value)) {
82
83
      $value = array_filter($value);
84
85
    } else {
86
87
      $value = trim($value);
88
    }
89
90
    return $value;
91
  }
92
93
  public function post($key)
94
  {
95
    $value = array_get($_POST, $key);
96
97
    if (is_array($value)) {
98
99
      $value = array_filter($value);
100
101
    } else {
102
103
      $value = trim($value);
104
    }
105
106
    return $value;
107
  }
108
109
  public function setPost($key, $value)
110
  {
111
    $_POST[$key] = $value;
112
  }
113
114
  public function posts()
115
  {
116
    return $_POST;
117
  }
118
119
  public function files()
120
  {
121
    return $_FILES;
122
  }
123
124
  public function file($input)
125
  {
126
    if (isset($this->files[$input])) {
127
128
      return $this->files[$input];
129
    }
130
131
    $upoadedFile = new UploadeFile($this->app, $input);
132
133
    $this->files[$input] = $upoadedFile;
134
135
    return $this->files[$input];
136
  }
137
138
  public function server($key)
139
  {
140
    return array_get($_SERVER, $key);
141
  }
142
143
  public function method()
144
  {
145
    return $this->server('REQUEST_METHOD');
146
  }
147
148
  public function referer()
149
  {
150
    return $this->server('HTTP_REFERER');
151
  }
152
153
  public function baseUrl()
154
  {
155
    return $this->baseUrl;
156
  }
157
158
  public function url()
159
  {
160
    return $this->url;
161
  }
162
163
  public function link()
164
  {
165
    return $this->link;
166
  }
167
}