Completed
Push — master ( 94b58b...843436 )
by Aimeos
02:17
created

Laravel5::getFiles()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 24
rs 9.536
c 0
b 0
f 0
cc 4
nc 4
nop 1
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2015-2017
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 request data.
20
 *
21
 * @package MW
22
 * @subpackage View
23
 */
24
class Laravel5
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;
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
29
30
31
	/**
32
	 * Initializes the request view helper.
33
	 *
34
	 * @param \\Aimeos\MW\View\Iface $view View instance with registered view helpers
35
	 * @param \Illuminate\Http\Request $request Laravel request object
36
	 */
37
	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...
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->ip();
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
		if( ( $route = $this->request->route() ) !== null ) {
64
			return $route->getName();
65
		}
66
	}
67
68
69
	/**
70
	 * Transforms a Symfony request into a PSR-7 request object
71
	 *
72
	 * @param \Illuminate\Http\Request $nativeRequest Laravel request object
73
	 * @return \Psr\Http\Message\ServerRequestInterface PSR-7 request object
74
	 */
75
	protected function createRequest( \Illuminate\Http\Request $nativeRequest )
76
	{
77
		$files = ServerRequestFactory::normalizeFiles( $this->getFiles( $nativeRequest->files->all() ) );
78
		$server = ServerRequestFactory::normalizeServer( $nativeRequest->server->all() );
79
		$headers = $nativeRequest->headers->all();
80
		$cookies = $nativeRequest->cookies->all();
81
		$post = $nativeRequest->request->all();
82
		$query = $nativeRequest->query->all();
83
		$method = $nativeRequest->getMethod();
84
		$uri = $nativeRequest->getUri();
85
86
		$body = new Stream( 'php://temp', 'wb+' );
87
		$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...
88
89
		$request = new ServerRequest( $server, $files, $uri, $method, $body, $headers, $cookies, $query, $post );
90
91
		foreach( $nativeRequest->attributes->all() as $key => $value ) {
92
			$request = $request->withAttribute( $key, $value );
93
		}
94
95
		return $request;
96
	}
97
98
99
	/**
100
	 * Converts Symfony uploaded files array to the PSR-7 one.
101
	 *
102
	 * @param array $files Multi-dimensional list of uploaded files from Symfony request
103
	 * @return array Multi-dimensional list of uploaded files as PSR-7 objects
104
	 */
105
	protected function getFiles( array $files )
106
	{
107
		$list = [];
108
109
		foreach( $files as $key => $value )
110
		{
111
			if( $value instanceof \Symfony\Component\HttpFoundation\File\UploadedFile )
112
			{
113
				$list[$key] = new \Zend\Diactoros\UploadedFile(
114
					$value->getRealPath(),
115
					$value->getSize(),
116
					$value->getError(),
0 ignored issues
show
Bug introduced by
It seems like $value->getError() targeting Symfony\Component\HttpFo...ploadedFile::getError() can also be of type boolean; however, Zend\Diactoros\UploadedFile::__construct() does only seem to accept integer, 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...
117
					$value->getClientOriginalName(),
118
					$value->getClientMimeType()
119
				);
120
			}
121
			elseif( is_array( $value ) )
122
			{
123
				$list[$key] = $this->getFiles( $value );
124
			}
125
		}
126
127
		return $list;
128
	}
129
}
130