Completed
Push — master ( 0492d9...7c4761 )
by Nazar
04:01
created

Psr7::from_psr7_data_and_files()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 3
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
10
trait Psr7 {
11
	/**
12
	 * Initialize request from PSR-7 request object
13
	 *
14
	 * @param \Psr\Http\Message\ServerRequestInterface $request
15
	 *
16
	 * @throws \cs\ExitException
17
	 */
18
	function from_psr7 ($request) {
19
		$this->from_psr7_server($request);
20
		$this->from_psr7_query($request);
21
		$this->from_psr7_data_and_files($request);
22
		$this->init_route();
23
	}
24
	/**
25
	 * @param \Psr\Http\Message\ServerRequestInterface $request
26
	 */
27
	protected function from_psr7_server ($request) {
28
		$uri          = $request->getUri();
29
		$this->method = $request->getMethod();
30
		$this->host   = $uri->getHost();
31
		$this->scheme = $uri->getScheme();
32
		$this->secure = $this->scheme == 'https';
33
		if (
34
			(!$this->secure && $uri->getPort() != 80) ||
35
			($this->secure && $uri->getPort() != 443)
36
		) {
37
			$this->host .= ':'.$uri->getPort();
38
		}
39
		$this->protocol     = 'HTTP/'.$request->getProtocolVersion();
40
		$this->path         = $uri->getPath();
41
		$this->query_string = $uri->getQuery();
42
		/** @noinspection NestedTernaryOperatorInspection */
43
		$this->uri         = $this->path.($this->query_string ? "?$this->query_string" : '') ?: '/';
44
		$this->remote_addr = @$request->getServerParams()['REMOTE_ADDR'] ?: '127.0.0.1';
45
		$this->ip          = $this->ip(
46
			[
47
				'HTTP_X_FORWARDED_FOR'     => $request->getHeaderLine('x-forwarded-for'),
48
				'HTTP_CLIENT_IP'           => $request->getHeaderLine('client-ip'),
49
				'HTTP_X_FORWARDED'         => $request->getHeaderLine('x-forwarded'),
50
				'HTTP_X_CLUSTER_CLIENT_IP' => $request->getHeaderLine('x-cluster-client-ip'),
51
				'HTTP_FORWARDED_FOR'       => $request->getHeaderLine('forwarded-for'),
52
				'HTTP_FORWARDED'           => $request->getHeaderLine('forwarded')
53
			]
54
		);
55
	}
56
	/**
57
	 * @param \Psr\Http\Message\ServerRequestInterface $request
58
	 */
59
	protected function from_psr7_query ($request) {
60
		$this->query = $request->getQueryParams();
61
	}
62
	/**
63
	 * @param \Psr\Http\Message\ServerRequestInterface $request
64
	 *
65
	 * @throws \cs\ExitException
66
	 */
67
	protected function from_psr7_data_and_files ($request) {
68
		Psr7_data_stream::$stream = $request->getBody();
69
		$this->init_data_and_files([], [], fopen('request-psr7-data://', 'r'));
70
	}
71
}
72