Completed
Push — test ( c38ed8 )
by Aimeos
10:42
created

JsonapiController::getPsrRequest()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 12
nc 4
nop 0
1
<?php
2
3
/**
4
 * @license LGPLv3, http://www.gnu.org/copyleft/lgpl.html
5
 * @copyright Aimeos (aimeos.org), 2017
6
 * @package flow
7
 * @subpackage Controller
8
 */
9
10
11
namespace Aimeos\Shop\Controller;
12
13
use TYPO3\Flow\Annotations as Flow;
14
use Zend\Diactoros\Response;
15
16
17
/**
18
 * Aimeos controller for the frontend JSON REST API
19
 *
20
 * @package flow
21
 * @subpackage Controller
22
 */
23
class JsonapiController extends \TYPO3\Flow\Mvc\Controller\ActionController
24
{
25
	/**
26
	 * @var \Aimeos\Shop\Base\Aimeos
27
	 * @Flow\Inject
28
	 */
29
	protected $aimeos;
30
31
	/**
32
	 * @var \Aimeos\Shop\Base\Context
33
	 * @Flow\Inject
34
	 */
35
	protected $context;
36
37
	/**
38
	 * @var \Aimeos\Shop\Base\View
39
	 * @Flow\Inject
40
	 */
41
	protected $viewContainer;
42
43
44
	/**
45
	 * Deletes the resource object or a list of resource objects
46
	 *
47
	 * @param string $resource Resource location, e.g. "product"
48
	 * @param string $site Unique site code
49
	 * @param integer $id Unique resource ID
50
	 * @return string Response message content
51
	 */
52
	public function deleteAction( $resource, $site = 'default', $id = '' )
0 ignored issues
show
Unused Code introduced by
The parameter $id is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
53
	{
54
		$request = $this->request->getHttpRequest();
0 ignored issues
show
Unused Code introduced by
$request is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
55
56
		$client = $this->createClient( $site, $resource );
57
		$psrResponse = $client->delete( $this->getPsrRequest(), new Response() );
58
59
		return $this->setPsrResponse( $psrResponse );
60
	}
61
62
63
	/**
64
	 * Returns the requested resource object or list of resource objects
65
	 *
66
	 * @param string $resource Resource location, e.g. "product"
67
	 * @param string $site Unique site code
68
	 * @param integer $id Unique resource ID
69
	 * @return string Response message content
70
	 */
71
	public function getAction( $resource, $site = 'default', $id = '' )
0 ignored issues
show
Unused Code introduced by
The parameter $id is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
72
	{
73
		$request = $this->request->getHttpRequest();
0 ignored issues
show
Unused Code introduced by
$request is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
74
75
		$client = $this->createClient( $site, $resource );
76
		$psrResponse = $client->get( $this->getPsrRequest(), new Response() );
77
78
		return $this->setPsrResponse( $psrResponse );
79
	}
80
81
82
	/**
83
	 * Updates a resource object or a list of resource objects
84
	 *
85
	 * @param string $resource Resource location, e.g. "product"
86
	 * @param string $site Unique site code
87
	 * @param integer $id Unique resource ID
88
	 * @return string Response message content
89
	 */
90
	public function patchAction( $resource, $site = 'default', $id = '' )
0 ignored issues
show
Unused Code introduced by
The parameter $id is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
91
	{
92
		$request = $this->request->getHttpRequest();
0 ignored issues
show
Unused Code introduced by
$request is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
93
94
		$client = $this->createClient( $site, $resource );
95
		$psrResponse = $client->patch( $this->getPsrRequest(), new Response() );
96
97
		return $this->setPsrResponse( $psrResponse );
98
	}
99
100
101
	/**
102
	 * Creates a new resource object or a list of resource objects
103
	 *
104
	 * @param string $resource Resource location, e.g. "product"
105
	 * @param string $site Unique site code
106
	 * @param integer $id Unique ID of the resource
107
	 * @return string Response message content
108
	 */
109
	public function postAction( $resource, $site = 'default', $id = '' )
0 ignored issues
show
Unused Code introduced by
The parameter $id is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
110
	{
111
		$request = $this->request->getHttpRequest();
0 ignored issues
show
Unused Code introduced by
$request is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
112
113
		$client = $this->createClient( $site, $resource );
114
		$psrResponse = $client->post( $this->getPsrRequest(), new Response() );
115
116
		return $this->setPsrResponse( $psrResponse );
117
	}
118
119
120
	/**
121
	 * Creates or updates a single resource object
122
	 *
123
	 * @param string $resource Resource location, e.g. "product"
124
	 * @param string $site Unique site code
125
	 * @param integer $id Unique resource ID
126
	 * @return string Response message content
127
	 */
128
	public function putAction( $resource, $site = 'default', $id = '' )
0 ignored issues
show
Unused Code introduced by
The parameter $id is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
129
	{
130
		$request = $this->request->getHttpRequest();
0 ignored issues
show
Unused Code introduced by
$request is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
131
132
		$client = $this->createClient( $site, $resource );
133
		$psrResponse = $client->put( $this->getPsrRequest(), new Response() );
134
135
		return $this->setPsrResponse( $psrResponse );
136
	}
137
138
139
	/**
140
	 * Returns the available HTTP verbs and the resource URLs
141
	 *
142
	 * @param string $resource Resource location, e.g. "product"
143
	 * @param string $site Unique site code
144
	 * @return string Response message content
145
	 */
146
	public function optionsAction( $resource = '', $site = 'default' )
147
	{
148
		$request = $this->request->getHttpRequest();
0 ignored issues
show
Unused Code introduced by
$request is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
149
150
		$client = $this->createClient( $site, $resource );
151
		$psrResponse = $client->options( $this->getPsrRequest(), new Response() );
152
153
		return $this->setPsrResponse( $psrResponse );
154
	}
155
156
157
	/**
158
	 * Returns the resource controller
159
	 *
160
	 * @param string $sitecode Unique site code
161
	 * @param string Resource location, e.g. "product"
162
	 * @return \Aimeos\Client\JsonApi\Iface JsonApi client
163
	 */
164
	protected function createClient( $sitecode, $resource )
0 ignored issues
show
Unused Code introduced by
The parameter $sitecode is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
165
	{
166
		$tmplPaths = $this->aimeos->get()->getCustomPaths( 'client/jsonapi/templates' );
167
168
		$context = $this->context->get( $this->request );
169
		$langid = $context->getLocale()->getLanguageId();
170
171
		$view =$this->viewContainer->create( $context, $this->uriBuilder, $tmplPaths, $this->request, $langid );
172
		$context->setView( $view );
173
174
		return \Aimeos\Client\JsonApi\Factory::createClient( $context, $tmplPaths, $resource );
175
	}
176
177
178
	/**
179
	 * Returns a PSR-7 request object for the current request
180
	 *
181
	 * @return \Psr\Http\Message\ServerRequestInterface PSR-7 request object
182
	 */
183
	protected function getPsrRequest()
184
	{
185
		$psrRequest = new \Zend\Diactoros\ServerRequest();
186
		$flowRequest = $this->request->getHttpRequest();
187
188
		try {
189
			$resource = $flowRequest->getContent( true );
190
		} catch( \TYPO3\Flow\Http\Exception $exception ) {
191
			$resource = fopen( 'php://temp', 'rw' );
192
			fwrite( $resource, $flowRequest->getContent() );
193
		}
194
195
		$psrRequest = $psrRequest->withBody( new \Zend\Diactoros\Stream( $resource ) );
196
197
		foreach( $flowRequest->getHeaders()->getAll() as $headerName => $headerValues ) {
198
			$psrRequest = $psrRequest->withHeader( $headerName, $headerValues );
199
		}
200
201
		return $psrRequest;
202
	}
203
204
205
	/**
206
	 * Set the response data from a PSR-7 response object
207
	 *
208
	 * @param \Psr\Http\Message\ResponseInterface $response PSR-7 response object
209
	 * @return string Response message content
210
	 */
211
	protected function setPsrResponse( \Psr\Http\Message\ResponseInterface $response )
212
	{
213
		$this->response->setStatus( $response->getStatusCode() );
214
215
		foreach( $response->getHeaders() as $key => $value ) {
216
			$this->response->setHeader( $key, $value );
217
		}
218
219
		return (string) $response->getBody();
220
	}
221
}
222