Standard   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 273
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 60
dl 0
loc 273
rs 10
c 0
b 0
f 0
wmc 15

4 Methods

Rating   Name   Duplication   Size   Complexity  
B get() 0 42 7
A render() 0 31 1
A delete() 0 30 6
A options() 0 15 1
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2018-2025
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
	/** client/jsonapi/subscription/name
28
	 * Class name of the used subscription 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\Subscription\Standard
38
	 *
39
	 * and you want to replace it with your own version named
40
	 *
41
	 *  \Aimeos\Client\JsonApi\Subscription\Mysubscription
42
	 *
43
	 * then you have to set the this configuration option:
44
	 *
45
	 *  client/jsonapi/subscription/name = Mysubscription
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 "MySubscription"!
55
	 *
56
	 * @param string Last part of the class name
57
	 * @since 2017.03
58
	 * @category Developer
59
	 */
60
61
	/** client/jsonapi/subscription/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 2017.07
81
	 * @category Developer
82
	 * @see client/jsonapi/common/decorators/default
83
	 * @see client/jsonapi/subscription/decorators/global
84
	 * @see client/jsonapi/subscription/decorators/local
85
	 */
86
87
	/** client/jsonapi/subscription/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/subscription/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
	 * "subscription" JsonApi client.
104
	 *
105
	 * @param array List of decorator names
106
	 * @since 2017.07
107
	 * @category Developer
108
	 * @see client/jsonapi/common/decorators/default
109
	 * @see client/jsonapi/subscription/decorators/excludes
110
	 * @see client/jsonapi/subscription/decorators/local
111
	 */
112
113
	/** client/jsonapi/subscription/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\Subscription\Decorator\*") around the JsonApi
123
	 * client.
124
	 *
125
	 *  client/jsonapi/subscription/decorators/local = array( 'decorator2' )
126
	 *
127
	 * This would add the decorator named "decorator2" defined by
128
	 * "\Aimeos\Client\JsonApi\Subscription\Decorator\Decorator2" only to the
129
	 * "subscription" JsonApi client.
130
	 *
131
	 * @param array List of decorator names
132
	 * @since 2017.07
133
	 * @category Developer
134
	 * @see client/jsonapi/common/decorators/default
135
	 * @see client/jsonapi/subscription/decorators/excludes
136
	 * @see client/jsonapi/subscription/decorators/global
137
	 */
138
139
140
	/**
141
	 * Cancels the subscription
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 delete( ServerRequestInterface $request, ResponseInterface $response ) : \Psr\Http\Message\ResponseInterface
148
	{
149
		$view = $this->view();
150
151
		try
152
		{
153
			$cntl = \Aimeos\Controller\Frontend::create( $this->context(), 'subscription' );
154
155
			$view->items = $cntl->cancel( $view->param( 'id', '' ) );
156
			$view->total = 1;
157
158
			$status = 200;
159
		}
160
		catch( \Aimeos\Controller\Frontend\Exception $e )
161
		{
162
			$status = 403;
163
			$view->errors = $this->getErrorDetails( $e, 'controller/frontend' );
164
		}
165
		catch( \Aimeos\MShop\Exception $e )
166
		{
167
			$status = 404;
168
			$view->errors = $this->getErrorDetails( $e, 'mshop' );
169
		}
170
		catch( \Exception $e )
171
		{
172
			$status = $e->getCode() >= 100 && $e->getCode() < 600 ? $e->getCode() : 500;
173
			$view->errors = $this->getErrorDetails( $e );
174
		}
175
176
		return $this->render( $response, $view, $status );
177
	}
178
179
180
	/**
181
	 * Returns the resource or the resource list
182
	 *
183
	 * @param \Psr\Http\Message\ServerRequestInterface $request Request object
184
	 * @param \Psr\Http\Message\ResponseInterface $response Response object
185
	 * @return \Psr\Http\Message\ResponseInterface Modified response object
186
	 */
187
	public function get( ServerRequestInterface $request, ResponseInterface $response ) : \Psr\Http\Message\ResponseInterface
188
	{
189
		$view = $this->view();
190
191
		try
192
		{
193
			$cntl = \Aimeos\Controller\Frontend::create( $this->context(), 'subscription' );
194
195
			if( ( $id = $view->param( 'id' ) ) != '' )
196
			{
197
				$view->items = $cntl->get( $id );
198
				$view->total = 1;
199
			}
200
			else
201
			{
202
				$total = 0;
203
				$items = $cntl->slice( $view->param( 'page/offset', 0 ), $view->param( 'page/limit', 25 ) )
204
					->sort( $view->param( 'sort' ) )->parse( $view->param( 'filter', [] ) )->search( $total );
205
206
				$view->items = $items;
207
				$view->total = $total;
208
			}
209
210
			$status = 200;
211
		}
212
		catch( \Aimeos\Controller\Frontend\Exception $e )
213
		{
214
			$status = 403;
215
			$view->errors = $this->getErrorDetails( $e, 'controller/frontend' );
216
		}
217
		catch( \Aimeos\MShop\Exception $e )
218
		{
219
			$status = 404;
220
			$view->errors = $this->getErrorDetails( $e, 'mshop' );
221
		}
222
		catch( \Exception $e )
223
		{
224
			$status = $e->getCode() >= 100 && $e->getCode() < 600 ? $e->getCode() : 500;
225
			$view->errors = $this->getErrorDetails( $e );
226
		}
227
228
		return $this->render( $response, $view, $status );
229
	}
230
231
232
	/**
233
	 * Returns the available REST verbs and the available parameters
234
	 *
235
	 * @param \Psr\Http\Message\ServerRequestInterface $request Request object
236
	 * @param \Psr\Http\Message\ResponseInterface $response Response object
237
	 * @return \Psr\Http\Message\ResponseInterface Modified response object
238
	 */
239
	public function options( ServerRequestInterface $request, ResponseInterface $response ) : \Psr\Http\Message\ResponseInterface
240
	{
241
		$view = $this->view();
242
		$view->attributes = [];
243
244
		$tplconf = 'client/jsonapi/template-options';
245
		$default = 'options-standard';
246
247
		$body = $view->render( $view->config( $tplconf, $default ) );
248
249
		return $response->withHeader( 'Allow', 'GET,OPTIONS,POST' )
250
			->withHeader( 'Cache-Control', 'max-age=300' )
251
			->withHeader( 'Content-Type', 'application/vnd.api+json' )
252
			->withBody( $view->response()->createStreamFromString( $body ) )
253
			->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

253
			->/** @scrutinizer ignore-call */ withStatus( 200 );
Loading history...
254
	}
255
256
257
	/**
258
	 * Returns the response object with the rendered header and body
259
	 *
260
	 * @param \Psr\Http\Message\ResponseInterface $response Response object
261
	 * @param \Aimeos\Base\View\Iface $view View instance
262
	 * @param integer $status HTTP status code
263
	 * @return \Psr\Http\Message\ResponseInterface Modified response object
264
	 */
265
	protected function render( ResponseInterface $response, \Aimeos\Base\View\Iface $view, $status ) : \Psr\Http\Message\ResponseInterface
266
	{
267
		/** client/jsonapi/subscription/template
268
		 * Relative path to the subscription JSON API template
269
		 *
270
		 * The template file contains the code and processing instructions
271
		 * to generate the result shown in the JSON API body. The
272
		 * configuration string is the path to the template file relative
273
		 * to the templates directory (usually in templates/client/jsonapi).
274
		 *
275
		 * You can overwrite the template file configuration in extensions and
276
		 * provide alternative templates. These alternative templates should be
277
		 * named like the default one but with the string "standard" replaced by
278
		 * an unique name. You may use the name of your project for this. If
279
		 * you've implemented an alternative client class as well, "standard"
280
		 * should be replaced by the name of the new class.
281
		 *
282
		 * @param string Relative path to the template creating the body of the JSON API
283
		 * @since 2017.03
284
		 * @category Developer
285
		 */
286
		$tplconf = 'client/jsonapi/subscription/template';
287
		$default = 'subscription/standard';
288
289
		$body = $view->render( $view->config( $tplconf, $default ) );
290
291
		return $response->withHeader( 'Allow', 'GET,OPTIONS' )
292
			->withHeader( 'Cache-Control', 'no-cache, private' )
293
			->withHeader( 'Content-Type', 'application/vnd.api+json' )
294
			->withBody( $view->response()->createStreamFromString( $body ) )
295
			->withStatus( $status );
296
	}
297
}
298