Passed
Push — master ( 926a33...3dcbbf )
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() ?: 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() ?: 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
			$filter = $manager->filter()->add( 'review.customerid', '==', $context->getUserId() );
190
			$item = $manager->get( $id, [], false, $filter );
0 ignored issues
show
Unused Code introduced by
The call to Aimeos\MShop\Common\Manager\Iface::get() has too many arguments starting with $filter. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

190
			/** @scrutinizer ignore-call */ 
191
   $item = $manager->get( $id, [], false, $filter );

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
191
192
			$attr = (array) $payload->data->attributes;
193
			unset( $attr['review.response'], $attr['review.status'] );
194
195
			$manager->save( $item->fromArray( $attr )->setCustomerId( $context->getUserId() ) );
196
197
			$view->items = $item;
198
			$view->total = 1;
199
			$status = 200;
200
		}
201
		catch( \Aimeos\Controller\Frontend\Review\Exception $e )
202
		{
203
			$status = 403;
204
			$view->errors = $this->getErrorDetails( $e, 'controller/frontend' );
205
		}
206
		catch( \Aimeos\MShop\Exception $e )
207
		{
208
			$status = 404;
209
			$view->errors = $this->getErrorDetails( $e, 'mshop' );
210
		}
211
		catch( \Exception $e )
212
		{
213
			$status = $e->getCode() ?: 500;
214
			$view->errors = $this->getErrorDetails( $e );
215
		}
216
217
		return $this->render( $response, $view, $status );
218
	}
219
220
221
	/**
222
	 * Creates or updates the resource or the resource list
223
	 *
224
	 * @param \Psr\Http\Message\ServerRequestInterface $request Request object
225
	 * @param \Psr\Http\Message\ResponseInterface $response Response object
226
	 * @return \Psr\Http\Message\ResponseInterface Modified response object
227
	 */
228
	public function post( ServerRequestInterface $request, ResponseInterface $response ) : \Psr\Http\Message\ResponseInterface
229
	{
230
		$view = $this->getView();
231
		$context = $this->getContext();
232
233
		try
234
		{
235
			$body = (string) $request->getBody();
236
237
			if( ( $payload = json_decode( $body ) ) === null || !isset( $payload->data ) ) {
238
				throw new \Aimeos\Client\JsonApi\Exception( sprintf( 'Invalid JSON in body' ), 400 );
239
			}
240
241
			if( !is_array( $payload->data ) ) {
242
				$payload->data = [$payload->data];
243
			}
244
245
			$items = [];
246
			$manager = \Aimeos\MShop::create( $context, 'review' );
247
248
			foreach( $payload->data as $entry )
249
			{
250
				if( !isset( $entry->attributes ) ) {
251
					throw new \Aimeos\Client\JsonApi\Exception( sprintf( 'Attributes are missing', 400 ) );
252
				}
253
254
				$attr = (array) $entry->attributes;
255
				unset( $attr['review.response'], $attr['review.status'] );
256
257
				$items[] = $manager->create()->fromArray( $attr )->setCustomerId( $context->getUserId() );
258
			}
259
260
			$view->items = map( $manager->save( $items ) );
261
			$view->total = count( $view->items );
262
			$status = 201;
263
		}
264
		catch( \Aimeos\Controller\Frontend\Review\Exception $e )
265
		{
266
			$status = 403;
267
			$view->errors = $this->getErrorDetails( $e, 'controller/frontend' );
268
		}
269
		catch( \Aimeos\MShop\Exception $e )
270
		{
271
			$status = 404;
272
			$view->errors = $this->getErrorDetails( $e, 'mshop' );
273
		}
274
		catch( \Exception $e )
275
		{
276
			$status = $e->getCode() ?: 500;
277
			$view->errors = $this->getErrorDetails( $e );
278
		}
279
280
		return $this->render( $response, $view, $status );
281
	}
282
283
284
	/**
285
	 * Returns the available REST verbs and the available parameters
286
	 *
287
	 * @param \Psr\Http\Message\ServerRequestInterface $request Request object
288
	 * @param \Psr\Http\Message\ResponseInterface $response Response object
289
	 * @return \Psr\Http\Message\ResponseInterface Modified response object
290
	 */
291
	public function options( ServerRequestInterface $request, ResponseInterface $response ) : \Psr\Http\Message\ResponseInterface
292
	{
293
		$view = $this->getView();
294
295
		$view->attributes = [
296
			'review.domain' => [
297
				'label' => 'Domain of the reviewed item',
298
				'type' => 'string', 'default' => '', 'required' => true,
299
			],
300
			'review.refid' => [
301
				'label' => 'ID of the reviewd item',
302
				'type' => 'string', 'default' => '', 'required' => true,
303
			],
304
			'review.rating' => [
305
				'label' => 'Rating from 0 to 5',
306
				'type' => 'string', 'default' => '', 'required' => true,
307
			],
308
			'review.name' => [
309
				'label' => 'Name of the reviewer',
310
				'type' => 'string', 'default' => '', 'required' => false,
311
			],
312
			'review.comment' => [
313
				'label' => 'Comment for the rating',
314
				'type' => 'string', 'default' => '', 'required' => false,
315
			],
316
		];
317
318
		$tplconf = 'client/jsonapi/standard/template-options';
319
		$default = 'options-standard';
320
321
		$body = $view->render( $view->config( $tplconf, $default ) );
322
323
		return $response->withHeader( 'Allow', 'DELETE,GET,OPTIONS,PATCH,POST' )
324
			->withHeader( 'Cache-Control', 'max-age=300' )
325
			->withHeader( 'Content-Type', 'application/vnd.api+json' )
326
			->withBody( $view->response()->createStreamFromString( $body ) )
327
			->withStatus( 200 );
328
	}
329
330
331
	/**
332
	 * Returns the response object with the rendered header and body
333
	 *
334
	 * @param \Psr\Http\Message\ResponseInterface $response Response object
335
	 * @param \Aimeos\MW\View\Iface $view View instance
336
	 * @param int $status HTTP status code
337
	 * @return \Psr\Http\Message\ResponseInterface Modified response object
338
	 */
339
	protected function render( ResponseInterface $response, \Aimeos\MW\View\Iface $view, int $status ) : \Psr\Http\Message\ResponseInterface
340
	{
341
		/** client/jsonapi/customer/review/standard/template
342
		 * Relative path to the customer review JSON API template
343
		 *
344
		 * The template file contains the code and processing instructions
345
		 * to generate the result shown in the JSON API body. The
346
		 * configuration string is the path to the template file relative
347
		 * to the templates directory (usually in client/jsonapi/templates).
348
		 *
349
		 * You can overwrite the template file configuration in extensions and
350
		 * provide alternative templates. These alternative templates should be
351
		 * named like the default one but with the string "standard" replaced by
352
		 * an unique name. You may use the name of your project for this. If
353
		 * you've implemented an alternative client class as well, "standard"
354
		 * should be replaced by the name of the new class.
355
		 *
356
		 * @param string Relative path to the template creating the body for the JSON API
357
		 * @since 2017.07
358
		 * @category Developer
359
		 */
360
		$tplconf = 'client/jsonapi/customer/review/standard/template';
361
		$default = 'customer/review/standard';
362
363
		$view->customerid = $this->getContext()->getUserId();
364
		$body = $view->render( $view->config( $tplconf, $default ) );
365
366
		return $response->withHeader( 'Allow', 'DELETE,GET,OPTIONS,PATCH,POST' )
367
			->withHeader( 'Cache-Control', 'no-cache, private' )
368
			->withHeader( 'Content-Type', 'application/vnd.api+json' )
369
			->withBody( $view->response()->createStreamFromString( $body ) )
370
			->withStatus( $status );
371
	}
372
}
373