Completed
Push — master ( 270945...1cb1eb )
by Aimeos
03:29
created

Standard::render()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 32
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
c 1
b 0
f 0
dl 0
loc 32
rs 9.9666
cc 1
nc 1
nop 3
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
			$manager = \Aimeos\MShop::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
				$ids = [];
55
56
				foreach( $payload->data as $entry )
57
				{
58
					if( !isset( $entry->id ) ) {
59
						throw new \Aimeos\Client\JsonApi\Exception( sprintf( 'ID is missing' ), 400 );
60
					}
61
62
					$ids[] = $entry->id;
63
				}
64
65
				$filter = $manager->filter()->add( [
66
					'review.customerid' => $context->getUserId(),
67
					'review.id' => $ids
68
				] );
69
70
				$manager->delete( $manager->search( $filter )->toArray() );
71
			}
72
			else
73
			{
74
				$item = $manager->get( $relId );
75
76
				if( $item->getCustomerId() !== $context->getUserId() ) {
77
					throw new \Aimeos\Client\JsonApi\Exception( sprintf( 'Operation not allowed' ), 403 );
78
				}
79
80
				$manager->delete( $item );
81
			}
82
83
			$status = 200;
84
		}
85
		catch( \Aimeos\Controller\Frontend\Review\Exception $e )
86
		{
87
			$status = 403;
88
			$view->errors = $this->getErrorDetails( $e, 'controller/frontend' );
89
		}
90
		catch( \Aimeos\MShop\Exception $e )
91
		{
92
			$status = 404;
93
			$view->errors = $this->getErrorDetails( $e, 'mshop' );
94
		}
95
		catch( \Exception $e )
96
		{
97
			$status = $e->getCode() >= 100 && $e->getCode() < 600 ? $e->getCode() : 500;
98
			$view->errors = $this->getErrorDetails( $e );
99
		}
100
101
		return $this->render( $response, $view, $status );
102
	}
103
104
105
	/**
106
	 * Returns the resource or the resource list
107
	 *
108
	 * @param \Psr\Http\Message\ServerRequestInterface $request Request object
109
	 * @param \Psr\Http\Message\ResponseInterface $response Response object
110
	 * @return \Psr\Http\Message\ResponseInterface Modified response object
111
	 */
112
	public function get( ServerRequestInterface $request, ResponseInterface $response ) : \Psr\Http\Message\ResponseInterface
113
	{
114
		$view = $this->getView();
115
		$context = $this->getContext();
116
117
		try
118
		{
119
			$manager = \Aimeos\MShop::create( $context, 'review' );
120
121
			if( ( $relId = $view->param( 'relatedid' ) ) == null )
122
			{
123
				$total = 0;
124
				$filter = $manager->filter()->add( 'review.customerid', '==', $context->getUserId() );
125
				$filter->add( $filter->parse( $view->param( 'filter', [] ) ) ?: [] );
126
127
				$view->items = $manager->search( $filter, [], $total );
128
				$view->total = $total;
129
			}
130
			else
131
			{
132
				$item = $manager->get( $relId );
133
134
				if( $item->getCustomerId() !== $context->getUserId() ) {
135
					throw new \Aimeos\Client\JsonApi\Exception( sprintf( 'Item not found' ), 404 );
136
				}
137
138
				$view->items = $item;
139
				$view->total = 1;
140
			}
141
142
			$status = 200;
143
		}
144
		catch( \Aimeos\Controller\Frontend\Review\Exception $e )
145
		{
146
			$status = 403;
147
			$view->errors = $this->getErrorDetails( $e, 'controller/frontend' );
148
		}
149
		catch( \Aimeos\MShop\Exception $e )
150
		{
151
			$status = 404;
152
			$view->errors = $this->getErrorDetails( $e, 'mshop' );
153
		}
154
		catch( \Exception $e )
155
		{
156
			$status = $e->getCode() >= 100 && $e->getCode() < 600 ? $e->getCode() : 500;
157
			$view->errors = $this->getErrorDetails( $e );
158
		}
159
160
		return $this->render( $response, $view, $status );
161
	}
162
163
164
	/**
165
	 * Updates the resource or the resource list partitially
166
	 *
167
	 * @param \Psr\Http\Message\ServerRequestInterface $request Request object
168
	 * @param \Psr\Http\Message\ResponseInterface $response Response object
169
	 * @return \Psr\Http\Message\ResponseInterface Modified response object
170
	 */
171
	public function patch( ServerRequestInterface $request, ResponseInterface $response ) : \Psr\Http\Message\ResponseInterface
172
	{
173
		$view = $this->getView();
174
		$context = $this->getContext();
175
176
		try
177
		{
178
			$body = (string) $request->getBody();
179
180
			if( ( $payload = json_decode( $body ) ) === null || !isset( $payload->data->attributes ) ) {
181
				throw new \Aimeos\Client\JsonApi\Exception( sprintf( 'Invalid JSON in body' ), 400 );
182
			}
183
184
			if( ( $id = $view->param( 'relatedid' ) ) === null ) {
185
				throw new \Aimeos\Client\JsonApi\Exception( sprintf( 'Required "relatedid" is missing' ), 400 );
186
			}
187
188
			$manager = \Aimeos\MShop::create( $context, 'review' );
189
			$item = $manager->get( $id, [], false );
190
191
			if( $item->getCustomerId() !== $context->getUserId() ) {
192
				throw new \Aimeos\Client\JsonApi\Exception( sprintf( 'Item not found' ), 404 );
193
			}
194
195
			$attr = (array) $payload->data->attributes;
196
			unset( $attr['review.response'], $attr['review.status'] );
197
198
			$manager->save( $item->fromArray( $attr )->setCustomerId( $context->getUserId() ) );
199
200
			$view->items = $item;
201
			$view->total = 1;
202
			$status = 200;
203
		}
204
		catch( \Aimeos\Controller\Frontend\Review\Exception $e )
205
		{
206
			$status = 403;
207
			$view->errors = $this->getErrorDetails( $e, 'controller/frontend' );
208
		}
209
		catch( \Aimeos\MShop\Exception $e )
210
		{
211
			$status = 404;
212
			$view->errors = $this->getErrorDetails( $e, 'mshop' );
213
		}
214
		catch( \Exception $e )
215
		{
216
			$status = $e->getCode() >= 100 && $e->getCode() < 600 ? $e->getCode() : 500;
217
			$view->errors = $this->getErrorDetails( $e );
218
		}
219
220
		return $this->render( $response, $view, $status );
221
	}
222
223
224
	/**
225
	 * Creates or updates the resource or the resource list
226
	 *
227
	 * @param \Psr\Http\Message\ServerRequestInterface $request Request object
228
	 * @param \Psr\Http\Message\ResponseInterface $response Response object
229
	 * @return \Psr\Http\Message\ResponseInterface Modified response object
230
	 */
231
	public function post( ServerRequestInterface $request, ResponseInterface $response ) : \Psr\Http\Message\ResponseInterface
232
	{
233
		$view = $this->getView();
234
		$context = $this->getContext();
235
236
		try
237
		{
238
			$body = (string) $request->getBody();
239
240
			if( ( $payload = json_decode( $body ) ) === null || !isset( $payload->data ) ) {
241
				throw new \Aimeos\Client\JsonApi\Exception( sprintf( 'Invalid JSON in body' ), 400 );
242
			}
243
244
			if( !is_array( $payload->data ) ) {
245
				$payload->data = [$payload->data];
246
			}
247
248
			$items = [];
249
			$manager = \Aimeos\MShop::create( $context, 'review' );
250
251
			foreach( $payload->data as $entry )
252
			{
253
				if( !isset( $entry->attributes ) ) {
254
					throw new \Aimeos\Client\JsonApi\Exception( sprintf( 'Attributes are missing', 400 ) );
255
				}
256
257
				$attr = (array) $entry->attributes;
258
				unset( $attr['review.response'], $attr['review.status'] );
259
260
				$items[] = $manager->create()->fromArray( $attr )->setCustomerId( $context->getUserId() );
261
			}
262
263
			$view->items = map( $manager->save( $items ) );
264
			$view->total = count( $view->items );
265
			$status = 201;
266
		}
267
		catch( \Aimeos\Controller\Frontend\Review\Exception $e )
268
		{
269
			$status = 403;
270
			$view->errors = $this->getErrorDetails( $e, 'controller/frontend' );
271
		}
272
		catch( \Aimeos\MShop\Exception $e )
273
		{
274
			$status = 404;
275
			$view->errors = $this->getErrorDetails( $e, 'mshop' );
276
		}
277
		catch( \Exception $e )
278
		{
279
			$status = $e->getCode() >= 100 && $e->getCode() < 600 ? $e->getCode() : 500;
280
			$view->errors = $this->getErrorDetails( $e );
281
		}
282
283
		return $this->render( $response, $view, $status );
284
	}
285
286
287
	/**
288
	 * Returns the available REST verbs and the available parameters
289
	 *
290
	 * @param \Psr\Http\Message\ServerRequestInterface $request Request object
291
	 * @param \Psr\Http\Message\ResponseInterface $response Response object
292
	 * @return \Psr\Http\Message\ResponseInterface Modified response object
293
	 */
294
	public function options( ServerRequestInterface $request, ResponseInterface $response ) : \Psr\Http\Message\ResponseInterface
295
	{
296
		$view = $this->getView();
297
298
		$view->attributes = [
299
			'review.domain' => [
300
				'label' => 'Domain of the reviewed item',
301
				'type' => 'string', 'default' => '', 'required' => true,
302
			],
303
			'review.refid' => [
304
				'label' => 'ID of the reviewd item',
305
				'type' => 'string', 'default' => '', 'required' => true,
306
			],
307
			'review.rating' => [
308
				'label' => 'Rating from 0 to 5',
309
				'type' => 'string', 'default' => '', 'required' => true,
310
			],
311
			'review.name' => [
312
				'label' => 'Name of the reviewer',
313
				'type' => 'string', 'default' => '', 'required' => false,
314
			],
315
			'review.comment' => [
316
				'label' => 'Comment for the rating',
317
				'type' => 'string', 'default' => '', 'required' => false,
318
			],
319
		];
320
321
		$tplconf = 'client/jsonapi/standard/template-options';
322
		$default = 'options-standard';
323
324
		$body = $view->render( $view->config( $tplconf, $default ) );
325
326
		return $response->withHeader( 'Allow', 'DELETE,GET,OPTIONS,PATCH,POST' )
327
			->withHeader( 'Cache-Control', 'max-age=300' )
328
			->withHeader( 'Content-Type', 'application/vnd.api+json' )
329
			->withBody( $view->response()->createStreamFromString( $body ) )
330
			->withStatus( 200 );
331
	}
332
333
334
	/**
335
	 * Returns the response object with the rendered header and body
336
	 *
337
	 * @param \Psr\Http\Message\ResponseInterface $response Response object
338
	 * @param \Aimeos\MW\View\Iface $view View instance
339
	 * @param int $status HTTP status code
340
	 * @return \Psr\Http\Message\ResponseInterface Modified response object
341
	 */
342
	protected function render( ResponseInterface $response, \Aimeos\MW\View\Iface $view, int $status ) : \Psr\Http\Message\ResponseInterface
343
	{
344
		/** client/jsonapi/customer/review/standard/template
345
		 * Relative path to the customer review JSON API template
346
		 *
347
		 * The template file contains the code and processing instructions
348
		 * to generate the result shown in the JSON API body. The
349
		 * configuration string is the path to the template file relative
350
		 * to the templates directory (usually in client/jsonapi/templates).
351
		 *
352
		 * You can overwrite the template file configuration in extensions and
353
		 * provide alternative templates. These alternative templates should be
354
		 * named like the default one but with the string "standard" replaced by
355
		 * an unique name. You may use the name of your project for this. If
356
		 * you've implemented an alternative client class as well, "standard"
357
		 * should be replaced by the name of the new class.
358
		 *
359
		 * @param string Relative path to the template creating the body for the JSON API
360
		 * @since 2017.07
361
		 * @category Developer
362
		 */
363
		$tplconf = 'client/jsonapi/customer/review/standard/template';
364
		$default = 'customer/review/standard';
365
366
		$view->customerid = $this->getContext()->getUserId();
367
		$body = $view->render( $view->config( $tplconf, $default ) );
368
369
		return $response->withHeader( 'Allow', 'DELETE,GET,OPTIONS,PATCH,POST' )
370
			->withHeader( 'Cache-Control', 'no-cache, private' )
371
			->withHeader( 'Content-Type', 'application/vnd.api+json' )
372
			->withBody( $view->response()->createStreamFromString( $body ) )
373
			->withStatus( $status );
374
	}
375
}
376