Passed
Push — master ( 099796...e40516 )
by Aimeos
03:54
created

Standard   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 120
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
eloc 44
c 1
b 0
f 0
dl 0
loc 120
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A get() 0 42 5
A render() 0 31 1
A options() 0 15 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
				$items = $cntl->slice( $view->param( 'page/offset', 0 ), $view->param( 'page/limit', 25 ) )
51
					->sort( $view->param( 'sort' ) )->parse( $view->param( 'filter', [] ) )->search( $total );
52
53
				$view->items = $items;
54
				$view->total = $total;
55
			}
56
57
			$status = 200;
58
		}
59
		catch( \Aimeos\Controller\Frontend\Exception $e )
60
		{
61
			$status = 403;
62
			$view->errors = $this->getErrorDetails( $e, 'controller/frontend' );
63
		}
64
		catch( \Aimeos\MShop\Exception $e )
65
		{
66
			$status = 404;
67
			$view->errors = $this->getErrorDetails( $e, 'mshop' );
68
		}
69
		catch( \Exception $e )
70
		{
71
			$status = 500;
72
			$view->errors = $this->getErrorDetails( $e );
73
		}
74
75
		return $this->render( $response, $view, $status );
76
	}
77
78
79
	/**
80
	 * Returns the available REST verbs and the available parameters
81
	 *
82
	 * @param \Psr\Http\Message\ServerRequestInterface $request Request object
83
	 * @param \Psr\Http\Message\ResponseInterface $response Response object
84
	 * @return \Psr\Http\Message\ResponseInterface Modified response object
85
	 */
86
	public function options( ServerRequestInterface $request, ResponseInterface $response ) : \Psr\Http\Message\ResponseInterface
87
	{
88
		$view = $this->getView();
89
		$view->attributes = [];
90
91
		$tplconf = 'client/jsonapi/standard/template-options';
92
		$default = 'options-standard';
93
94
		$body = $view->render( $view->config( $tplconf, $default ) );
95
96
		return $response->withHeader( 'Allow', 'GET,OPTIONS' )
97
			->withHeader( 'Cache-Control', 'max-age=300' )
98
			->withHeader( 'Content-Type', 'application/vnd.api+json' )
99
			->withBody( $view->response()->createStreamFromString( $body ) )
100
			->withStatus( 200 );
101
	}
102
103
104
	/**
105
	 * Returns the response object with the rendered header and body
106
	 *
107
	 * @param \Psr\Http\Message\ResponseInterface $response Response object
108
	 * @param \Aimeos\MW\View\Iface $view View instance
109
	 * @param integer $status HTTP status code
110
	 * @return \Psr\Http\Message\ResponseInterface Modified response object
111
	 */
112
	protected function render( ResponseInterface $response, \Aimeos\MW\View\Iface $view, $status ) : \Psr\Http\Message\ResponseInterface
113
	{
114
		/** client/jsonapi/review/standard/template
115
		 * Relative path to the review JSON API template
116
		 *
117
		 * The template file contains the code and processing instructions
118
		 * to generate the result shown in the JSON API body. The
119
		 * configuration string is the path to the template file relative
120
		 * to the templates directory (usually in client/jsonapi/templates).
121
		 *
122
		 * You can overwrite the template file configuration in extensions and
123
		 * provide alternative templates. These alternative templates should be
124
		 * named like the default one but with the string "standard" replaced by
125
		 * an unique name. You may use the name of your project for this. If
126
		 * you've implemented an alternative client class as well, "standard"
127
		 * should be replaced by the name of the new class.
128
		 *
129
		 * @param string Relative path to the template creating the body of the JSON API
130
		 * @since 2017.03
131
		 * @category Developer
132
		 */
133
		$tplconf = 'client/jsonapi/review/standard/template';
134
		$default = 'review/standard';
135
136
		$body = $view->render( $view->config( $tplconf, $default ) );
137
138
		return $response->withHeader( 'Allow', 'GET,OPTIONS' )
139
			->withHeader( 'Cache-Control', 'max-age=300' )
140
			->withHeader( 'Content-Type', 'application/vnd.api+json' )
141
			->withBody( $view->response()->createStreamFromString( $body ) )
142
			->withStatus( $status );
143
	}
144
}
145