Passed
Push — master ( fa79ba...4fcbee )
by Aimeos
08:39 queued 05:58
created

Typo3::createRequest()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 9
nc 2
nop 5
dl 0
loc 16
rs 9.9666
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2015-2022
6
 * @package MW
7
 * @subpackage View
8
 */
9
10
11
namespace Aimeos\Base\View\Helper\Request;
12
13
14
/**
15
 * View helper class for accessing request data from Flow
16
 *
17
 * @package MW
18
 * @subpackage View
19
 */
20
class Typo3
21
	extends \Aimeos\Base\View\Helper\Request\Standard
22
	implements \Aimeos\Base\View\Helper\Request\Iface
23
{
24
	private $ip;
25
	private $target;
26
27
28
	/**
29
	 * Initializes the request view helper.
30
	 *
31
	 * @param \Aimeos\Base\View\Iface $view View instance with registered view helpers
32
	 * @param string|null TYPO3 target page ID
33
	 * @param array $files List of uploaded files like in $_FILES
34
	 * @param array $query List of uploaded files like in $_GET
35
	 * @param array $post List of uploaded files like in $_POST
36
	 * @param array $cookies List of uploaded files like in $_COOKIES
37
	 * @param array $server List of uploaded files like in $_SERVER
38
	 */
39
	public function __construct( \Aimeos\Base\View\Iface $view, string $target = null, array $files = [],
40
		array $query = [], array $post = [], array $cookies = [], array $server = [] )
41
	{
42
		$this->target = $target;
43
		$this->ip = ( isset( $server['REMOTE_ADDR'] ) ? $server['REMOTE_ADDR'] : null );
44
45
		$psr7request = $this->createRequest( $files, $query, $post, $cookies, $server );
46
47
		parent::__construct( $view, $psr7request );
48
	}
49
50
51
	/**
52
	 * Returns the client IP address.
53
	 *
54
	 * @return string Client IP address
55
	 */
56
	public function getClientAddress() : string
57
	{
58
		return $this->ip;
59
	}
60
61
62
	/**
63
	 * Returns the current page or route name
64
	 *
65
	 * @return string|null Current page ID
66
	 */
67
	public function getTarget() : ?string
68
	{
69
		return $this->target;
70
	}
71
72
73
	/**
74
	 * Creates a PSR-7 compatible request
75
	 *
76
	 * @param array $files List of uploaded files like in $_FILES
77
	 * @param array $query List of uploaded files like in $_GET
78
	 * @param array $post List of uploaded files like in $_POST
79
	 * @param array $cookies List of uploaded files like in $_COOKIES
80
	 * @param array $server List of uploaded files like in $_SERVER
81
	 * @return \Psr\Http\Message\ServerRequestInterface PSR-7 request object
82
	 */
83
	protected function createRequest( array $files, array $query, array $post, array $cookies, array $server ) : \Psr\Http\Message\ServerRequestInterface
84
	{
85
		if( !isset( $server['HTTP_HOST'] ) ) {
86
			$server['HTTP_HOST'] = 'localhost';
87
		}
88
89
		$psr17Factory = new \Nyholm\Psr7\Factory\Psr17Factory();
90
91
		$creator = new \Nyholm\Psr7Server\ServerRequestCreator(
92
			$psr17Factory, // ServerRequestFactory
93
			$psr17Factory, // UriFactory
94
			$psr17Factory, // UploadedFileFactory
95
			$psr17Factory  // StreamFactory
96
		);
97
98
		return $creator->fromGlobals();
99
	}
100
}
101