Completed
Push — jsonapi ( d4e38d...e06451 )
by Aimeos
02:03
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 Method

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