Standard   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 350
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 77
dl 0
loc 350
rs 10
c 0
b 0
f 0
wmc 15

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getController() 0 12 1
B get() 0 33 8
A getItem() 0 8 1
A aggregate() 0 7 1
A render() 0 58 2
A options() 0 37 1
A getItems() 0 8 1
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2020-2025
6
 * @package Client
7
 * @subpackage JsonApi
8
 */
9
10
11
namespace Aimeos\Client\JsonApi\Review;
12
13
use Psr\Http\Message\ResponseInterface;
14
use Psr\Http\Message\ServerRequestInterface;
15
16
17
/**
18
 * JSON API standard 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
	/** client/jsonapi/review/name
28
	 * Class name of the used review client implementation
29
	 *
30
	 * Each default JSON API client can be replace by an alternative imlementation.
31
	 * To use this implementation, you have to set the last part of the class
32
	 * name as configuration value so the client factory knows which class it
33
	 * has to instantiate.
34
	 *
35
	 * For example, if the name of the default class is
36
	 *
37
	 *  \Aimeos\Client\JsonApi\Review\Standard
38
	 *
39
	 * and you want to replace it with your own version named
40
	 *
41
	 *  \Aimeos\Client\JsonApi\Review\Myreview
42
	 *
43
	 * then you have to set the this configuration option:
44
	 *
45
	 *  client/jsonapi/review/name = Myreview
46
	 *
47
	 * The value is the last part of your own class name and it's case sensitive,
48
	 * so take care that the configuration value is exactly named like the last
49
	 * part of the class name.
50
	 *
51
	 * The allowed characters of the class name are A-Z, a-z and 0-9. No other
52
	 * characters are possible! You should always start the last part of the class
53
	 * name with an upper case character and continue only with lower case characters
54
	 * or numbers. Avoid chamel case names like "MyReview"!
55
	 *
56
	 * @param string Last part of the class name
57
	 * @since 2017.03
58
	 * @category Developer
59
	 */
60
61
	/** client/jsonapi/review/decorators/excludes
62
	 * Excludes decorators added by the "common" option from the JSON API clients
63
	 *
64
	 * Decorators extend the functionality of a class by adding new aspects
65
	 * (e.g. log what is currently done), executing the methods of the underlying
66
	 * class only in certain conditions (e.g. only for logged in users) or
67
	 * modify what is returned to the caller.
68
	 *
69
	 * This option allows you to remove a decorator added via
70
	 * "client/jsonapi/common/decorators/default" before they are wrapped
71
	 * around the JsonApi client.
72
	 *
73
	 *  client/jsonapi/decorators/excludes = array( 'decorator1' )
74
	 *
75
	 * This would remove the decorator named "decorator1" from the list of
76
	 * common decorators ("\Aimeos\Client\JsonApi\Common\Decorator\*") added via
77
	 * "client/jsonapi/common/decorators/default" for the JSON API client.
78
	 *
79
	 * @param array List of decorator names
80
	 * @since 2020.10
81
	 * @category Developer
82
	 * @see client/jsonapi/common/decorators/default
83
	 * @see client/jsonapi/review/decorators/global
84
	 * @see client/jsonapi/review/decorators/local
85
	 */
86
87
	/** client/jsonapi/review/decorators/global
88
	 * Adds a list of globally available decorators only to the JsonApi client
89
	 *
90
	 * Decorators extend the functionality of a class by adding new aspects
91
	 * (e.g. log what is currently done), executing the methods of the underlying
92
	 * class only in certain conditions (e.g. only for logged in users) or
93
	 * modify what is returned to the caller.
94
	 *
95
	 * This option allows you to wrap global decorators
96
	 * ("\Aimeos\Client\JsonApi\Common\Decorator\*") around the JsonApi
97
	 * client.
98
	 *
99
	 *  client/jsonapi/review/decorators/global = array( 'decorator1' )
100
	 *
101
	 * This would add the decorator named "decorator1" defined by
102
	 * "\Aimeos\Client\JsonApi\Common\Decorator\Decorator1" only to the
103
	 * "review" JsonApi client.
104
	 *
105
	 * @param array List of decorator names
106
	 * @since 2020.10
107
	 * @category Developer
108
	 * @see client/jsonapi/common/decorators/default
109
	 * @see client/jsonapi/review/decorators/excludes
110
	 * @see client/jsonapi/review/decorators/local
111
	 */
112
113
	/** client/jsonapi/review/decorators/local
114
	 * Adds a list of local decorators only to the JsonApi client
115
	 *
116
	 * Decorators extend the functionality of a class by adding new aspects
117
	 * (e.g. log what is currently done), executing the methods of the underlying
118
	 * class only in certain conditions (e.g. only for logged in users) or
119
	 * modify what is returned to the caller.
120
	 *
121
	 * This option allows you to wrap local decorators
122
	 * ("\Aimeos\Client\JsonApi\Review\Decorator\*") around the JsonApi
123
	 * client.
124
	 *
125
	 *  client/jsonapi/review/decorators/local = array( 'decorator2' )
126
	 *
127
	 * This would add the decorator named "decorator2" defined by
128
	 * "\Aimeos\Client\JsonApi\Review\Decorator\Decorator2" only to the
129
	 * "review" JsonApi client.
130
	 *
131
	 * @param array List of decorator names
132
	 * @since 2020.10
133
	 * @category Developer
134
	 * @see client/jsonapi/common/decorators/default
135
	 * @see client/jsonapi/review/decorators/excludes
136
	 * @see client/jsonapi/review/decorators/global
137
	 */
138
139
140
	/**
141
	 * Returns the resource or the resource list
142
	 *
143
	 * @param \Psr\Http\Message\ServerRequestInterface $request Request object
144
	 * @param \Psr\Http\Message\ResponseInterface $response Response object
145
	 * @return \Psr\Http\Message\ResponseInterface Modified response object
146
	 */
147
	public function get( ServerRequestInterface $request, ResponseInterface $response ) : \Psr\Http\Message\ResponseInterface
148
	{
149
		$view = $this->view();
150
151
		try
152
		{
153
			if( $view->param( 'aggregate' ) != '' ) {
154
				$response = $this->aggregate( $view, $request, $response );
155
			} elseif( $view->param( 'id' ) != '' ) {
156
				$response = $this->getItem( $view, $request, $response );
157
			} else {
158
				$response = $this->getItems( $view, $request, $response );
159
			}
160
161
			$status = 200;
162
		}
163
		catch( \Aimeos\Controller\Frontend\Exception $e )
164
		{
165
			$status = 403;
166
			$view->errors = $this->getErrorDetails( $e, 'controller/frontend' );
167
		}
168
		catch( \Aimeos\MShop\Exception $e )
169
		{
170
			$status = 404;
171
			$view->errors = $this->getErrorDetails( $e, 'mshop' );
172
		}
173
		catch( \Exception $e )
174
		{
175
			$status = $e->getCode() >= 100 && $e->getCode() < 600 ? $e->getCode() : 500;
176
			$view->errors = $this->getErrorDetails( $e );
177
		}
178
179
		return $this->render( $response, $view, $status );
180
	}
181
182
183
	/**
184
	 * Returns the available REST verbs and the available parameters
185
	 *
186
	 * @param \Psr\Http\Message\ServerRequestInterface $request Request object
187
	 * @param \Psr\Http\Message\ResponseInterface $response Response object
188
	 * @return \Psr\Http\Message\ResponseInterface Modified response object
189
	 */
190
	public function options( ServerRequestInterface $request, ResponseInterface $response ) : \Psr\Http\Message\ResponseInterface
191
	{
192
		$view = $this->view();
193
		$view->attributes = [];
194
195
		$view->filter = [
196
			'f_domain' => [
197
				'label' => 'Return reviews for that domain, e.g. "product"',
198
				'type' => 'string', 'default' => '', 'required' => true,
199
			],
200
			'f_refid' => [
201
				'label' => 'Return reviews for the ID of the specified domain',
202
				'type' => 'string', 'default' => '', 'required' => true,
203
			],
204
		];
205
206
		$view->sort = [
207
			'ctime' => [
208
				'label' => 'Sort reviews by creation date/time',
209
				'type' => 'string', 'default' => false, 'required' => false,
210
			],
211
			'rating' => [
212
				'label' => 'Sort reviews by rating (ascending, "-rating" for descending)',
213
				'type' => 'string', 'default' => false, 'required' => false,
214
			],
215
		];
216
217
		$tplconf = 'client/jsonapi/template-options';
218
		$default = 'options-standard';
219
220
		$body = $view->render( $view->config( $tplconf, $default ) );
221
222
		return $response->withHeader( 'Allow', 'GET,OPTIONS' )
223
			->withHeader( 'Cache-Control', 'max-age=300' )
224
			->withHeader( 'Content-Type', 'application/vnd.api+json' )
225
			->withBody( $view->response()->createStreamFromString( $body ) )
226
			->withStatus( 200 );
0 ignored issues
show
Bug introduced by
The method withStatus() does not exist on Psr\Http\Message\MessageInterface. It seems like you code against a sub-type of Psr\Http\Message\MessageInterface such as Psr\Http\Message\ResponseInterface or Aimeos\Base\View\Helper\Request\Standard. ( Ignorable by Annotation )

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

226
			->/** @scrutinizer ignore-call */ withStatus( 200 );
Loading history...
227
	}
228
229
230
	/**
231
	 * Counts the number of products for the requested key
232
	 *
233
	 * @param \Aimeos\Base\View\Iface $view View instance
234
	 * @param \Psr\Http\Message\ServerRequestInterface $request Request object
235
	 * @param \Psr\Http\Message\ResponseInterface $response Response object
236
	 * @return \Psr\Http\Message\ResponseInterface Modified response object
237
	 */
238
	protected function aggregate( \Aimeos\Base\View\Iface $view, ServerRequestInterface $request, ResponseInterface $response ) : \Psr\Http\Message\ResponseInterface
239
	{
240
		$view->data = $this->getController( $view )->sort()
241
			->slice( $view->param( 'page/offset', 0 ), $view->param( 'page/limit', 10000 ) )
242
			->aggregate( $view->param( 'aggregate' ) );
243
244
		return $response;
245
	}
246
247
248
	/**
249
	 * Returns the initialized product controller
250
	 *
251
	 * @param \Aimeos\Base\View\Iface $view View instance
252
	 * @return \Aimeos\Controller\Frontend\Product\Iface Initialized product controller
253
	 */
254
	protected function getController( \Aimeos\Base\View\Iface $view )
255
	{
256
		$context = $this->context();
257
		$cntl = \Aimeos\Controller\Frontend::create( $context, 'review' );
258
259
		$cntl->for( $view->param( 'filter/f_domain', 'product' ), $view->param( 'filter/f_refid' ) );
260
261
		$params = (array) $view->param( 'filter', [] );
262
		unset( $params['f_domain'], $params['f_refid'] );
263
264
		return $cntl->sort( $view->param( 'sort', '-ctime' ) )->parse( $params )
265
			->slice( $view->param( 'page/offset', 0 ), $view->param( 'page/limit', 10 ) );
266
	}
267
268
269
	/**
270
	 * Retrieves the item and adds the data to the view
271
	 *
272
	 * @param \Aimeos\Base\View\Iface $view View instance
273
	 * @param \Psr\Http\Message\ServerRequestInterface $request Request object
274
	 * @param \Psr\Http\Message\ResponseInterface $response Response object
275
	 * @return \Psr\Http\Message\ResponseInterface Modified response object
276
	 */
277
	protected function getItem( \Aimeos\Base\View\Iface $view, ServerRequestInterface $request, ResponseInterface $response ) : \Psr\Http\Message\ResponseInterface
278
	{
279
		$cntl = \Aimeos\Controller\Frontend::create( $this->context(), 'review' );
280
281
		$view->items = $cntl->get( $view->param( 'id' ) );
282
		$view->total = 1;
283
284
		return $response;
285
	}
286
287
288
	/**
289
	 * Retrieves the items and adds the data to the view
290
	 *
291
	 * @param \Aimeos\Base\View\Iface $view View instance
292
	 * @param \Psr\Http\Message\ServerRequestInterface $request Request object
293
	 * @param \Psr\Http\Message\ResponseInterface $response Response object
294
	 * @return \Psr\Http\Message\ResponseInterface Modified response object
295
	 */
296
	protected function getItems( \Aimeos\Base\View\Iface $view, ServerRequestInterface $request, ResponseInterface $response ) : \Psr\Http\Message\ResponseInterface
297
	{
298
		$total = 0;
299
300
		$view->items = $this->getController( $view )->search( $total );
301
		$view->total = $total;
302
303
		return $response;
304
	}
305
306
307
	/**
308
	 * Returns the response object with the rendered header and body
309
	 *
310
	 * @param \Psr\Http\Message\ResponseInterface $response Response object
311
	 * @param \Aimeos\Base\View\Iface $view View instance
312
	 * @param integer $status HTTP status code
313
	 * @return \Psr\Http\Message\ResponseInterface Modified response object
314
	 */
315
	protected function render( ResponseInterface $response, \Aimeos\Base\View\Iface $view, $status ) : \Psr\Http\Message\ResponseInterface
316
	{
317
		if( $view->param( 'aggregate' ) != '' )
318
		{
319
			/** client/jsonapi/review/template-aggregate
320
			 * Relative path to the review aggregate JSON API template
321
			 *
322
			 * The template file contains the code and processing instructions
323
			 * to generate the result shown in the JSON API body. The
324
			 * configuration string is the path to the template file relative
325
			 * to the templates directory (usually in templates/client/jsonapi).
326
			 *
327
			 * You can overwrite the template file configuration in extensions and
328
			 * provide alternative templates. These alternative templates should be
329
			 * named like the default one but with the string "standard" replaced by
330
			 * an unique name. You may use the name of your project for this. If
331
			 * you've implemented an alternative client class as well, "standard"
332
			 * should be replaced by the name of the new class.
333
			 *
334
			 * @param string Relative path to the template creating the list of aggregated review counts
335
			 * @since 2020.10
336
			 * @category Developer
337
			 */
338
			$tplconf = 'client/jsonapi/review/template-aggregate';
339
			$default = 'aggregate-standard';
340
		}
341
		else
342
		{
343
			/** client/jsonapi/review/template
344
			 * Relative path to the review JSON API template
345
			 *
346
			 * The template file contains the code and processing instructions
347
			 * to generate the result shown in the JSON API body. The
348
			 * configuration string is the path to the template file relative
349
			 * to the templates directory (usually in templates/client/jsonapi).
350
			 *
351
			 * You can overwrite the template file configuration in extensions and
352
			 * provide alternative templates. These alternative templates should be
353
			 * named like the default one but with the string "standard" replaced by
354
			 * an unique name. You may use the name of your project for this. If
355
			 * you've implemented an alternative client class as well, "standard"
356
			 * should be replaced by the name of the new class.
357
			 *
358
			 * @param string Relative path to the template creating the body of the JSON API
359
			 * @since 2017.03
360
			 * @category Developer
361
			 */
362
			$tplconf = 'client/jsonapi/review/template';
363
			$default = 'review/standard';
364
		}
365
366
		$body = $view->render( $view->config( $tplconf, $default ) );
367
368
		return $response->withHeader( 'Allow', 'GET,OPTIONS' )
369
			->withHeader( 'Cache-Control', 'max-age=300' )
370
			->withHeader( 'Content-Type', 'application/vnd.api+json' )
371
			->withBody( $view->response()->createStreamFromString( $body ) )
372
			->withStatus( $status );
373
	}
374
}
375