Completed
Push — master ( 1b0476...709623 )
by Aimeos
04:54
created

Laravel5::getFiles()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 24
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 24
rs 8.9713
cc 3
eloc 13
nc 3
nop 1
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2015-2016
6
 * @package MW
7
 * @subpackage View
8
 */
9
10
11
namespace Aimeos\MW\View\Helper\Request;
12
13
14
/**
15
 * View helper class for retrieving request data.
16
 *
17
 * @package MW
18
 * @subpackage View
19
 */
20
class Laravel5
21
	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...
22
	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...
23
{
24
	private $request;
25
26
27
	/**
28
	 * Initializes the request view helper.
29
	 *
30
	 * @param \\Aimeos\MW\View\Iface $view View instance with registered view helpers
31
	 * @param \Illuminate\Http\Request $request Laravel request object
32
	 */
33
	public function __construct( \Aimeos\MW\View\Iface $view, \Illuminate\Http\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...
34
	{
35
		$this->request = $request;
36
37
		parent::__construct( $view, $this->createRequest( $request ) );
38
	}
39
40
41
	/**
42
	 * Returns the client IP address.
43
	 *
44
	 * @return string Client IP address
45
	 */
46
	public function getClientAddress()
47
	{
48
		return $this->request->ip();
49
	}
50
51
52
	/**
53
	 * Returns the current page or route name
54
	 *
55
	 * @return string|null Current page or route name
56
	 */
57
	public function getTarget()
58
	{
59
		if( ( $route = $this->request->route() ) !== null ) {
60
			return $route->getName();
61
		}
62
	}
63
64
65
	/**
66
	 * Transforms a Symfony request into a PSR-7 request object
67
	 *
68
	 * @param \Illuminate\Http\Request $nativeRequest Laravel request object
69
	 * @return \Psr\Http\Message\ServerRequestInterface PSR-7 request object
70
	 */
71
	protected function createRequest( \Illuminate\Http\Request $nativeRequest )
72
	{
73
		$files = ServerRequestFactory::normalizeFiles( $this->getFiles( $nativeRequest->files->all() ) );
74
		$server = ServerRequestFactory::normalizeServer( $nativeRequest->server->all() );
75
		$headers = $nativeRequest->headers->all();
76
		$cookies = $nativeRequest->cookies->all();
77
		$post = $nativeRequest->request->all();
78
		$query = $nativeRequest->query->all();
79
		$method = $nativeRequest->getMethod();
80
		$uri = $nativeRequest->getUri();
81
82
		$body = new Stream( 'php://temp', 'wb+' );
83
		$body->write( $nativeRequest->getContent() );
84
85
		$request = new ServerRequest( $server, $files, $uri, $method, $body, $headers, $cookies, $query, $post );
86
87
		foreach( $nativeRequest->attributes->all() as $key => $value ) {
88
			$request = $request->withAttribute( $key, $value );
89
		}
90
91
		return $request;
92
	}
93
94
95
	/**
96
	 * Converts Symfony uploaded files array to the PSR-7 one.
97
	 *
98
	 * @param array $files Multi-dimensional list of uploaded files from Symfony request
99
	 * @return array Multi-dimensional list of uploaded files as PSR-7 objects
100
	 */
101
	protected function getFiles( array $files )
102
	{
103
		$list = array();
104
105
		foreach( $files as $key => $value )
106
		{
107
			if( $value instanceof \Illuminate\Http\UploadedFile )
108
			{
109
				$list[$key] = new \Zend\Diactoros\UploadedFile(
110
					$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...
111
					$file->getSize(),
112
					$file->getError(),
113
					$file->getClientOriginalName(),
114
					$file->getClientMimeType()
115
				);
116
			}
117
			else
118
			{
119
				$list[$key] = $this->getFiles( $value );
120
			}
121
		}
122
123
		return $list;
124
	}
125
}
126