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

Standard   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 160
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 15
eloc 60
c 1
b 0
f 0
dl 0
loc 160
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A render() 0 31 1
A options() 0 15 1
B get() 0 42 7
A delete() 0 30 6
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2018-2020
6
 * @package Client
7
 * @subpackage JsonApi
8
 */
9
10
11
namespace Aimeos\Client\JsonApi\Subscription;
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
	 * Cancels the subscription
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
38
		try
39
		{
40
			$cntl = \Aimeos\Controller\Frontend::create( $this->getContext(), 'subscription' );
41
42
			$view->items = $cntl->cancel( $view->param( 'id', '' ) );
43
			$view->total = 1;
44
45
			$status = 200;
46
		}
47
		catch( \Aimeos\Controller\Frontend\Exception $e )
48
		{
49
			$status = 403;
50
			$view->errors = $this->getErrorDetails( $e, 'controller/frontend' );
51
		}
52
		catch( \Aimeos\MShop\Exception $e )
53
		{
54
			$status = 404;
55
			$view->errors = $this->getErrorDetails( $e, 'mshop' );
56
		}
57
		catch( \Exception $e )
58
		{
59
			$status = $e->getCode() >= 100 && $e->getCode() < 600 ? $e->getCode() : 500;
60
			$view->errors = $this->getErrorDetails( $e );
61
		}
62
63
		return $this->render( $response, $view, $status );
64
	}
65
66
67
	/**
68
	 * Returns the resource or the resource list
69
	 *
70
	 * @param \Psr\Http\Message\ServerRequestInterface $request Request object
71
	 * @param \Psr\Http\Message\ResponseInterface $response Response object
72
	 * @return \Psr\Http\Message\ResponseInterface Modified response object
73
	 */
74
	public function get( ServerRequestInterface $request, ResponseInterface $response ) : \Psr\Http\Message\ResponseInterface
75
	{
76
		$view = $this->getView();
77
78
		try
79
		{
80
			$cntl = \Aimeos\Controller\Frontend::create( $this->getContext(), 'subscription' );
81
82
			if( ( $id = $view->param( 'id' ) ) != '' )
83
			{
84
				$view->items = $cntl->get( $id );
85
				$view->total = 1;
86
			}
87
			else
88
			{
89
				$total = 0;
90
				$items = $cntl->slice( $view->param( 'page/offset', 0 ), $view->param( 'page/limit', 25 ) )
91
					->sort( $view->param( 'sort' ) )->parse( $view->param( 'filter', [] ) )->search( $total );
92
93
				$view->items = $items;
94
				$view->total = $total;
95
			}
96
97
			$status = 200;
98
		}
99
		catch( \Aimeos\Controller\Frontend\Exception $e )
100
		{
101
			$status = 403;
102
			$view->errors = $this->getErrorDetails( $e, 'controller/frontend' );
103
		}
104
		catch( \Aimeos\MShop\Exception $e )
105
		{
106
			$status = 404;
107
			$view->errors = $this->getErrorDetails( $e, 'mshop' );
108
		}
109
		catch( \Exception $e )
110
		{
111
			$status = $e->getCode() >= 100 && $e->getCode() < 600 ? $e->getCode() : 500;
112
			$view->errors = $this->getErrorDetails( $e );
113
		}
114
115
		return $this->render( $response, $view, $status );
116
	}
117
118
119
	/**
120
	 * Returns the available REST verbs and the available parameters
121
	 *
122
	 * @param \Psr\Http\Message\ServerRequestInterface $request Request object
123
	 * @param \Psr\Http\Message\ResponseInterface $response Response object
124
	 * @return \Psr\Http\Message\ResponseInterface Modified response object
125
	 */
126
	public function options( ServerRequestInterface $request, ResponseInterface $response ) : \Psr\Http\Message\ResponseInterface
127
	{
128
		$view = $this->getView();
129
		$view->attributes = [];
130
131
		$tplconf = 'client/jsonapi/standard/template-options';
132
		$default = 'options-standard';
133
134
		$body = $view->render( $view->config( $tplconf, $default ) );
135
136
		return $response->withHeader( 'Allow', 'GET,OPTIONS,POST' )
137
			->withHeader( 'Cache-Control', 'max-age=300' )
138
			->withHeader( 'Content-Type', 'application/vnd.api+json' )
139
			->withBody( $view->response()->createStreamFromString( $body ) )
140
			->withStatus( 200 );
141
	}
142
143
144
	/**
145
	 * Returns the response object with the rendered header and body
146
	 *
147
	 * @param \Psr\Http\Message\ResponseInterface $response Response object
148
	 * @param \Aimeos\MW\View\Iface $view View instance
149
	 * @param integer $status HTTP status code
150
	 * @return \Psr\Http\Message\ResponseInterface Modified response object
151
	 */
152
	protected function render( ResponseInterface $response, \Aimeos\MW\View\Iface $view, $status ) : \Psr\Http\Message\ResponseInterface
153
	{
154
		/** client/jsonapi/subscription/standard/template
155
		 * Relative path to the subscription JSON API template
156
		 *
157
		 * The template file contains the code and processing instructions
158
		 * to generate the result shown in the JSON API body. The
159
		 * configuration string is the path to the template file relative
160
		 * to the templates directory (usually in client/jsonapi/templates).
161
		 *
162
		 * You can overwrite the template file configuration in extensions and
163
		 * provide alternative templates. These alternative templates should be
164
		 * named like the default one but with the string "standard" replaced by
165
		 * an unique name. You may use the name of your project for this. If
166
		 * you've implemented an alternative client class as well, "standard"
167
		 * should be replaced by the name of the new class.
168
		 *
169
		 * @param string Relative path to the template creating the body of the JSON API
170
		 * @since 2017.03
171
		 * @category Developer
172
		 */
173
		$tplconf = 'client/jsonapi/subscription/standard/template';
174
		$default = 'subscription/standard';
175
176
		$body = $view->render( $view->config( $tplconf, $default ) );
177
178
		return $response->withHeader( 'Allow', 'GET,OPTIONS' )
179
			->withHeader( 'Cache-Control', 'no-cache, private' )
180
			->withHeader( 'Content-Type', 'application/vnd.api+json' )
181
			->withBody( $view->response()->createStreamFromString( $body ) )
182
			->withStatus( $status );
183
	}
184
}
185