Passed
Push — master ( 842b57...5350bc )
by Aimeos
02:53
created

Standard::getItem()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
c 0
b 0
f 0
dl 0
loc 8
rs 10
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\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
	/**
28
	 * Returns 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 get( ServerRequestInterface $request, ResponseInterface $response ) : \Psr\Http\Message\ResponseInterface
35
	{
36
		$view = $this->getView();
37
38
		try
39
		{
40
			if( $view->param( 'aggregate' ) != '' ) {
41
				$response = $this->aggregate( $view, $request, $response );
42
			} elseif( $view->param( 'id' ) != '' ) {
43
				$response = $this->getItem( $view, $request, $response );
44
			} else {
45
				$response = $this->getItems( $view, $request, $response );
46
			}
47
48
			$status = 200;
49
		}
50
		catch( \Aimeos\Controller\Frontend\Exception $e )
51
		{
52
			$status = 403;
53
			$view->errors = $this->getErrorDetails( $e, 'controller/frontend' );
54
		}
55
		catch( \Aimeos\MShop\Exception $e )
56
		{
57
			$status = 404;
58
			$view->errors = $this->getErrorDetails( $e, 'mshop' );
59
		}
60
		catch( \Exception $e )
61
		{
62
			$status = $e->getCode() >= 100 && $e->getCode() < 600 ? $e->getCode() : 500;
63
			$view->errors = $this->getErrorDetails( $e );
64
		}
65
66
		return $this->render( $response, $view, $status );
67
	}
68
69
70
	/**
71
	 * Returns the available REST verbs and the available parameters
72
	 *
73
	 * @param \Psr\Http\Message\ServerRequestInterface $request Request object
74
	 * @param \Psr\Http\Message\ResponseInterface $response Response object
75
	 * @return \Psr\Http\Message\ResponseInterface Modified response object
76
	 */
77
	public function options( ServerRequestInterface $request, ResponseInterface $response ) : \Psr\Http\Message\ResponseInterface
78
	{
79
		$view = $this->getView();
80
		$view->attributes = [];
81
82
		$view->filter = [
83
			'f_domain' => [
84
				'label' => 'Return reviews for that domain, e.g. "product"',
85
				'type' => 'string', 'default' => '', 'required' => true,
86
			],
87
			'f_refid' => [
88
				'label' => 'Return reviews for the ID of the specified domain',
89
				'type' => 'string', 'default' => '', 'required' => true,
90
			],
91
		];
92
93
		$view->sort = [
94
			'ctime' => [
95
				'label' => 'Sort reviews by creation date/time',
96
				'type' => 'string', 'default' => false, 'required' => false,
97
			],
98
			'rating' => [
99
				'label' => 'Sort reviews by rating (ascending, "-rating" for descending)',
100
				'type' => 'string', 'default' => false, 'required' => false,
101
			],
102
		];
103
104
		$tplconf = 'client/jsonapi/standard/template-options';
105
		$default = 'options-standard';
106
107
		$body = $view->render( $view->config( $tplconf, $default ) );
108
109
		return $response->withHeader( 'Allow', 'GET,OPTIONS' )
110
			->withHeader( 'Cache-Control', 'max-age=300' )
111
			->withHeader( 'Content-Type', 'application/vnd.api+json' )
112
			->withBody( $view->response()->createStreamFromString( $body ) )
113
			->withStatus( 200 );
114
	}
115
116
117
	/**
118
	 * Counts the number of products for the requested key
119
	 *
120
	 * @param \Aimeos\MW\View\Iface $view View instance
121
	 * @param \Psr\Http\Message\ServerRequestInterface $request Request object
122
	 * @param \Psr\Http\Message\ResponseInterface $response Response object
123
	 * @return \Psr\Http\Message\ResponseInterface Modified response object
124
	 */
125
	protected function aggregate( \Aimeos\MW\View\Iface $view, ServerRequestInterface $request, ResponseInterface $response ) : \Psr\Http\Message\ResponseInterface
126
	{
127
		$view->data = $this->getController( $view )->sort()
128
			->slice( $view->param( 'page/offset', 0 ), $view->param( 'page/limit', 10000 ) )
129
			->aggregate( $view->param( 'aggregate' ) );
130
131
		return $response;
132
	}
133
134
135
	/**
136
	 * Returns the initialized product controller
137
	 *
138
	 * @param \Aimeos\MW\View\Iface $view View instance
139
	 * @return \Aimeos\Controller\Frontend\Product\Iface Initialized product controller
140
	 */
141
	protected function getController( \Aimeos\MW\View\Iface $view )
142
	{
143
		$context = $this->getContext();
144
		$cntl = \Aimeos\Controller\Frontend::create( $context, 'review' );
145
146
		$cntl->for( $view->param( 'filter/f_domain', 'product' ), $view->param( 'filter/f_refid' ) );
147
148
		$params = (array) $view->param( 'filter', [] );
149
		unset( $params['f_domain'], $params['f_refid'] );
150
151
		return $cntl->parse( $params )
152
			->sort( $view->param( 'sort', '-ctime' ) )
153
			->slice( $view->param( 'page/offset', 0 ), $view->param( 'page/limit', 10 ) );
154
	}
155
156
157
	/**
158
	 * Retrieves the item and adds the data to the view
159
	 *
160
	 * @param \Aimeos\MW\View\Iface $view View instance
161
	 * @param \Psr\Http\Message\ServerRequestInterface $request Request object
162
	 * @param \Psr\Http\Message\ResponseInterface $response Response object
163
	 * @return \Psr\Http\Message\ResponseInterface Modified response object
164
	 */
165
	protected function getItem( \Aimeos\MW\View\Iface $view, ServerRequestInterface $request, ResponseInterface $response ) : \Psr\Http\Message\ResponseInterface
166
	{
167
		$cntl = \Aimeos\Controller\Frontend::create( $this->getContext(), 'review' );
168
169
		$view->items = $cntl->get( $view->param( 'id' ) );
170
		$view->total = 1;
171
172
		return $response;
173
	}
174
175
176
	/**
177
	 * Retrieves the items and adds the data to the view
178
	 *
179
	 * @param \Aimeos\MW\View\Iface $view View instance
180
	 * @param \Psr\Http\Message\ServerRequestInterface $request Request object
181
	 * @param \Psr\Http\Message\ResponseInterface $response Response object
182
	 * @return \Psr\Http\Message\ResponseInterface Modified response object
183
	 */
184
	protected function getItems( \Aimeos\MW\View\Iface $view, ServerRequestInterface $request, ResponseInterface $response ) : \Psr\Http\Message\ResponseInterface
185
	{
186
		$total = 0;
187
188
		$view->items = $this->getController( $view )->search( $total );
189
		$view->total = $total;
190
191
		return $response;
192
	}
193
194
195
	/**
196
	 * Returns the response object with the rendered header and body
197
	 *
198
	 * @param \Psr\Http\Message\ResponseInterface $response Response object
199
	 * @param \Aimeos\MW\View\Iface $view View instance
200
	 * @param integer $status HTTP status code
201
	 * @return \Psr\Http\Message\ResponseInterface Modified response object
202
	 */
203
	protected function render( ResponseInterface $response, \Aimeos\MW\View\Iface $view, $status ) : \Psr\Http\Message\ResponseInterface
204
	{
205
		if( $view->param( 'aggregate' ) != '' )
206
		{
207
			/** client/jsonapi/review/standard/template-aggregate
208
			 * Relative path to the review aggregate JSON API template
209
			 *
210
			 * The template file contains the code and processing instructions
211
			 * to generate the result shown in the JSON API body. The
212
			 * configuration string is the path to the template file relative
213
			 * to the templates directory (usually in client/jsonapi/templates).
214
			 *
215
			 * You can overwrite the template file configuration in extensions and
216
			 * provide alternative templates. These alternative templates should be
217
			 * named like the default one but with the string "standard" replaced by
218
			 * an unique name. You may use the name of your project for this. If
219
			 * you've implemented an alternative client class as well, "standard"
220
			 * should be replaced by the name of the new class.
221
			 *
222
			 * @param string Relative path to the template creating the list of aggregated review counts
223
			 * @since 2020.10
224
			 * @category Developer
225
			 */
226
			$tplconf = 'client/jsonapi/review/standard/template-aggregate';
227
			$default = 'aggregate-standard';
228
		}
229
		else
230
		{
231
			/** client/jsonapi/review/standard/template
232
			 * Relative path to the review JSON API template
233
			 *
234
			 * The template file contains the code and processing instructions
235
			 * to generate the result shown in the JSON API body. The
236
			 * configuration string is the path to the template file relative
237
			 * to the templates directory (usually in client/jsonapi/templates).
238
			 *
239
			 * You can overwrite the template file configuration in extensions and
240
			 * provide alternative templates. These alternative templates should be
241
			 * named like the default one but with the string "standard" replaced by
242
			 * an unique name. You may use the name of your project for this. If
243
			 * you've implemented an alternative client class as well, "standard"
244
			 * should be replaced by the name of the new class.
245
			 *
246
			 * @param string Relative path to the template creating the body of the JSON API
247
			 * @since 2017.03
248
			 * @category Developer
249
			 */
250
			$tplconf = 'client/jsonapi/review/standard/template';
251
			$default = 'review/standard';
252
		}
253
254
		$body = $view->render( $view->config( $tplconf, $default ) );
255
256
		return $response->withHeader( 'Allow', 'GET,OPTIONS' )
257
			->withHeader( 'Cache-Control', 'max-age=300' )
258
			->withHeader( 'Content-Type', 'application/vnd.api+json' )
259
			->withBody( $view->response()->createStreamFromString( $body ) )
260
			->withStatus( $status );
261
	}
262
}
263