Standard   B
last analyzed

Complexity

Total Complexity 49

Size/Duplication

Total Lines 452
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 152
dl 0
loc 452
rs 8.48
c 0
b 0
f 0
wmc 49

6 Methods

Rating   Name   Duplication   Size   Complexity  
A options() 0 45 1
D delete() 0 62 15
C post() 0 49 12
B patch() 0 48 10
B get() 0 42 10
A render() 0 31 1

How to fix   Complexity   

Complex Class

Complex classes like Standard often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Standard, and based on these observations, apply Extract Interface, too.

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

432
			->/** @scrutinizer ignore-call */ withStatus( 200 );
Loading history...
433
	}
434
435
436
	/**
437
	 * Returns the response object with the rendered header and body
438
	 *
439
	 * @param \Psr\Http\Message\ResponseInterface $response Response object
440
	 * @param \Aimeos\Base\View\Iface $view View instance
441
	 * @param int $status HTTP status code
442
	 * @return \Psr\Http\Message\ResponseInterface Modified response object
443
	 */
444
	protected function render( ResponseInterface $response, \Aimeos\Base\View\Iface $view, int $status ) : \Psr\Http\Message\ResponseInterface
445
	{
446
		/** client/jsonapi/customer/relationships/template
447
		 * Relative path to the customer relationships JSON API template
448
		 *
449
		 * The template file contains the code and processing instructions
450
		 * to generate the result shown in the JSON API body. The
451
		 * configuration string is the path to the template file relative
452
		 * to the templates directory (usually in templates/client/jsonapi).
453
		 *
454
		 * You can overwrite the template file configuration in extensions and
455
		 * provide alternative templates. These alternative templates should be
456
		 * named like the default one but with the string "standard" replaced by
457
		 * an unique name. You may use the name of your project for this. If
458
		 * you've implemented an alternative client class as well, "standard"
459
		 * should be replaced by the name of the new class.
460
		 *
461
		 * @param string Relative path to the template creating the body for the JSON API
462
		 * @since 2017.07
463
		 * @category Developer
464
		 */
465
		$tplconf = 'client/jsonapi/customer/relationships/template';
466
		$default = 'customer/relationships/standard';
467
468
		$body = $view->render( $view->config( $tplconf, $default ) );
469
470
		return $response->withHeader( 'Allow', 'DELETE,GET,OPTIONS,PATCH,POST' )
471
			->withHeader( 'Cache-Control', 'no-cache, private' )
472
			->withHeader( 'Content-Type', 'application/vnd.api+json' )
473
			->withBody( $view->response()->createStreamFromString( $body ) )
474
			->withStatus( $status );
475
	}
476
}
477