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

Standard   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 149
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
eloc 63
c 1
b 0
f 0
dl 0
loc 149
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
B get() 0 49 7
A render() 0 31 1
A options() 0 37 1
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
			$cntl = \Aimeos\Controller\Frontend::create( $this->getContext(), 'review' );
41
42
			if( ( $id = $view->param( 'id' ) ) != '' )
43
			{
44
				$view->items = $cntl->get( $id );
45
				$view->total = 1;
46
			}
47
			else
48
			{
49
				$total = 0;
50
				$cntl->for( $view->param( 'filter/f_domain', 'product' ), $view->param( 'filter/f_refid' ) );
51
52
				$params = (array) $view->param( 'filter', [] );
53
				unset( $params['f_domain'], $params['f_refid'] );
54
55
				$items = $cntl->slice( $view->param( 'page/offset', 0 ), $view->param( 'page/limit', 25 ) )
56
					->sort( $view->param( 'sort', '-ctime' ) )
57
					->parse( $params )
58
					->search( $total );
59
60
				$view->items = $items;
61
				$view->total = $total;
62
			}
63
64
			$status = 200;
65
		}
66
		catch( \Aimeos\Controller\Frontend\Exception $e )
67
		{
68
			$status = 403;
69
			$view->errors = $this->getErrorDetails( $e, 'controller/frontend' );
70
		}
71
		catch( \Aimeos\MShop\Exception $e )
72
		{
73
			$status = 404;
74
			$view->errors = $this->getErrorDetails( $e, 'mshop' );
75
		}
76
		catch( \Exception $e )
77
		{
78
			$status = $e->getCode() >= 100 && $e->getCode() < 600 ? $e->getCode() : 500;
79
			$view->errors = $this->getErrorDetails( $e );
80
		}
81
82
		return $this->render( $response, $view, $status );
83
	}
84
85
86
	/**
87
	 * Returns the available REST verbs and the available parameters
88
	 *
89
	 * @param \Psr\Http\Message\ServerRequestInterface $request Request object
90
	 * @param \Psr\Http\Message\ResponseInterface $response Response object
91
	 * @return \Psr\Http\Message\ResponseInterface Modified response object
92
	 */
93
	public function options( ServerRequestInterface $request, ResponseInterface $response ) : \Psr\Http\Message\ResponseInterface
94
	{
95
		$view = $this->getView();
96
		$view->attributes = [];
97
98
		$view->filter = [
99
			'f_domain' => [
100
				'label' => 'Return reviews for that domain, e.g. "product"',
101
				'type' => 'string', 'default' => '', 'required' => true,
102
			],
103
			'f_refid' => [
104
				'label' => 'Return reviews for the ID of the specified domain',
105
				'type' => 'string', 'default' => '', 'required' => true,
106
			],
107
		];
108
109
		$view->sort = [
110
			'ctime' => [
111
				'label' => 'Sort reviews by creation date/time',
112
				'type' => 'string', 'default' => false, 'required' => false,
113
			],
114
			'rating' => [
115
				'label' => 'Sort reviews by rating (ascending, "-rating" for descending)',
116
				'type' => 'string', 'default' => false, 'required' => false,
117
			],
118
		];
119
120
		$tplconf = 'client/jsonapi/standard/template-options';
121
		$default = 'options-standard';
122
123
		$body = $view->render( $view->config( $tplconf, $default ) );
124
125
		return $response->withHeader( 'Allow', 'GET,OPTIONS' )
126
			->withHeader( 'Cache-Control', 'max-age=300' )
127
			->withHeader( 'Content-Type', 'application/vnd.api+json' )
128
			->withBody( $view->response()->createStreamFromString( $body ) )
129
			->withStatus( 200 );
130
	}
131
132
133
	/**
134
	 * Returns the response object with the rendered header and body
135
	 *
136
	 * @param \Psr\Http\Message\ResponseInterface $response Response object
137
	 * @param \Aimeos\MW\View\Iface $view View instance
138
	 * @param integer $status HTTP status code
139
	 * @return \Psr\Http\Message\ResponseInterface Modified response object
140
	 */
141
	protected function render( ResponseInterface $response, \Aimeos\MW\View\Iface $view, $status ) : \Psr\Http\Message\ResponseInterface
142
	{
143
		/** client/jsonapi/review/standard/template
144
		 * Relative path to the review JSON API template
145
		 *
146
		 * The template file contains the code and processing instructions
147
		 * to generate the result shown in the JSON API body. The
148
		 * configuration string is the path to the template file relative
149
		 * to the templates directory (usually in client/jsonapi/templates).
150
		 *
151
		 * You can overwrite the template file configuration in extensions and
152
		 * provide alternative templates. These alternative templates should be
153
		 * named like the default one but with the string "standard" replaced by
154
		 * an unique name. You may use the name of your project for this. If
155
		 * you've implemented an alternative client class as well, "standard"
156
		 * should be replaced by the name of the new class.
157
		 *
158
		 * @param string Relative path to the template creating the body of the JSON API
159
		 * @since 2017.03
160
		 * @category Developer
161
		 */
162
		$tplconf = 'client/jsonapi/review/standard/template';
163
		$default = 'review/standard';
164
165
		$body = $view->render( $view->config( $tplconf, $default ) );
166
167
		return $response->withHeader( 'Allow', 'GET,OPTIONS' )
168
			->withHeader( 'Cache-Control', 'max-age=300' )
169
			->withHeader( 'Content-Type', 'application/vnd.api+json' )
170
			->withBody( $view->response()->createStreamFromString( $body ) )
171
			->withStatus( $status );
172
	}
173
}
174