Completed
Push — master ( 363df4...472eac )
by Aimeos
05:28
created

Symfony2::createRequest()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 22
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 22
rs 9.2
cc 2
eloc 15
nc 2
nop 1
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2014-2016
6
 * @package MW
7
 * @subpackage View
8
 */
9
10
11
namespace Aimeos\MW\View\Helper\Request;
12
13
use Zend\Diactoros\ServerRequestFactory;
14
use Zend\Diactoros\ServerRequest;
15
use Zend\Diactoros\Stream;
16
17
18
/**
19
 * View helper class for retrieving data from Symfony2 requests.
20
 *
21
 * @package MW
22
 * @subpackage View
23
 */
24
class Symfony2
25
	extends \Aimeos\MW\View\Helper\Request\Standard
0 ignored issues
show
Coding Style introduced by
The extends keyword must be on the same line as the class name
Loading history...
Coding Style introduced by
Expected 0 spaces between "Standard" and comma; 1 found
Loading history...
26
	implements \Aimeos\MW\View\Helper\Request\Iface
0 ignored issues
show
Coding Style introduced by
The implements keyword must be on the same line as the class name
Loading history...
27
{
28
	private $request;
29
30
31
	/**
32
	 * Initializes the URL view helper.
33
	 *
34
	 * @param \Aimeos\MW\View\Iface $view View instance with registered view helpers
35
	 * @param \Symfony\Component\HttpFoundation\Request $request Symfony2 request object
36
	 */
37
	public function __construct( $view, \Symfony\Component\HttpFoundation\Request $request )
0 ignored issues
show
Bug introduced by
You have injected the Request via parameter $request. This is generally not recommended as there might be multiple instances during a request cycle (f.e. when using sub-requests). Instead, it is recommended to inject the RequestStack and retrieve the current request each time you need it via getCurrentRequest().
Loading history...
38
	{
39
		$this->request = $request;
40
41
		parent::__construct( $view, $this->createRequest( $request ) );
42
	}
43
44
45
	/**
46
	 * Returns the client IP address.
47
	 *
48
	 * @return string Client IP address
49
	 */
50
	public function getClientAddress()
51
	{
52
		return $this->request->getClientIp();
53
	}
54
55
56
	/**
57
	 * Returns the current page or route name
58
	 *
59
	 * @return string|null Current page or route name
60
	 */
61
	public function getTarget()
62
	{
63
		return $this->request->get( '_route' );
64
	}
65
66
67
	/**
68
	 * Transforms a Symfony request into a PSR-7 request object
69
	 *
70
	 * @param \Symfony\Component\HttpFoundation\Request $nativeRequest Symfony request object
71
	 * @return \Psr\Http\Message\ServerRequestInterface PSR-7 request object
72
	 */
73
	protected function createRequest( \Symfony\Component\HttpFoundation\Request $nativeRequest )
74
	{
75
		$files = ServerRequestFactory::normalizeFiles( $this->getFiles( $nativeRequest->files->all() ) );
76
		$server = ServerRequestFactory::normalizeServer( $nativeRequest->server->all() );
77
		$headers = $nativeRequest->headers->all();
78
		$cookies = $nativeRequest->cookies->all();
79
		$post = $nativeRequest->request->all();
80
		$query = $nativeRequest->query->all();
81
		$method = $nativeRequest->getMethod();
82
		$uri = $nativeRequest->getUri();
83
84
		$body = new Stream( 'php://temp', 'wb+' );
85
		$body->write( $nativeRequest->getContent() );
0 ignored issues
show
Bug introduced by
It seems like $nativeRequest->getContent() targeting Symfony\Component\HttpFo...n\Request::getContent() can also be of type resource; however, Zend\Diactoros\Stream::write() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
86
87
		$request = new ServerRequest( $server, $files, $uri, $method, $body, $headers, $cookies, $query, $post );
88
89
		foreach( $nativeRequest->attributes->all() as $key => $value ) {
90
			$request = $request->withAttribute( $key, $value );
91
		}
92
93
		return $request;
94
	}
95
96
97
	/**
98
	 * Converts Symfony uploaded files array to the PSR-7 one.
99
	 *
100
	 * @param array $files Multi-dimensional list of uploaded files from Symfony request
101
	 * @return array Multi-dimensional list of uploaded files as PSR-7 objects
102
	 */
103
	protected function getFiles( array $files )
104
	{
105
		$list = array();
106
107
		foreach( $files as $key => $value )
108
		{
109
			if( $value instanceof \Symfony\Component\HttpFoundation\File\UploadedFile )
110
			{
111
				$list[$key] = new \Zend\Diactoros\UploadedFile(
112
					$file->getRealPath(),
0 ignored issues
show
Bug introduced by
The variable $file does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
113
					$file->getSize(),
114
					$file->getError(),
115
					$file->getClientOriginalName(),
116
					$file->getClientMimeType()
117
				);
118
			}
119
			else
120
			{
121
				$list[$key] = $this->getFiles( $value );
122
			}
123
		}
124
125
		return $list;
126
	}
127
}
128