Completed
Push — master ( 0fee6d...0e1952 )
by Nazar
04:20
created

Psr7::from_psr7_files()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 10
rs 9.4285
cc 1
eloc 4
nc 1
nop 1
1
<?php
2
/**
3
 * @package   CleverStyle CMS
4
 * @author    Nazar Mokrynskyi <[email protected]>
5
 * @copyright Copyright (c) 2016, Nazar Mokrynskyi
6
 * @license   MIT License, see license.txt
7
 */
8
namespace cs\Request;
9
use
10
	Exception,
11
	cs\Config;
12
13
trait Psr7 {
14
	/**
15
	 * Initialize request from PSR-7 request object
16
	 *
17
	 * @param \Psr\Http\Message\ServerRequestInterface $request
18
	 *
19
	 * @throws \cs\ExitException
20
	 */
21
	function from_psr7 ($request) {
22
		/**
23
		 * @var \cs\Request $this
24
		 */
25
		$this->from_psr7_server($request);
0 ignored issues
show
Bug introduced by
The method from_psr7_server() cannot be called from this context as it is declared protected in class cs\Request\Psr7.

This check looks for access to methods that are not accessible from the current context.

If you need to make a method accessible to another context you can raise its visibility level in the defining class.

Loading history...
26
		$this->from_psr7_query($request);
0 ignored issues
show
Bug introduced by
The method from_psr7_query() cannot be called from this context as it is declared protected in class cs\Request\Psr7.

This check looks for access to methods that are not accessible from the current context.

If you need to make a method accessible to another context you can raise its visibility level in the defining class.

Loading history...
27
		$this->from_psr7_data($request);
0 ignored issues
show
Bug introduced by
The method from_psr7_data() cannot be called from this context as it is declared protected in class cs\Request\Psr7.

This check looks for access to methods that are not accessible from the current context.

If you need to make a method accessible to another context you can raise its visibility level in the defining class.

Loading history...
28
		$this->from_psr7_files($request);
0 ignored issues
show
Bug introduced by
The method from_psr7_files() cannot be called from this context as it is declared protected in class cs\Request\Psr7.

This check looks for access to methods that are not accessible from the current context.

If you need to make a method accessible to another context you can raise its visibility level in the defining class.

Loading history...
29
		$this->init_route();
30
	}
31
	/**
32
	 * @param \Psr\Http\Message\ServerRequestInterface $request
33
	 */
34
	protected function from_psr7_server ($request) {
35
		/**
36
		 * @var \cs\Request $this
37
		 */
38
		$uri          = $request->getUri();
39
		$this->method = $request->getMethod();
40
		$this->host   = $uri->getHost();
41
		$this->scheme = $uri->getScheme();
42
		$this->secure = $this->scheme == 'https';
43
		if (
44
			(!$this->secure && $uri->getPort() != 80) ||
45
			($this->secure && $uri->getPort() != 443)
46
		) {
47
			$this->host .= ':'.$uri->getPort();
48
		}
49
		$this->protocol     = 'HTTP/'.$request->getProtocolVersion();
50
		$this->path         = $uri->getPath();
51
		$this->query_string = $uri->getQuery();
52
		/** @noinspection NestedTernaryOperatorInspection */
53
		$this->uri         = $this->path.($this->query_string ? "?$this->query_string" : '') ?: '/';
54
		$this->remote_addr = @$request->getServerParams()['REMOTE_ADDR'] ?: '127.0.0.1';
55
		$this->ip          = $this->ip(
0 ignored issues
show
Bug introduced by
The method ip() cannot be called from this context as it is declared protected in class cs\Request\Server.

This check looks for access to methods that are not accessible from the current context.

If you need to make a method accessible to another context you can raise its visibility level in the defining class.

Loading history...
56
			[
57
				'HTTP_X_FORWARDED_FOR'     => $request->getHeaderLine('x-forwarded-for'),
58
				'HTTP_CLIENT_IP'           => $request->getHeaderLine('client-ip'),
59
				'HTTP_X_FORWARDED'         => $request->getHeaderLine('x-forwarded'),
60
				'HTTP_X_CLUSTER_CLIENT_IP' => $request->getHeaderLine('x-cluster-client-ip'),
61
				'HTTP_FORWARDED_FOR'       => $request->getHeaderLine('forwarded-for'),
62
				'HTTP_FORWARDED'           => $request->getHeaderLine('forwarded')
63
			]
64
		);
65
	}
66
	/**
67
	 * @param \Psr\Http\Message\ServerRequestInterface $request
68
	 */
69
	protected function from_psr7_query ($request) {
70
		/**
71
		 * @var \cs\Request $this
72
		 */
73
		$this->query = $request->getQueryParams();
74
	}
75
	/**
76
	 * @param \Psr\Http\Message\ServerRequestInterface $request
77
	 */
78
	protected function from_psr7_data ($request) {
79
		/**
80
		 * @var \cs\Request $this
81
		 */
82
		$data         = [];
83
		$data_stream  = null;
84
		$content_type = $this->header('content-type');
85
		$body_stream  = $request->getBody();
86
		if (preg_match('#^application/([^+\s]+\+)?json#', $content_type)) {
87
			$data = _json_decode((string)$body_stream) ?: [];
88
		} elseif (strpos($content_type, 'application/x-www-form-urlencoded') === 0) {
89
			@parse_str((string)$body_stream, $data);
90
		} else {
91
			try {
92
				$position = $body_stream->tell();
93
				$body_stream->rewind();
94
				$data_stream = fopen('php://temp', 'br+');
95
				while (!$body_stream->eof()) {
96
					fwrite($data_stream, $body_stream->read(1024));
97
				}
98
				$body_stream->seek($position);
99
			} catch (Exception $e) {
100
				// Do nothing
101
			}
102
		}
103
		$this->init_data($data, $data_stream);
104
	}
105
	/**
106
	 * @param \Psr\Http\Message\ServerRequestInterface $request
107
	 */
108
	protected function from_psr7_files ($request) {
109
		/**
110
		 * @var \cs\Request $this
111
		 */
112
		$this->init_files(
113
			$this->from_psr7_files_internal(
0 ignored issues
show
Bug introduced by
The method from_psr7_files_internal() cannot be called from this context as it is declared protected in class cs\Request\Psr7.

This check looks for access to methods that are not accessible from the current context.

If you need to make a method accessible to another context you can raise its visibility level in the defining class.

Loading history...
114
				$request->getUploadedFiles()
115
			)
116
		);
117
	}
118
	/**
119
	 * @param array|\Psr\Http\Message\UploadedFileInterface $files
120
	 *
121
	 * @return array|\Psr\Http\Message\UploadedFileInterface
122
	 */
123
	protected function from_psr7_files_internal ($files) {
124
		if (is_array($files)) {
125
			foreach ($files as $field => &$file) {
126
				$file = $this->from_psr7_files_internal($file);
127
				if (!$file) {
128
					unset($files[$field]);
129
				}
130
			}
131
			return $files;
132
		}
133
		try {
134
			$source_file_stream = $files->getStream();
135
			$position           = $source_file_stream->tell();
136
			$source_file_stream->rewind();
137
			$file_stream = fopen('php://temp', 'br+');
138
			while (!$source_file_stream->eof()) {
139
				fwrite($file_stream, $source_file_stream->read(1024));
140
			}
141
			$source_file_stream->seek($position);
142
		} catch (Exception $e) {
143
			return [];
144
		}
145
		return [
146
			'name'   => $files->getClientFilename(),
147
			'type'   => $files->getClientMediaType(),
148
			'size'   => $files->getSize(),
149
			'stream' => $file_stream,
150
			'error'  => $files->getError()
151
		];
152
	}
153
	/**
154
	 * @todo Implementation needed
155
	 *
156
	 * @param \Psr\Http\Message\ServerRequestInterface $request
157
	 *
158
	 * @return \Psr\Http\Message\ServerRequestInterface
159
	 */
160
	protected function to_psr7 ($request) {
161
		return $request;
162
	}
163
}
164