JsonadmController::getAction()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 8
rs 10
1
<?php
2
3
/**
4
 * @license LGPLv3, http://www.gnu.org/copyleft/lgpl.html
5
 * @copyright Aimeos (aimeos.org), 2015-2016
6
 * @package flow
7
 * @subpackage Controller
8
 */
9
10
11
namespace Aimeos\Shop\Controller;
12
13
use Neos\Flow\Annotations as Flow;
14
use Zend\Diactoros\Response;
15
16
17
/**
18
 * Aimeos controller for the JSON REST API
19
 *
20
 * @package flow
21
 * @subpackage Controller
22
 */
23
class JsonadmController extends \Neos\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\I18n
39
	 * @Flow\Inject
40
	 */
41
	protected $i18n;
42
43
	/**
44
	 * @var \Aimeos\Shop\Base\Locale
45
	 * @Flow\Inject
46
	 */
47
	protected $locale;
48
49
	/**
50
	 * @var \Aimeos\Shop\Base\View
51
	 * @Flow\Inject
52
	 */
53
	protected $viewContainer;
54
55
56
	/**
57
	 * Deletes the resource object or a list of resource objects
58
	 *
59
	 * @param string $resource Resource location, e.g. "product/property/type"
60
	 * @param string $site Unique site code
61
	 * @return string Response message content
62
	 */
63
	public function deleteAction( $resource, $site = 'default' )
64
	{
65
		$request = $this->request->getHttpRequest();
66
67
		$client = $this->createAdmin( $site, $resource, $request->getArgument( 'lang' ) );
68
		$psrResponse = $client->delete( $this->getPsrRequest(), new Response() );
69
70
		return $this->setPsrResponse( $psrResponse );
71
	}
72
73
74
	/**
75
	 * Returns the requested resource object or list of resource objects
76
	 *
77
	 * @param string $resource Resource location, e.g. "product/property/type"
78
	 * @param string $site Unique site code
79
	 * @return string Response message content
80
	 */
81
	public function getAction( $resource, $site = 'default' )
82
	{
83
		$request = $this->request->getHttpRequest();
84
85
		$client = $this->createAdmin( $site, $resource, $request->getArgument( 'lang' ) );
86
		$psrResponse = $client->get( $this->getPsrRequest(), new Response() );
87
88
		return $this->setPsrResponse( $psrResponse );
89
	}
90
91
92
	/**
93
	 * Updates a resource object or a list of resource objects
94
	 *
95
	 * @param string $resource Resource location, e.g. "product/property/type"
96
	 * @param string $site Unique site code
97
	 * @return string Response message content
98
	 */
99
	public function patchAction( $resource, $site = 'default' )
100
	{
101
		$request = $this->request->getHttpRequest();
102
103
		$client = $this->createAdmin( $site, $resource, $request->getArgument( 'lang' ) );
104
		$psrResponse = $client->patch( $this->getPsrRequest(), new Response() );
105
106
		return $this->setPsrResponse( $psrResponse );
107
	}
108
109
110
	/**
111
	 * Creates a new resource object or a list of resource objects
112
	 *
113
	 * @param string $resource Resource location, e.g. "product/property/type"
114
	 * @param string $site Unique site code
115
	 * @return string Response message content
116
	 */
117
	public function postAction( $resource, $site = 'default' )
118
	{
119
		$request = $this->request->getHttpRequest();
120
121
		$client = $this->createAdmin( $site, $resource, $request->getArgument( 'lang' ) );
122
		$psrResponse = $client->post( $this->getPsrRequest(), new Response() );
123
124
		return $this->setPsrResponse( $psrResponse );
125
	}
126
127
128
	/**
129
	 * Creates or updates a single resource object
130
	 *
131
	 * @param string $resource Resource location, e.g. "product/property/type"
132
	 * @param string $site Unique site code
133
	 * @return string Response message content
134
	 */
135
	public function putAction( $resource, $site = 'default' )
136
	{
137
		$request = $this->request->getHttpRequest();
138
139
		$client = $this->createAdmin( $site, $resource, $request->getArgument( 'lang' ) );
140
		$psrResponse = $client->put( $this->getPsrRequest(), new Response() );
141
142
		return $this->setPsrResponse( $psrResponse );
143
	}
144
145
146
	/**
147
	 * Returns the available HTTP verbs and the resource URLs
148
	 *
149
	 * @param string $resource Resource location, e.g. "product/property/type"
150
	 * @param string $site Unique site code
151
	 * @return string Response message content
152
	 */
153
	public function optionsAction( $resource = '', $site = 'default' )
154
	{
155
		$request = $this->request->getHttpRequest();
156
157
		$client = $this->createAdmin( $site, $resource, $request->getArgument( 'lang' ) );
158
		$psrResponse = $client->options( $this->getPsrRequest(), new Response() );
159
160
		return $this->setPsrResponse( $psrResponse );
161
	}
162
163
164
	/**
165
	 * Returns the resource controller
166
	 *
167
	 * @param string $sitecode Unique site code
168
	 * @param string Resource location, e.g. "product/property/type"
169
	 * @param string $lang Language code
170
	 * @return \Aimeos\Admin\JsonAdm\Iface JsonAdm client
171
	 */
172
	protected function createAdmin( $sitecode, $resource, $lang )
173
	{
174
		$aimeos = $this->aimeos->get();
175
		$lang = ( $lang ? $lang : 'en' );
176
		$templatePaths = $aimeos->getCustomPaths( 'admin/jsonadm/templates' );
177
178
		$context = $this->context->get( null, 'backend' );
179
		$context->setI18n( $this->i18n->get( array( $lang, 'en' ) ) );
180
		$context->setLocale( $this->locale->getBackend( $context, $sitecode ) );
181
		$context->setView( $this->viewContainer->create( $context, $this->uriBuilder, $templatePaths, $this->request, $lang ) );
182
183
		return \Aimeos\Admin\JsonAdm::create( $context, $aimeos, $resource );
184
	}
185
186
187
	/**
188
	 * Returns a PSR-7 request object for the current request
189
	 *
190
	 * @return \Psr\Http\Message\ServerRequestInterface PSR-7 request object
191
	 */
192
	protected function getPsrRequest()
193
	{
194
		$psrRequest = new \Zend\Diactoros\ServerRequest();
195
		$flowRequest = $this->request->getHttpRequest();
196
197
		try {
198
			$resource = $flowRequest->getContent( true );
199
		} catch( \Neos\Flow\Http\Exception $exception ) {
200
			$resource = fopen( 'php://temp', 'rw' );
201
			fwrite( $resource, $flowRequest->getContent() );
0 ignored issues
show
Bug introduced by
It seems like $resource can also be of type false; however, parameter $handle of fwrite() does only seem to accept resource, 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

201
			fwrite( /** @scrutinizer ignore-type */ $resource, $flowRequest->getContent() );
Loading history...
202
		}
203
204
		$psrRequest = $psrRequest->withBody( new \Zend\Diactoros\Stream( $resource ) );
0 ignored issues
show
Bug introduced by
It seems like $resource can also be of type false; however, parameter $stream of Zend\Diactoros\Stream::__construct() does only seem to accept resource|string, 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

204
		$psrRequest = $psrRequest->withBody( new \Zend\Diactoros\Stream( /** @scrutinizer ignore-type */ $resource ) );
Loading history...
205
206
		foreach( $flowRequest->getHeaders()->getAll() as $headerName => $headerValues ) {
207
			$psrRequest = $psrRequest->withHeader( $headerName, $headerValues );
208
		}
209
210
		return $psrRequest;
211
	}
212
213
214
	/**
215
	 * Set the response data from a PSR-7 response object
216
	 *
217
	 * @param \Psr\Http\Message\ResponseInterface $response PSR-7 response object
218
	 * @return string Response message content
219
	 */
220
	protected function setPsrResponse( \Psr\Http\Message\ResponseInterface $response )
221
	{
222
		$this->response->setStatus( $response->getStatusCode() );
223
224
		foreach( $response->getHeaders() as $key => $value ) {
225
			$this->response->setHeader( $key, $value );
226
		}
227
228
		return (string) $response->getBody();
229
	}
230
}
231