Completed
Push — master ( 9e3b53...4d169e )
by Aimeos
11:47
created

JsonapiController::postAction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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