Typo3::createRequest()   A
last analyzed

Complexity

Conditions 6
Paths 16

Size

Total Lines 30
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 17
c 1
b 0
f 0
dl 0
loc 30
rs 9.0777
cc 6
nc 16
nop 5
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2015-2025
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 ?string $target;
25
	private ?string $ip;
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
		$psr17Factory = new \Nyholm\Psr7\Factory\Psr17Factory();
86
87
		$creator = new \Nyholm\Psr7Server\ServerRequestCreator(
88
			$psr17Factory, // ServerRequestFactory
89
			$psr17Factory, // UriFactory
90
			$psr17Factory, // UploadedFileFactory
91
			$psr17Factory  // StreamFactory
92
		);
93
94
		if( !isset( $server['HTTP_HOST'] ) ) {
95
			$server['HTTP_HOST'] = 'localhost';
96
		}
97
98
		if( !isset( $server['REQUEST_METHOD'] ) ) {
99
			$server['REQUEST_METHOD'] = 'GET';
100
		}
101
102
		$headers = function_exists( 'getallheaders' ) ? getallheaders() : $creator::getHeadersFromServer( $server );
103
		$body = fopen( 'php://input', 'r' ) ?: null;
104
105
		$request = $creator->fromArrays( $server, $headers, $cookies, $query, $post, $files, $body );
0 ignored issues
show
Bug introduced by
It seems like $headers can also be of type true; however, parameter $headers of Nyholm\Psr7Server\Server...stCreator::fromArrays() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

105
		$request = $creator->fromArrays( $server, /** @scrutinizer ignore-type */ $headers, $cookies, $query, $post, $files, $body );
Loading history...
106
		$psr7files = $request->getUploadedFiles();
107
108
		if( isset( $psr7files['ai'] ) ) {
109
			$request = $request->withUploadedFiles( $psr7files['ai'] );
110
		}
111
112
		return $request;
113
	}
114
}
115