Completed
Push — master ( b0c8aa...f38e69 )
by Aimeos
04:47
created

Standard   A

Complexity

Total Complexity 39

Size/Duplication

Total Lines 308
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 39
eloc 133
c 1
b 0
f 0
dl 0
loc 308
rs 9.28

6 Methods

Rating   Name   Duplication   Size   Complexity  
A render() 0 32 1
B post() 0 50 11
A options() 0 37 1
B patch() 0 41 9
B delete() 0 46 10
B get() 0 40 7
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2020
6
 * @package Client
7
 * @subpackage JsonApi
8
 */
9
10
11
namespace Aimeos\Client\JsonApi\Customer\Review;
12
13
use Psr\Http\Message\ResponseInterface;
14
use Psr\Http\Message\ServerRequestInterface;
15
16
17
/**
18
 * JSON API customer/review client
19
 *
20
 * @package Client
21
 * @subpackage JsonApi
22
 */
23
class Standard
24
	extends \Aimeos\Client\JsonApi\Base
25
	implements \Aimeos\Client\JsonApi\Iface
26
{
27
	/**
28
	 * Deletes the resource or the resource list
29
	 *
30
	 * @param \Psr\Http\Message\ServerRequestInterface $request Request object
31
	 * @param \Psr\Http\Message\ResponseInterface $response Response object
32
	 * @return \Psr\Http\Message\ResponseInterface Modified response object
33
	 */
34
	public function delete( ServerRequestInterface $request, ResponseInterface $response ) : \Psr\Http\Message\ResponseInterface
35
	{
36
		$view = $this->getView();
37
		$context = $this->getContext();
38
39
		try
40
		{
41
			$body = (string) $request->getBody();
42
			$cntl = \Aimeos\Controller\Frontend::create( $context, 'review' );
43
44
			if( ( $relId = $view->param( 'relatedid' ) ) === null )
45
			{
46
				if( ( $payload = json_decode( $body ) ) === null || !isset( $payload->data ) ) {
47
					throw new \Aimeos\Client\JsonApi\Exception( sprintf( 'Invalid JSON in body' ), 400 );
48
				}
49
50
				if( !is_array( $payload->data ) ) {
51
					$payload->data = [$payload->data];
52
				}
53
54
				$cntl->delete( array_column( $payload->data, 'id' ) );
55
			}
56
			else
57
			{
58
				$cntl->delete( $relId );
59
			}
60
61
			$status = 200;
62
		}
63
		catch( \Aimeos\Controller\Frontend\Review\Exception $e )
64
		{
65
			$status = 403;
66
			$view->errors = $this->getErrorDetails( $e, 'controller/frontend' );
67
		}
68
		catch( \Aimeos\MShop\Exception $e )
69
		{
70
			$status = 404;
71
			$view->errors = $this->getErrorDetails( $e, 'mshop' );
72
		}
73
		catch( \Exception $e )
74
		{
75
			$status = $e->getCode() >= 100 && $e->getCode() < 600 ? $e->getCode() : 500;
76
			$view->errors = $this->getErrorDetails( $e );
77
		}
78
79
		return $this->render( $response, $view, $status );
80
	}
81
82
83
	/**
84
	 * Returns the resource or the resource list
85
	 *
86
	 * @param \Psr\Http\Message\ServerRequestInterface $request Request object
87
	 * @param \Psr\Http\Message\ResponseInterface $response Response object
88
	 * @return \Psr\Http\Message\ResponseInterface Modified response object
89
	 */
90
	public function get( ServerRequestInterface $request, ResponseInterface $response ) : \Psr\Http\Message\ResponseInterface
91
	{
92
		$view = $this->getView();
93
		$context = $this->getContext();
94
95
		try
96
		{
97
			$cntl = \Aimeos\Controller\Frontend::create( $context, 'review' );
98
99
			if( ( $relId = $view->param( 'relatedid' ) ) == null )
100
			{
101
				$total = 0;
102
				$view->items = $cntl->parse( $view->param( 'filter', [] ) )->list( $total );
103
				$view->total = $total;
104
			}
105
			else
106
			{
107
				$view->items = $cntl->get( $relId );
108
				$view->total = 1;
109
			}
110
111
			$status = 200;
112
		}
113
		catch( \Aimeos\Controller\Frontend\Review\Exception $e )
114
		{
115
			$status = 403;
116
			$view->errors = $this->getErrorDetails( $e, 'controller/frontend' );
117
		}
118
		catch( \Aimeos\MShop\Exception $e )
119
		{
120
			$status = 404;
121
			$view->errors = $this->getErrorDetails( $e, 'mshop' );
122
		}
123
		catch( \Exception $e )
124
		{
125
			$status = $e->getCode() >= 100 && $e->getCode() < 600 ? $e->getCode() : 500;
126
			$view->errors = $this->getErrorDetails( $e );
127
		}
128
129
		return $this->render( $response, $view, $status );
130
	}
131
132
133
	/**
134
	 * Updates the resource or the resource list partitially
135
	 *
136
	 * @param \Psr\Http\Message\ServerRequestInterface $request Request object
137
	 * @param \Psr\Http\Message\ResponseInterface $response Response object
138
	 * @return \Psr\Http\Message\ResponseInterface Modified response object
139
	 */
140
	public function patch( ServerRequestInterface $request, ResponseInterface $response ) : \Psr\Http\Message\ResponseInterface
141
	{
142
		$view = $this->getView();
143
		$context = $this->getContext();
144
145
		try
146
		{
147
			$body = (string) $request->getBody();
148
149
			if( ( $payload = json_decode( $body ) ) === null || !isset( $payload->data->attributes ) ) {
150
				throw new \Aimeos\Client\JsonApi\Exception( sprintf( 'Invalid JSON in body' ), 400 );
151
			}
152
153
			if( ( $id = $view->param( 'relatedid' ) ) === null ) {
154
				throw new \Aimeos\Client\JsonApi\Exception( sprintf( 'Required "relatedid" is missing' ), 400 );
155
			}
156
157
			$cntl = \Aimeos\Controller\Frontend::create( $context, 'review' );
158
			$item = $cntl->create( (array) $payload->data->attributes )->setId( $id );
159
160
			$view->items = $cntl->save( $item );
161
			$view->total = 1;
162
			$status = 200;
163
		}
164
		catch( \Aimeos\Controller\Frontend\Review\Exception $e )
165
		{
166
			$status = 403;
167
			$view->errors = $this->getErrorDetails( $e, 'controller/frontend' );
168
		}
169
		catch( \Aimeos\MShop\Exception $e )
170
		{
171
			$status = 404;
172
			$view->errors = $this->getErrorDetails( $e, 'mshop' );
173
		}
174
		catch( \Exception $e )
175
		{
176
			$status = $e->getCode() >= 100 && $e->getCode() < 600 ? $e->getCode() : 500;
177
			$view->errors = $this->getErrorDetails( $e );
178
		}
179
180
		return $this->render( $response, $view, $status );
181
	}
182
183
184
	/**
185
	 * Creates or updates the resource or the resource list
186
	 *
187
	 * @param \Psr\Http\Message\ServerRequestInterface $request Request object
188
	 * @param \Psr\Http\Message\ResponseInterface $response Response object
189
	 * @return \Psr\Http\Message\ResponseInterface Modified response object
190
	 */
191
	public function post( ServerRequestInterface $request, ResponseInterface $response ) : \Psr\Http\Message\ResponseInterface
192
	{
193
		$view = $this->getView();
194
		$context = $this->getContext();
195
196
		try
197
		{
198
			$body = (string) $request->getBody();
199
200
			if( ( $payload = json_decode( $body ) ) === null || !isset( $payload->data ) ) {
201
				throw new \Aimeos\Client\JsonApi\Exception( sprintf( 'Invalid JSON in body' ), 400 );
202
			}
203
204
			if( !is_array( $payload->data ) ) {
205
				$payload->data = [$payload->data];
206
			}
207
208
			$items = [];
209
			$cntl = \Aimeos\Controller\Frontend::create( $context, 'review' );
210
211
			foreach( $payload->data as $entry )
212
			{
213
				if( !isset( $entry->attributes ) ) {
214
					throw new \Aimeos\Client\JsonApi\Exception( sprintf( 'Attributes are missing', 400 ) );
215
				}
216
217
				$items[] = $cntl->save( $cntl->create( (array) $entry->attributes ) );
218
			}
219
220
			$view->total = count( $items );
221
			$view->items = map( $items );
222
			$status = 201;
223
		}
224
		catch( \Aimeos\Controller\Frontend\Review\Exception $e )
225
		{
226
			$status = 403;
227
			$view->errors = $this->getErrorDetails( $e, 'controller/frontend' );
228
		}
229
		catch( \Aimeos\MShop\Exception $e )
230
		{
231
			$status = 404;
232
			$view->errors = $this->getErrorDetails( $e, 'mshop' );
233
		}
234
		catch( \Exception $e )
235
		{
236
			$status = $e->getCode() >= 100 && $e->getCode() < 600 ? $e->getCode() : 500;
237
			$view->errors = $this->getErrorDetails( $e );
238
		}
239
240
		return $this->render( $response, $view, $status );
241
	}
242
243
244
	/**
245
	 * Returns the available REST verbs and the available parameters
246
	 *
247
	 * @param \Psr\Http\Message\ServerRequestInterface $request Request object
248
	 * @param \Psr\Http\Message\ResponseInterface $response Response object
249
	 * @return \Psr\Http\Message\ResponseInterface Modified response object
250
	 */
251
	public function options( ServerRequestInterface $request, ResponseInterface $response ) : \Psr\Http\Message\ResponseInterface
252
	{
253
		$view = $this->getView();
254
255
		$view->attributes = [
256
			'review.domain' => [
257
				'label' => 'Domain of the reviewed item',
258
				'type' => 'string', 'default' => '', 'required' => true,
259
			],
260
			'review.refid' => [
261
				'label' => 'ID of the reviewd item',
262
				'type' => 'string', 'default' => '', 'required' => true,
263
			],
264
			'review.rating' => [
265
				'label' => 'Rating from 0 to 5',
266
				'type' => 'string', 'default' => '', 'required' => true,
267
			],
268
			'review.name' => [
269
				'label' => 'Name of the reviewer',
270
				'type' => 'string', 'default' => '', 'required' => false,
271
			],
272
			'review.comment' => [
273
				'label' => 'Comment for the rating',
274
				'type' => 'string', 'default' => '', 'required' => false,
275
			],
276
		];
277
278
		$tplconf = 'client/jsonapi/standard/template-options';
279
		$default = 'options-standard';
280
281
		$body = $view->render( $view->config( $tplconf, $default ) );
282
283
		return $response->withHeader( 'Allow', 'DELETE,GET,OPTIONS,PATCH,POST' )
284
			->withHeader( 'Cache-Control', 'max-age=300' )
285
			->withHeader( 'Content-Type', 'application/vnd.api+json' )
286
			->withBody( $view->response()->createStreamFromString( $body ) )
287
			->withStatus( 200 );
288
	}
289
290
291
	/**
292
	 * Returns the response object with the rendered header and body
293
	 *
294
	 * @param \Psr\Http\Message\ResponseInterface $response Response object
295
	 * @param \Aimeos\MW\View\Iface $view View instance
296
	 * @param int $status HTTP status code
297
	 * @return \Psr\Http\Message\ResponseInterface Modified response object
298
	 */
299
	protected function render( ResponseInterface $response, \Aimeos\MW\View\Iface $view, int $status ) : \Psr\Http\Message\ResponseInterface
300
	{
301
		/** client/jsonapi/customer/review/standard/template
302
		 * Relative path to the customer review JSON API template
303
		 *
304
		 * The template file contains the code and processing instructions
305
		 * to generate the result shown in the JSON API body. The
306
		 * configuration string is the path to the template file relative
307
		 * to the templates directory (usually in client/jsonapi/templates).
308
		 *
309
		 * You can overwrite the template file configuration in extensions and
310
		 * provide alternative templates. These alternative templates should be
311
		 * named like the default one but with the string "standard" replaced by
312
		 * an unique name. You may use the name of your project for this. If
313
		 * you've implemented an alternative client class as well, "standard"
314
		 * should be replaced by the name of the new class.
315
		 *
316
		 * @param string Relative path to the template creating the body for the JSON API
317
		 * @since 2017.07
318
		 * @category Developer
319
		 */
320
		$tplconf = 'client/jsonapi/customer/review/standard/template';
321
		$default = 'customer/review/standard';
322
323
		$view->customerid = $this->getContext()->getUserId();
324
		$body = $view->render( $view->config( $tplconf, $default ) );
325
326
		return $response->withHeader( 'Allow', 'DELETE,GET,OPTIONS,PATCH,POST' )
327
			->withHeader( 'Cache-Control', 'no-cache, private' )
328
			->withHeader( 'Content-Type', 'application/vnd.api+json' )
329
			->withBody( $view->response()->createStreamFromString( $body ) )
330
			->withStatus( $status );
331
	}
332
}
333