Test Failed
Pull Request — master (#97)
by Gildonei
03:38
created

Psr7::requestToGlobal()   F

Complexity

Conditions 20
Paths > 20000

Size

Total Lines 88
Code Lines 71

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 420

Importance

Changes 5
Bugs 0 Features 1
Metric Value
eloc 71
c 5
b 0
f 1
dl 0
loc 88
ccs 0
cts 85
cp 0
rs 0
cc 20
nc 32256
nop 1
crap 420

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Ubiquity\utils\http\foundation;
4
5
/**
6
 * Gateway between php and PSR-7 Http messages.
7
 * Used with react and php-pm servers
8
 * Ubiquity\utils\http\foundation$Psr7
9
 * This class is part of Ubiquity
10
 *
11
 * @author jcheron <[email protected]>
12
 * @version 1.1.0
13
 *
14
 */
15
class Psr7 {
16
17
	public static function requestToGlobal(\Psr\Http\Message\ServerRequestInterface $request) {
18
		$method = $request->getMethod ();
19
		$uri = $request->getUri ();
20
		$parameters = $request->getQueryParams ();
21
		$parsedBody = $request->getParsedBody ();
22
		$headers = $request->getHeaders ();
23
24
		list ( $host, $port, $scheme, $userInfo, $path, $uQuery ) = [ $uri->getHost (),$uri->getPort (),$uri->getScheme (),$uri->getUserInfo (),$uri->getPath (),$uri->getQuery () ];
25
		$server = array_replace ( [
26
									'SERVER_NAME' => 'localhost',
27
									'SERVER_PORT' => 80,
28
									'HTTP_HOST' => 'localhost',
29
									'HTTP_USER_AGENT' => 'Ubiquity',
30
									'HTTP_ACCEPT' => 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
31
									'HTTP_ACCEPT_LANGUAGE' => 'en-us,en;q=0.5',
32
									'HTTP_ACCEPT_CHARSET' => 'ISO-8859-1,utf-8;q=0.7,*;q=0.7',
33
									'REMOTE_ADDR' => '127.0.0.1',
34
									'SCRIPT_NAME' => '',
35
									'SCRIPT_FILENAME' => '',
36
									'SERVER_PROTOCOL' => 'HTTP/1.1',
37
									'REQUEST_TIME' => time () ], $request->getServerParams () );
38
		$server ['PATH_INFO'] = '';
39
		$server ['REQUEST_METHOD'] = \strtoupper ( $method );
40
41
		if ($host != null) {
42
			$server ['SERVER_NAME'] = $host;
43
			$server ['HTTP_HOST'] = $host;
44
		}
45
		if ($scheme != null) {
46
			if ('https' === $scheme) {
47
				$server ['HTTPS'] = 'on';
48
				$server ['SERVER_PORT'] = 443;
49
			} else {
50
				unset ( $server ['HTTPS'] );
51
				$server ['SERVER_PORT'] = 80;
52
			}
53
		}
54
		if ($port != null) {
55
			$server ['SERVER_PORT'] = $port;
56
			$server ['HTTP_HOST'] .= ':' . $port;
57
		}
58
		if ($userInfo != null) {
59
			$userInfo = explode ( ':', $userInfo );
60
			$server ['PHP_AUTH_USER'] = $userInfo [0];
61
			if (\sizeof ( $userInfo ) > 1) {
62
				$server ['PHP_AUTH_PW'] = $userInfo [1];
63
			}
64
		}
65
		$path = $path ?? '/';
66
		switch (\strtoupper ( $method )) {
67
			case 'POST' :
68
			case 'PUT' :
69
			case 'DELETE' :
70
				if (! isset ( $server ['CONTENT_TYPE'] )) {
71
					$server ['CONTENT_TYPE'] = 'application/x-www-form-urlencoded';
72
				}
73
			// no break
74
			case 'PATCH' :
75
				$query = [ ];
76
				break;
77
			default :
78
				$query = $parameters;
79
				break;
80
		}
81
		$queryString = '';
82
		if ($uQuery != null) {
83
			parse_str ( \html_entity_decode ( $uQuery ), $qs );
84
			if ($query) {
85
				$query = \array_replace ( $qs, $query );
86
				$queryString = \http_build_query ( $query, '', '&' );
87
			} else {
88
				$queryString = $uQuery;
89
			}
90
		} elseif ($query) {
91
			$queryString = \http_build_query ( $query, '', '&' );
92
		}
93
		$server ['REQUEST_URI'] = $path . ('' !== $queryString ? '?' . $queryString : '');
94
		$server ['QUERY_STRING'] = $queryString;
95
		if (isset ( $headers ['X-Requested-With'] ) && array_search ( 'XMLHttpRequest', $headers ['X-Requested-With'] ) !== false) {
96
			$server ['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest';
97
		}
98
		if (strtoupper ( $method ) === 'POST') {
99
			$_POST = $parsedBody;
100
		}
101
		if (sizeof ( $parameters ) > 0) {
102
			$_GET = \array_merge ( $_GET, $parameters );
103
		}
104
		$_SERVER = $server;
105
		// To ADD $_FILES
106
	}
107
}
108
109