Completed
Push — jsonapi ( 59eae9...d4e38d )
by Aimeos
01:50
created

JsonapiController::indexAction()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 23
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 23
rs 8.5906
cc 6
eloc 17
nc 6
nop 1
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 Neos\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 \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\View
39
	 * @Flow\Inject
40
	 */
41
	protected $viewContainer;
42
43
44
	/**
45
	 * Fallback action for JSON REST API
46
	 *
47
	 * @param string Resource location, e.g. "customer"
48
	 * @return string Response message content
49
	 */
50
	public function indexAction( $resource )
51
	{
52
		$client = $this->createClient( $resource );
53
		$method = $this->request->getHttpRequest()->getMethod();
54
55
		switch( $method )
56
		{
57
			case 'GET':
58
				$psrResponse = $client->get( $this->getPsrRequest(), new Response() ); break;
59
			case 'DELETE':
60
				$psrResponse = $client->delete( $this->getPsrRequest(), new Response() ); break;
61
			case 'PATCH':
62
				$psrResponse = $client->patch( $this->getPsrRequest(), new Response() ); break;
63
			case 'POST':
64
				$psrResponse = $client->post( $this->getPsrRequest(), new Response() ); break;
65
			case 'PUT':
66
				$psrResponse = $client->put( $this->getPsrRequest(), new Response() ); break;
67
			default:
68
				throw new \RuntimeException( sprintf( 'Unsupported HTTP method "%1$s"', $method ) );
69
		}
70
71
		return $this->setPsrResponse( $psrResponse );
72
	}
73
74
75
	/**
76
	 * Deletes the resource object or a list of resource objects
77
	 *
78
	 * @param string Resource location, e.g. "customer"
79
	 * @return string Response message content
80
	 */
81
	public function deleteAction( $resource )
82
	{
83
		$client = $this->createClient( $resource );
84
		$psrResponse = $client->delete( $this->getPsrRequest(), new Response() );
85
86
		return $this->setPsrResponse( $psrResponse );
87
	}
88
89
90
	/**
91
	 * Returns the requested resource object or list of resource objects
92
	 *
93
	 * @param string Resource location, e.g. "customer"
94
	 * @return string Response message content
95
	 */
96
	public function getAction( $resource )
97
	{
98
		$client = $this->createClient( $resource );
99
		$psrResponse = $client->get( $this->getPsrRequest(), new Response() );
100
101
		return $this->setPsrResponse( $psrResponse );
102
	}
103
104
105
	/**
106
	 * Updates a resource object or a list of resource objects
107
	 *
108
	 * @param string Resource location, e.g. "customer"
109
	 * @return string Response message content
110
	 */
111
	public function patchAction( $resource )
112
	{
113
		$client = $this->createClient( $resource );
114
		$psrResponse = $client->patch( $this->getPsrRequest(), new Response() );
115
116
		return $this->setPsrResponse( $psrResponse );
117
	}
118
119
120
	/**
121
	 * Creates a new resource object or a list of resource objects
122
	 *
123
	 * @param string Resource location, e.g. "customer"
124
	 * @return string Response message content
125
	 */
126
	public function postAction( $resource )
127
	{
128
		$client = $this->createClient( $resource );
129
		$psrResponse = $client->post( $this->getPsrRequest(), new Response() );
130
131
		return $this->setPsrResponse( $psrResponse );
132
	}
133
134
135
	/**
136
	 * Creates or updates a single resource object
137
	 *
138
	 * @param string Resource location, e.g. "customer"
139
	 * @return string Response message content
140
	 */
141
	public function putAction( $resource )
142
	{
143
		$client = $this->createClient( $resource );
144
		$psrResponse = $client->put( $this->getPsrRequest(), new Response() );
145
146
		return $this->setPsrResponse( $psrResponse );
147
	}
148
149
150
	/**
151
	 * Returns the available HTTP verbs and the resource URLs
152
	 *
153
	 * @param string $resource Resource location, e.g. "product"
154
	 * @return string Response message content
155
	 */
156
	public function optionsAction( $resource = '' )
157
	{
158
		$client = $this->createClient( $resource );
159
		$psrResponse = $client->options( $this->getPsrRequest(), new Response() );
160
161
		return $this->setPsrResponse( $psrResponse );
162
	}
163
164
165
	/**
166
	 * Returns the resource controller
167
	 *
168
	 * @param string Resource location, e.g. "customer"
169
	 * @return \Aimeos\Client\JsonApi\Iface JsonApi client
170
	 */
171
	protected function createClient( $resource )
172
	{
173
		$related = '';
174
		$tmplPaths = $this->aimeos->get()->getCustomPaths( 'client/jsonapi/templates' );
175
176
		if( $this->request->hasArgument( 'related' ) ) {
177
			$related = $this->request->getArgument( 'related' );
178
		}
179
180
		$context = $this->context->get( $this->request );
181
		$langid = $context->getLocale()->getLanguageId();
182
183
		$view =$this->viewContainer->create( $context, $this->uriBuilder, $tmplPaths, $this->request, $langid );
184
		$context->setView( $view );
185
186
		return \Aimeos\Client\JsonApi\Factory::createClient( $context, $tmplPaths, $resource . '/' . $related );
187
	}
188
189
190
	/**
191
	 * Returns a PSR-7 request object for the current request
192
	 *
193
	 * @return \Psr\Http\Message\ServerRequestInterface PSR-7 request object
194
	 */
195
	protected function getPsrRequest()
196
	{
197
		$psrRequest = new \Zend\Diactoros\ServerRequest();
198
		$flowRequest = $this->request->getHttpRequest();
199
200
		try {
201
			$resource = $flowRequest->getContent( true );
202
		} catch( \Neos\Flow\Http\Exception $exception ) {
203
			$resource = fopen( 'php://temp', 'rw' );
204
			fwrite( $resource, $flowRequest->getContent() );
205
		}
206
207
		$psrRequest = $psrRequest->withBody( new \Zend\Diactoros\Stream( $resource ) );
208
209
		foreach( $flowRequest->getHeaders()->getAll() as $headerName => $headerValues ) {
210
			$psrRequest = $psrRequest->withHeader( $headerName, $headerValues );
211
		}
212
213
		return $psrRequest;
214
	}
215
216
217
	/**
218
	 * Set the response data from a PSR-7 response object
219
	 *
220
	 * @param \Psr\Http\Message\ResponseInterface $response PSR-7 response object
221
	 * @return string Response message content
222
	 */
223
	protected function setPsrResponse( \Psr\Http\Message\ResponseInterface $response )
224
	{
225
		$this->response->setStatus( $response->getStatusCode() );
226
227
		foreach( $response->getHeaders() as $key => $value ) {
228
			$this->response->setHeader( $key, $value );
229
		}
230
231
		return (string) $response->getBody();
232
	}
233
}
234