Standard::delete()   C
last analyzed

Complexity

Conditions 14
Paths 138

Size

Total Lines 59
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 14
eloc 30
nc 138
nop 2
dl 0
loc 59
rs 5.95
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-2025
6
 * @package Client
7
 * @subpackage JsonApi
8
 */
9
10
11
namespace Aimeos\Client\JsonApi\Customer\Address;
12
13
use Psr\Http\Message\ResponseInterface;
14
use Psr\Http\Message\ServerRequestInterface;
15
16
17
/**
18
 * JSON API customer/address 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/address/name
28
	 * Class name of the used customer/address 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\Address\Standard
38
	 *
39
	 * and you want to replace it with your own version named
40
	 *
41
	 *  \Aimeos\Client\JsonApi\Customer\Address\Mycustomer/address
42
	 *
43
	 * then you have to set the this configuration option:
44
	 *
45
	 *  client/jsonapi/customer/address/name = Mycustomer/address
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 "MyAddress"!
55
	 *
56
	 * @param string Last part of the class name
57
	 * @since 2017.03
58
	 * @category Developer
59
	 */
60
61
	/** client/jsonapi/customer/address/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/address/decorators/global
84
	 * @see client/jsonapi/customer/address/decorators/local
85
	 */
86
87
	/** client/jsonapi/customer/address/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/address/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/address/decorators/excludes
110
	 * @see client/jsonapi/customer/address/decorators/local
111
	 */
112
113
	/** client/jsonapi/customer/address/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\Address\Decorator\*") around the JsonApi
123
	 * client.
124
	 *
125
	 *  client/jsonapi/customer/address/decorators/local = array( 'decorator2' )
126
	 *
127
	 * This would add the decorator named "decorator2" defined by
128
	 * "\Aimeos\Client\JsonApi\Customer\Address\Decorator\Decorator2" only to the
129
	 * "customer address" 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/address/decorators/excludes
136
	 * @see client/jsonapi/customer/address/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
			$cntl = \Aimeos\Controller\Frontend::create( $this->context(), 'customer' );
155
			$items = $cntl->uses( ['customer/address'] )->get()->getAddressItems();
156
157
			if( ( $relId = $view->param( 'relatedid' ) ) === null )
158
			{
159
				if( ( $payload = json_decode( $body ) ) === null || !isset( $payload->data ) ) {
160
					throw new \Aimeos\Client\JsonApi\Exception( 'Invalid JSON in body', 400 );
161
				}
162
163
				if( !is_array( $payload->data ) ) {
164
					$payload->data = [$payload->data];
165
				}
166
167
				foreach( $payload->data as $entry )
168
				{
169
					if( !isset( $entry->id ) ) {
170
						throw new \Aimeos\Client\JsonApi\Exception( 'ID is missing', 400 );
171
					}
172
173
					if( ( $item = $items->get( $entry->id ) ) !== null ) {
174
						$cntl->deleteAddressItem( $item );
175
					}
176
				}
177
178
				$cntl->store();
179
			}
180
			else
181
			{
182
				if( ( $item = $items->get( $relId ) ) !== null ) {
183
					$cntl->deleteAddressItem( $item )->store();
184
				}
185
			}
186
187
			$status = 200;
188
		}
189
		catch( \Aimeos\Controller\Frontend\Customer\Exception $e )
190
		{
191
			$status = 403;
192
			$view->errors = $this->getErrorDetails( $e, 'controller/frontend' );
193
		}
194
		catch( \Aimeos\MShop\Exception $e )
195
		{
196
			$status = 404;
197
			$view->errors = $this->getErrorDetails( $e, 'mshop' );
198
		}
199
		catch( \Exception $e )
200
		{
201
			$status = $e->getCode() >= 100 && $e->getCode() < 600 ? $e->getCode() : 500;
202
			$view->errors = $this->getErrorDetails( $e );
203
		}
204
205
		return $this->render( $response, $view, $status );
206
	}
207
208
209
	/**
210
	 * Returns the resource or the resource list
211
	 *
212
	 * @param \Psr\Http\Message\ServerRequestInterface $request Request object
213
	 * @param \Psr\Http\Message\ResponseInterface $response Response object
214
	 * @return \Psr\Http\Message\ResponseInterface Modified response object
215
	 */
216
	public function get( ServerRequestInterface $request, ResponseInterface $response ) : \Psr\Http\Message\ResponseInterface
217
	{
218
		$view = $this->view();
219
220
		try
221
		{
222
			$cntl = \Aimeos\Controller\Frontend::create( $this->context(), 'customer' );
223
			$item = $cntl->uses( ['customer/address'] )->get();
224
225
			if( ( $relId = $view->param( 'relatedid' ) ) == null )
226
			{
227
				$view->items = $item->getAddressItems();
228
				$view->total = count( $view->items );
229
			}
230
			else
231
			{
232
				$view->items = $item->getAddressItem( $relId );
233
				$view->total = empty( $view->items ) ? 0 : 1;
234
			}
235
236
			$status = 200;
237
		}
238
		catch( \Aimeos\Controller\Frontend\Customer\Exception $e )
239
		{
240
			$status = 403;
241
			$view->errors = $this->getErrorDetails( $e, 'controller/frontend' );
242
		}
243
		catch( \Aimeos\MShop\Exception $e )
244
		{
245
			$status = 404;
246
			$view->errors = $this->getErrorDetails( $e, 'mshop' );
247
		}
248
		catch( \Exception $e )
249
		{
250
			$status = $e->getCode() >= 100 && $e->getCode() < 600 ? $e->getCode() : 500;
251
			$view->errors = $this->getErrorDetails( $e );
252
		}
253
254
		return $this->render( $response, $view, $status );
255
	}
256
257
258
	/**
259
	 * Updates the resource or the resource list partitially
260
	 *
261
	 * @param \Psr\Http\Message\ServerRequestInterface $request Request object
262
	 * @param \Psr\Http\Message\ResponseInterface $response Response object
263
	 * @return \Psr\Http\Message\ResponseInterface Modified response object
264
	 */
265
	public function patch( ServerRequestInterface $request, ResponseInterface $response ) : \Psr\Http\Message\ResponseInterface
266
	{
267
		$view = $this->view();
268
269
		try
270
		{
271
			$body = (string) $request->getBody();
272
273
			if( ( $payload = json_decode( $body ) ) === null || !isset( $payload->data->attributes ) ) {
274
				throw new \Aimeos\Client\JsonApi\Exception( 'Invalid JSON in body', 400 );
275
			}
276
277
			$status = 404;
278
			$view->total = 0;
279
			$id = $view->param( 'relatedid' );
280
			$cntl = \Aimeos\Controller\Frontend::create( $this->context(), 'customer' );
281
282
			if( ( $item = $cntl->uses( ['customer/address'] )->get()->getAddressItem( $id ) ) !== null )
283
			{
284
				$attributes = (array) $payload->data->attributes;
285
				$item = $item->fromArray( $attributes );
286
				$cntl->addAddressItem( $item, $id )->store();
287
288
				$view->items = $item;
289
				$view->total = 1;
290
				$status = 200;
291
			}
292
		}
293
		catch( \Aimeos\Controller\Frontend\Customer\Exception $e )
294
		{
295
			$status = 403;
296
			$view->errors = $this->getErrorDetails( $e, 'controller/frontend' );
297
		}
298
		catch( \Aimeos\MShop\Exception $e )
299
		{
300
			$status = 404;
301
			$view->errors = $this->getErrorDetails( $e, 'mshop' );
302
		}
303
		catch( \Exception $e )
304
		{
305
			$status = $e->getCode() >= 100 && $e->getCode() < 600 ? $e->getCode() : 500;
306
			$view->errors = $this->getErrorDetails( $e );
307
		}
308
309
		return $this->render( $response, $view, $status );
310
	}
311
312
313
	/**
314
	 * Creates or updates the resource or the resource list
315
	 *
316
	 * @param \Psr\Http\Message\ServerRequestInterface $request Request object
317
	 * @param \Psr\Http\Message\ResponseInterface $response Response object
318
	 * @return \Psr\Http\Message\ResponseInterface Modified response object
319
	 */
320
	public function post( ServerRequestInterface $request, ResponseInterface $response ) : \Psr\Http\Message\ResponseInterface
321
	{
322
		$view = $this->view();
323
324
		try
325
		{
326
			$body = (string) $request->getBody();
327
328
			if( ( $payload = json_decode( $body ) ) === null || !isset( $payload->data ) ) {
329
				throw new \Aimeos\Client\JsonApi\Exception( 'Invalid JSON in body', 400 );
330
			}
331
332
			if( !is_array( $payload->data ) ) {
333
				$payload->data = [$payload->data];
334
			}
335
336
			$cntl = \Aimeos\Controller\Frontend::create( $this->context(), 'customer' )->uses( ['customer/address'] );
337
338
			foreach( $payload->data as $entry )
339
			{
340
				if( !isset( $entry->attributes ) ) {
341
					throw new \Aimeos\Client\JsonApi\Exception( 'Attributes are missing', 400 );
342
				}
343
344
				$addrItem = $cntl->createAddressItem( (array) $entry->attributes );
345
				$cntl->addAddressItem( $addrItem );
346
			}
347
348
			$view->items = $cntl->store()->get()->getAddressItems();
349
			$view->total = count( $view->items );
350
			$status = 201;
351
		}
352
		catch( \Aimeos\Controller\Frontend\Customer\Exception $e )
353
		{
354
			$status = 403;
355
			$view->errors = $this->getErrorDetails( $e, 'controller/frontend' );
356
		}
357
		catch( \Aimeos\MShop\Exception $e )
358
		{
359
			$status = 404;
360
			$view->errors = $this->getErrorDetails( $e, 'mshop' );
361
		}
362
		catch( \Exception $e )
363
		{
364
			$status = $e->getCode() >= 100 && $e->getCode() < 600 ? $e->getCode() : 500;
365
			$view->errors = $this->getErrorDetails( $e );
366
		}
367
368
		return $this->render( $response, $view, $status );
369
	}
370
371
372
	/**
373
	 * Returns the available REST verbs and the available parameters
374
	 *
375
	 * @param \Psr\Http\Message\ServerRequestInterface $request Request object
376
	 * @param \Psr\Http\Message\ResponseInterface $response Response object
377
	 * @return \Psr\Http\Message\ResponseInterface Modified response object
378
	 */
379
	public function options( ServerRequestInterface $request, ResponseInterface $response ) : \Psr\Http\Message\ResponseInterface
380
	{
381
		$view = $this->view();
382
383
		$view->attributes = [
384
			'customer.address.salutation' => [
385
				'label' => 'Customer salutation, i.e. "comany" ,"mr", "ms" or ""',
386
				'type' => 'string', 'default' => '', 'required' => false,
387
			],
388
			'customer.address.company' => [
389
				'label' => 'Company name',
390
				'type' => 'string', 'default' => '', 'required' => false,
391
			],
392
			'customer.address.vatid' => [
393
				'label' => 'VAT ID of the company',
394
				'type' => 'string', 'default' => '', 'required' => false,
395
			],
396
			'customer.address.title' => [
397
				'label' => 'Title of the customer',
398
				'type' => 'string', 'default' => '', 'required' => false,
399
			],
400
			'customer.address.firstname' => [
401
				'label' => 'First name of the customer',
402
				'type' => 'string', 'default' => '', 'required' => false,
403
			],
404
			'customer.address.lastname' => [
405
				'label' => 'Last name of the customer or full name',
406
				'type' => 'string', 'default' => '', 'required' => true,
407
			],
408
			'customer.address.address1' => [
409
				'label' => 'First address part like street',
410
				'type' => 'string', 'default' => '', 'required' => true,
411
			],
412
			'customer.address.address2' => [
413
				'label' => 'Second address part like house number',
414
				'type' => 'string', 'default' => '', 'required' => false,
415
			],
416
			'customer.address.address3' => [
417
				'label' => 'Third address part like flat number',
418
				'type' => 'string', 'default' => '', 'required' => false,
419
			],
420
			'customer.address.postal' => [
421
				'label' => 'Zip code of the city',
422
				'type' => 'string', 'default' => '', 'required' => false,
423
			],
424
			'customer.address.city' => [
425
				'label' => 'Name of the town/city',
426
				'type' => 'string', 'default' => '', 'required' => true,
427
			],
428
			'customer.address.state' => [
429
				'label' => 'Two letter code of the country state',
430
				'type' => 'string', 'default' => '', 'required' => false,
431
			],
432
			'customer.address.countryid' => [
433
				'label' => 'Two letter ISO country code',
434
				'type' => 'string', 'default' => '', 'required' => true,
435
			],
436
			'customer.address.languageid' => [
437
				'label' => 'Two or five letter ISO language code, e.g. "de" or "de_CH"',
438
				'type' => 'string', 'default' => '', 'required' => false,
439
			],
440
			'customer.address.telephone' => [
441
				'label' => 'Telephone number consisting of option leading "+" and digits without spaces',
442
				'type' => 'string', 'default' => '', 'required' => false,
443
			],
444
			'customer.address.telefax' => [
445
				'label' => 'Faximile number consisting of option leading "+" and digits without spaces',
446
				'type' => 'string', 'default' => '', 'required' => false,
447
			],
448
			'customer.address.email' => [
449
				'label' => 'E-mail address',
450
				'type' => 'string', 'default' => '', 'required' => false,
451
			],
452
			'customer.address.website' => [
453
				'label' => 'Web site including "http://" or "https://"',
454
				'type' => 'string', 'default' => '', 'required' => false,
455
			],
456
			'customer.address.longitude' => [
457
				'label' => 'Longitude of the customer location as float value',
458
				'type' => 'float', 'default' => '', 'required' => false,
459
			],
460
			'customer.address.latitude' => [
461
				'label' => 'Latitude of the customer location as float value',
462
				'type' => 'float', 'default' => '', 'required' => false,
463
			],
464
			'customer.address.birthday' => [
465
				'label' => 'ISO date in YYYY-MM-DD format of the birthday',
466
				'type' => 'string', 'default' => '', 'required' => false,
467
			],
468
		];
469
470
		$tplconf = 'client/jsonapi/template-options';
471
		$default = 'options-standard';
472
473
		$body = $view->render( $view->config( $tplconf, $default ) );
474
475
		return $response->withHeader( 'Allow', 'DELETE,GET,OPTIONS,PATCH,POST' )
476
			->withHeader( 'Cache-Control', 'max-age=300' )
477
			->withHeader( 'Content-Type', 'application/vnd.api+json' )
478
			->withBody( $view->response()->createStreamFromString( $body ) )
479
			->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

479
			->/** @scrutinizer ignore-call */ withStatus( 200 );
Loading history...
480
	}
481
482
483
	/**
484
	 * Returns the response object with the rendered header and body
485
	 *
486
	 * @param \Psr\Http\Message\ResponseInterface $response Response object
487
	 * @param \Aimeos\Base\View\Iface $view View instance
488
	 * @param int $status HTTP status code
489
	 * @return \Psr\Http\Message\ResponseInterface Modified response object
490
	 */
491
	protected function render( ResponseInterface $response, \Aimeos\Base\View\Iface $view, int $status ) : \Psr\Http\Message\ResponseInterface
492
	{
493
		/** client/jsonapi/customer/address/template
494
		 * Relative path to the customer address JSON API template
495
		 *
496
		 * The template file contains the code and processing instructions
497
		 * to generate the result shown in the JSON API body. The
498
		 * configuration string is the path to the template file relative
499
		 * to the templates directory (usually in templates/client/jsonapi).
500
		 *
501
		 * You can overwrite the template file configuration in extensions and
502
		 * provide alternative templates. These alternative templates should be
503
		 * named like the default one but with the string "standard" replaced by
504
		 * an unique name. You may use the name of your project for this. If
505
		 * you've implemented an alternative client class as well, "standard"
506
		 * should be replaced by the name of the new class.
507
		 *
508
		 * @param string Relative path to the template creating the body for the JSON API
509
		 * @since 2017.07
510
		 * @category Developer
511
		 */
512
		$tplconf = 'client/jsonapi/customer/address/template';
513
		$default = 'customer/address/standard';
514
515
		$body = $view->render( $view->config( $tplconf, $default ) );
516
517
		return $response->withHeader( 'Allow', 'DELETE,GET,OPTIONS,PATCH,POST' )
518
			->withHeader( 'Cache-Control', 'no-cache, private' )
519
			->withHeader( 'Content-Type', 'application/vnd.api+json' )
520
			->withBody( $view->response()->createStreamFromString( $body ) )
521
			->withStatus( $status );
522
	}
523
}
524