Passed
Push — master ( 077247...13de70 )
by Aimeos
09:19
created

Standard::patch()   D

Complexity

Conditions 14
Paths 252

Size

Total Lines 67
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 14
eloc 35
nc 252
nop 2
dl 0
loc 67
rs 4.7833
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2017-2022
6
 * @package Client
7
 * @subpackage JsonApi
8
 */
9
10
11
namespace Aimeos\Client\JsonApi\Basket\Service;
12
13
use Psr\Http\Message\ResponseInterface;
14
use Psr\Http\Message\ServerRequestInterface;
15
16
17
/**
18
 * JSON API basket/service client
19
 *
20
 * @package Client
21
 * @subpackage JsonApi
22
 */
23
class Standard
24
	extends \Aimeos\Client\JsonApi\Basket\Base
25
	implements \Aimeos\Client\JsonApi\Iface
26
{
27
	/** client/jsonapi/basket/service/name
28
	 * Class name of the used basket/service 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\Basket\Service\Standard
38
	 *
39
	 * and you want to replace it with your own version named
40
	 *
41
	 *  \Aimeos\Client\JsonApi\Basket\Service\Mybasket/service
42
	 *
43
	 * then you have to set the this configuration option:
44
	 *
45
	 *  client/jsonapi/basket/service/name = Mybasket/service
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 "MyService"!
55
	 *
56
	 * @param string Last part of the class name
57
	 * @since 2017.03
58
	 * @category Developer
59
	 */
60
61
	/** client/jsonapi/basket/service/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/basket/service/decorators/global
84
	 * @see client/jsonapi/basket/service/decorators/local
85
	 */
86
87
	/** client/jsonapi/basket/service/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/basket/service/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
	 * "basket" 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/basket/service/decorators/excludes
110
	 * @see client/jsonapi/basket/service/decorators/local
111
	 */
112
113
	/** client/jsonapi/basket/service/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\Basket\Service\Decorator\*") around the JsonApi
123
	 * client.
124
	 *
125
	 *  client/jsonapi/basket/service/decorators/local = array( 'decorator2' )
126
	 *
127
	 * This would add the decorator named "decorator2" defined by
128
	 * "\Aimeos\Client\JsonApi\Basket\Service\Decorator\Decorator2" only to the
129
	 * "basket service" 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/basket/service/decorators/excludes
136
	 * @see client/jsonapi/basket/service/decorators/global
137
	 */
138
139
140
	private $controller;
141
142
143
	/**
144
	 * Initializes the client
145
	 *
146
	 * @param \Aimeos\MShop\ContextIface $context MShop context object
147
	 */
148
	public function __construct( \Aimeos\MShop\ContextIface $context )
149
	{
150
		parent::__construct( $context );
151
152
		$this->controller = \Aimeos\Controller\Frontend::create( $this->context(), 'basket' );
153
	}
154
155
156
	/**
157
	 * Deletes the resource or the resource list
158
	 *
159
	 * @param \Psr\Http\Message\ServerRequestInterface $request Request object
160
	 * @param \Psr\Http\Message\ResponseInterface $response Response object
161
	 * @return \Psr\Http\Message\ResponseInterface Modified response object
162
	 */
163
	public function delete( ServerRequestInterface $request, ResponseInterface $response ) : \Psr\Http\Message\ResponseInterface
164
	{
165
		$view = $this->view();
166
167
		try
168
		{
169
			$this->clearCache();
170
			$this->controller->setType( $view->param( 'id', 'default' ) );
171
172
			$relId = $view->param( 'relatedid' );
173
			$body = (string) $request->getBody();
174
175
			if( $relId === '' || $relId === null )
176
			{
177
				if( ( $payload = json_decode( $body ) ) === null || !isset( $payload->data ) ) {
178
					throw new \Aimeos\Client\JsonApi\Exception( 'Invalid JSON in body', 400 );
179
				}
180
181
				if( !is_array( $payload->data ) ) {
182
					$payload->data = [$payload->data];
183
				}
184
185
				foreach( $payload->data as $entry )
186
				{
187
					if( !isset( $entry->id ) ) {
188
						throw new \Aimeos\Client\JsonApi\Exception( 'Type (ID) is missing', 400 );
189
					}
190
191
					$this->controller->deleteService( $entry->id );
192
				}
193
			}
194
			else
195
			{
196
				$this->controller->deleteService( $relId );
197
			}
198
199
200
			$view->item = $this->controller->get();
201
			$status = 200;
202
		}
203
		catch( \Aimeos\MShop\Plugin\Provider\Exception $e )
204
		{
205
			$status = 409;
206
			$errors = $this->translatePluginErrorCodes( $e->getErrorCodes() );
207
			$view->errors = $this->getErrorDetails( $e, 'mshop' ) + $errors;
208
		}
209
		catch( \Aimeos\MShop\Exception $e )
210
		{
211
			$status = 404;
212
			$view->errors = $this->getErrorDetails( $e, 'mshop' );
213
		}
214
		catch( \Exception $e )
215
		{
216
			$status = $e->getCode() >= 100 && $e->getCode() < 600 ? $e->getCode() : 500;
217
			$view->errors = $this->getErrorDetails( $e );
218
		}
219
220
		return $this->render( $response, $view, $status );
221
	}
222
223
224
	/**
225
	 * Updates the resource or the resource list partitially
226
	 *
227
	 * @param \Psr\Http\Message\ServerRequestInterface $request Request object
228
	 * @param \Psr\Http\Message\ResponseInterface $response Response object
229
	 * @return \Psr\Http\Message\ResponseInterface Modified response object
230
	 */
231
	public function patch( ServerRequestInterface $request, ResponseInterface $response ) : \Psr\Http\Message\ResponseInterface
232
	{
233
		$view = $this->view();
234
235
		try
236
		{
237
			$this->clearCache();
238
			$this->controller->setType( $view->param( 'id', 'default' ) );
239
240
			$body = (string) $request->getBody();
241
242
			if( ( $payload = json_decode( $body ) ) === null || !isset( $payload->data ) ) {
243
				throw new \Aimeos\Client\JsonApi\Exception( 'Invalid JSON in body', 400 );
244
			}
245
246
			if( !is_array( $payload->data ) ) {
247
				$payload->data = [$payload->data];
248
			}
249
250
			$cntl = \Aimeos\Controller\Frontend::create( $this->context(), 'service' );
251
252
			if( $relId = $view->param( 'relatedid' ) ) {
253
				$this->controller->deleteService( $relId );
254
			}
255
256
			foreach( $payload->data as $entry )
257
			{
258
				if( !isset( $entry->id ) ) {
259
					throw new \Aimeos\Client\JsonApi\Exception( 'Service type (ID) is missing', 400 );
260
				}
261
262
				if( !isset( $entry->attributes ) ) {
263
					throw new \Aimeos\Client\JsonApi\Exception( 'Service attributes are missing', 400 );
264
				}
265
266
				if( !isset( $entry->attributes->{'service.id'} ) ) {
267
					throw new \Aimeos\Client\JsonApi\Exception( 'Service ID in attributes is missing', 400 );
268
				}
269
270
				$item = $cntl->uses( ['media', 'price', 'text'] )->get( $entry->attributes->{'service.id'} );
271
				unset( $entry->attributes->{'service.id'} );
272
273
				$this->controller->addService( $item, (array) $entry->attributes );
274
			}
275
276
277
			$view->item = $this->controller->get();
278
			$status = 200;
279
		}
280
		catch( \Aimeos\MShop\Plugin\Provider\Exception $e )
281
		{
282
			$status = 409;
283
			$errors = $this->translatePluginErrorCodes( $e->getErrorCodes() );
284
			$view->errors = $this->getErrorDetails( $e, 'mshop' ) + $errors;
285
		}
286
		catch( \Aimeos\MShop\Exception $e )
287
		{
288
			$status = 404;
289
			$view->errors = $this->getErrorDetails( $e, 'mshop' );
290
		}
291
		catch( \Exception $e )
292
		{
293
			$status = $e->getCode() >= 100 && $e->getCode() < 600 ? $e->getCode() : 500;
294
			$view->errors = $this->getErrorDetails( $e );
295
		}
296
297
		return $this->render( $response, $view, $status );
298
	}
299
300
301
	/**
302
	 * Creates or updates the resource or the resource list
303
	 *
304
	 * @param \Psr\Http\Message\ServerRequestInterface $request Request object
305
	 * @param \Psr\Http\Message\ResponseInterface $response Response object
306
	 * @return \Psr\Http\Message\ResponseInterface Modified response object
307
	 */
308
	public function post( ServerRequestInterface $request, ResponseInterface $response ) : \Psr\Http\Message\ResponseInterface
309
	{
310
		$view = $this->view();
311
312
		try
313
		{
314
			$this->clearCache();
315
			$this->controller->setType( $view->param( 'id', 'default' ) );
316
317
			$body = (string) $request->getBody();
318
319
			if( ( $payload = json_decode( $body ) ) === null || !isset( $payload->data ) ) {
320
				throw new \Aimeos\Client\JsonApi\Exception( 'Invalid JSON in body', 400 );
321
			}
322
323
			if( !is_array( $payload->data ) ) {
324
				$payload->data = [$payload->data];
325
			}
326
327
			$cntl = \Aimeos\Controller\Frontend::create( $this->context(), 'service' );
328
329
			foreach( $payload->data as $entry )
330
			{
331
				if( !isset( $entry->id ) ) {
332
					throw new \Aimeos\Client\JsonApi\Exception( 'Service type (ID) is missing', 400 );
333
				}
334
335
				if( !isset( $entry->attributes ) ) {
336
					throw new \Aimeos\Client\JsonApi\Exception( 'Service attributes are missing', 400 );
337
				}
338
339
				if( !isset( $entry->attributes->{'service.id'} ) ) {
340
					throw new \Aimeos\Client\JsonApi\Exception( 'Service ID in attributes is missing', 400 );
341
				}
342
343
				$item = $cntl->uses( ['media', 'price', 'text'] )->get( $entry->attributes->{'service.id'} );
344
				unset( $entry->attributes->{'service.id'} );
345
346
				$this->controller->addService( $item, (array) $entry->attributes );
347
			}
348
349
350
			$view->item = $this->controller->get();
351
			$status = 201;
352
		}
353
		catch( \Aimeos\MShop\Plugin\Provider\Exception $e )
354
		{
355
			$status = 409;
356
			$errors = $this->translatePluginErrorCodes( $e->getErrorCodes() );
357
			$view->errors = $this->getErrorDetails( $e, 'mshop' ) + $errors;
358
		}
359
		catch( \Aimeos\MShop\Exception $e )
360
		{
361
			$status = 404;
362
			$view->errors = $this->getErrorDetails( $e, 'mshop' );
363
		}
364
		catch( \Exception $e )
365
		{
366
			$status = $e->getCode() >= 100 && $e->getCode() < 600 ? $e->getCode() : 500;
367
			$view->errors = $this->getErrorDetails( $e );
368
		}
369
370
		return $this->render( $response, $view, $status );
371
	}
372
373
374
	/**
375
	 * Returns the available REST verbs and the available parameters
376
	 *
377
	 * @param \Psr\Http\Message\ServerRequestInterface $request Request object
378
	 * @param \Psr\Http\Message\ResponseInterface $response Response object
379
	 * @return \Psr\Http\Message\ResponseInterface Modified response object
380
	 */
381
	public function options( ServerRequestInterface $request, ResponseInterface $response ) : \Psr\Http\Message\ResponseInterface
382
	{
383
		$view = $this->view();
384
385
		$view->attributes = [
386
			'service.id' => [
387
				'label' => 'ID of the payment or delivery service item',
388
				'type' => 'string', 'default' => '', 'required' => false,
389
			],
390
		];
391
392
		$tplconf = 'client/jsonapi/template-options';
393
		$default = 'options-standard';
394
395
		$body = $view->render( $view->config( $tplconf, $default ) );
396
397
		return $response->withHeader( 'Allow', 'DELETE,GET,OPTIONS,PATCH,POST' )
398
			->withHeader( 'Cache-Control', 'max-age=300' )
399
			->withHeader( 'Content-Type', 'application/vnd.api+json' )
400
			->withBody( $view->response()->createStreamFromString( $body ) )
401
			->withStatus( 200 );
402
	}
403
404
405
	/**
406
	 * Returns the response object with the rendered header and body
407
	 *
408
	 * @param \Psr\Http\Message\ResponseInterface $response Response object
409
	 * @param \Aimeos\Base\View\Iface $view View instance
410
	 * @param int $status HTTP status code
411
	 * @return \Psr\Http\Message\ResponseInterface Modified response object
412
	 */
413
	protected function render( ResponseInterface $response, \Aimeos\Base\View\Iface $view, int $status ) : \Psr\Http\Message\ResponseInterface
414
	{
415
		$tplconf = 'client/jsonapi/basket/template';
416
		$default = 'basket/standard';
417
418
		$body = $view->render( $view->config( $tplconf, $default ) );
419
420
		return $response->withHeader( 'Allow', 'DELETE,GET,OPTIONS,PATCH,POST' )
421
			->withHeader( 'Cache-Control', 'no-cache, private' )
422
			->withHeader( 'Content-Type', 'application/vnd.api+json' )
423
			->withBody( $view->response()->createStreamFromString( $body ) )
424
			->withStatus( $status );
425
	}
426
}
427