Passed
Push — master ( 0de2f4...fcf15e )
by Aimeos
02:41
created

Standard::delete()   C

Complexity

Conditions 12
Paths 75

Size

Total Lines 59
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 30
dl 0
loc 59
rs 6.9666
c 0
b 0
f 0
cc 12
nc 75
nop 2

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-2018
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
	/**
28
	 * Deletes the resource or the resource list
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 )
35
	{
36
		$view = $this->getView();
37
38
		try
39
		{
40
			$body = (string) $request->getBody();
41
			$cntl = \Aimeos\Controller\Frontend::create( $this->getContext(), 'customer' );
42
			$items = $cntl->use( ['customer/address'] )->get()->getAddressItems();
1 ignored issue
show
Bug introduced by
The method use() does not exist on Aimeos\Controller\Frontend\Iface. It seems like you code against a sub-type of said class. However, the method does not exist in Aimeos\Controller\Frontend\Common\Iface or Aimeos\Controller\Frontend\Common\Decorator\Iface or Aimeos\Controller\Fronte...ommon\Decorator\Example. Are you sure you never get one of those? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

42
			$items = $cntl->/** @scrutinizer ignore-call */ use( ['customer/address'] )->get()->getAddressItems();
Loading history...
43
44
			if( ( $relId = $view->param( 'relatedid' ) ) === null )
45
			{
46
				if( ( $payload = json_decode( $body ) ) === null || !isset( $payload->data ) ) {
47
					throw new \Aimeos\Client\JsonApi\Exception( sprintf( 'Invalid JSON in body' ), 400 );
48
				}
49
50
				if( !is_array( $payload->data ) ) {
51
					$payload->data = [$payload->data];
52
				}
53
54
				foreach( $payload->data as $entry )
55
				{
56
					if( !isset( $entry->id ) ) {
57
						throw new \Aimeos\Client\JsonApi\Exception( sprintf( 'ID is missing' ), 400 );
58
					}
59
60
					if( isset( $items[$entry->id] ) ) {
61
						$cntl->deleteAddressItem( $items[$entry->id] );
1 ignored issue
show
Bug introduced by
The method deleteAddressItem() does not exist on Aimeos\Controller\Frontend\Iface. It seems like you code against a sub-type of said class. However, the method does not exist in Aimeos\Controller\Frontend\Common\Iface or Aimeos\Controller\Frontend\Common\Decorator\Iface or Aimeos\Controller\Fronte...ommon\Decorator\Example. Are you sure you never get one of those? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

61
						$cntl->/** @scrutinizer ignore-call */ 
62
             deleteAddressItem( $items[$entry->id] );
Loading history...
62
					}
63
				}
64
65
				$cntl->store();
1 ignored issue
show
Bug introduced by
The method store() does not exist on Aimeos\Controller\Frontend\Iface. It seems like you code against a sub-type of said class. However, the method does not exist in Aimeos\Controller\Frontend\Common\Iface or Aimeos\Controller\Frontend\Common\Decorator\Iface or Aimeos\Controller\Fronte...ommon\Decorator\Example. Are you sure you never get one of those? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

65
				$cntl->/** @scrutinizer ignore-call */ 
66
           store();
Loading history...
66
			}
67
			else
68
			{
69
				if( isset( $items[$relId] ) ) {
70
					$cntl->deleteAddressItem( $items[$relId] )->store();
71
				}
72
			}
73
74
			$status = 200;
75
		}
76
		catch( \Aimeos\Controller\Frontend\Customer\Exception $e )
77
		{
78
			$status = 403;
79
			$view->errors = $this->getErrorDetails( $e, 'controller/frontend' );
80
		}
81
		catch( \Aimeos\MShop\Exception $e )
82
		{
83
			$status = 404;
84
			$view->errors = $this->getErrorDetails( $e, 'mshop' );
85
		}
86
		catch( \Exception $e )
87
		{
88
			$status = 500;
89
			$view->errors = $this->getErrorDetails( $e );
90
		}
91
92
		return $this->render( $response, $view, $status );
93
	}
94
95
96
	/**
97
	 * Returns the resource or the resource list
98
	 *
99
	 * @param \Psr\Http\Message\ServerRequestInterface $request Request object
100
	 * @param \Psr\Http\Message\ResponseInterface $response Response object
101
	 * @return \Psr\Http\Message\ResponseInterface Modified response object
102
	 */
103
	public function get( ServerRequestInterface $request, ResponseInterface $response )
104
	{
105
		$view = $this->getView();
106
107
		try
108
		{
109
			$cntl = \Aimeos\Controller\Frontend::create( $this->getContext(), 'customer' );
110
			$item = $cntl->use( ['customer/address'] )->get();
111
112
			if( ( $relId = $view->param( 'relatedid' ) ) == null )
113
			{
114
				$view->items = $item->getAddressItems();
115
				$view->total = count( $view->items );
116
			}
117
			else
118
			{
119
				$view->items = $item->getAddressItem( $relId );
120
				$view->total = empty( $view->items ) ? 0 : 1;
121
			}
122
123
			$status = 200;
124
		}
125
		catch( \Aimeos\Controller\Frontend\Customer\Exception $e )
126
		{
127
			$status = 403;
128
			$view->errors = $this->getErrorDetails( $e, 'controller/frontend' );
129
		}
130
		catch( \Aimeos\MShop\Exception $e )
131
		{
132
			$status = 404;
133
			$view->errors = $this->getErrorDetails( $e, 'mshop' );
134
		}
135
		catch( \Exception $e )
136
		{
137
			$status = 500;
138
			$view->errors = $this->getErrorDetails( $e );
139
		}
140
141
		return $this->render( $response, $view, $status );
142
	}
143
144
145
	/**
146
	 * Updates the resource or the resource list partitially
147
	 *
148
	 * @param \Psr\Http\Message\ServerRequestInterface $request Request object
149
	 * @param \Psr\Http\Message\ResponseInterface $response Response object
150
	 * @return \Psr\Http\Message\ResponseInterface Modified response object
151
	 */
152
	public function patch( ServerRequestInterface $request, ResponseInterface $response )
153
	{
154
		$view = $this->getView();
155
156
		try
157
		{
158
			$body = (string) $request->getBody();
159
160
			if( ( $payload = json_decode( $body ) ) === null || !isset( $payload->data->attributes ) ) {
161
				throw new \Aimeos\Client\JsonApi\Exception( sprintf( 'Invalid JSON in body' ), 400 );
162
			}
163
164
			$status = 404;
165
			$view->total = 0;
166
			$id = $view->param( 'relatedid' );
167
			$cntl = \Aimeos\Controller\Frontend::create( $this->getContext(), 'customer' );
168
169
			if( ( $item = $cntl->use( ['customer/address'] )->get()->getAddressItem( $id ) ) !== null )
170
			{
171
				$attributes = (array) $payload->data->attributes;
172
				$item = $item->fromArray( $attributes );
173
				$cntl->addAddressItem( $item, $id )->store();
1 ignored issue
show
Bug introduced by
The method addAddressItem() does not exist on Aimeos\Controller\Frontend\Iface. It seems like you code against a sub-type of said class. However, the method does not exist in Aimeos\Controller\Frontend\Common\Iface or Aimeos\Controller\Frontend\Common\Decorator\Iface or Aimeos\Controller\Fronte...ommon\Decorator\Example. Are you sure you never get one of those? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

173
				$cntl->/** @scrutinizer ignore-call */ 
174
           addAddressItem( $item, $id )->store();
Loading history...
174
175
				$view->items = $item;
176
				$view->total = 1;
177
				$status = 200;
178
			}
179
		}
180
		catch( \Aimeos\Controller\Frontend\Customer\Exception $e )
181
		{
182
			$status = 403;
183
			$view->errors = $this->getErrorDetails( $e, 'controller/frontend' );
184
		}
185
		catch( \Aimeos\MShop\Exception $e )
186
		{
187
			$status = 404;
188
			$view->errors = $this->getErrorDetails( $e, 'mshop' );
189
		}
190
		catch( \Exception $e )
191
		{
192
			$status = 500;
193
			$view->errors = $this->getErrorDetails( $e );
194
		}
195
196
		return $this->render( $response, $view, $status );
197
	}
198
199
200
	/**
201
	 * Creates or updates the resource or the resource list
202
	 *
203
	 * @param \Psr\Http\Message\ServerRequestInterface $request Request object
204
	 * @param \Psr\Http\Message\ResponseInterface $response Response object
205
	 * @return \Psr\Http\Message\ResponseInterface Modified response object
206
	 */
207
	public function post( ServerRequestInterface $request, ResponseInterface $response )
208
	{
209
		$view = $this->getView();
210
211
		try
212
		{
213
			$body = (string) $request->getBody();
214
215
			if( ( $payload = json_decode( $body ) ) === null || !isset( $payload->data ) ) {
216
				throw new \Aimeos\Client\JsonApi\Exception( sprintf( 'Invalid JSON in body' ), 400 );
217
			}
218
219
			if( !is_array( $payload->data ) ) {
220
				$payload->data = [$payload->data];
221
			}
222
223
			$cntl = \Aimeos\Controller\Frontend::create( $this->getContext(), 'customer' )->use( ['customer/address'] );
224
225
			foreach( $payload->data as $entry )
226
			{
227
				if( !isset( $entry->attributes ) ) {
228
					throw new \Aimeos\Client\JsonApi\Exception( sprintf( 'Attributes are missing' ) );
229
				}
230
231
				$addrItem = $cntl->createAddressItem( (array) $entry->attributes );
232
				$cntl->addAddressItem( $addrItem );
233
			}
234
235
			$view->items = $cntl->store()->get()->getAddressItems();
236
			$view->total = count( $view->items );
237
			$status = 201;
238
		}
239
		catch( \Aimeos\Controller\Frontend\Customer\Exception $e )
240
		{
241
			$status = 403;
242
			$view->errors = $this->getErrorDetails( $e, 'controller/frontend' );
243
		}
244
		catch( \Aimeos\MShop\Exception $e )
245
		{
246
			$status = 404;
247
			$view->errors = $this->getErrorDetails( $e, 'mshop' );
248
		}
249
		catch( \Exception $e )
250
		{
251
			$status = 500;
252
			$view->errors = $this->getErrorDetails( $e );
253
		}
254
255
		return $this->render( $response, $view, $status );
256
	}
257
258
259
	/**
260
	 * Returns the available REST verbs and the available parameters
261
	 *
262
	 * @param \Psr\Http\Message\ServerRequestInterface $request Request object
263
	 * @param \Psr\Http\Message\ResponseInterface $response Response object
264
	 * @return \Psr\Http\Message\ResponseInterface Modified response object
265
	 */
266
	public function options( ServerRequestInterface $request, ResponseInterface $response )
267
	{
268
		$view = $this->getView();
269
270
		$view->attributes = [
271
			'customer.address.salutation' => [
272
				'label' => 'Customer salutation, i.e. "comany" ,"mr", "mrs", "miss" or ""',
273
				'type' => 'string', 'default' => '', 'required' => false,
274
			],
275
			'customer.address.company' => [
276
				'label' => 'Company name',
277
				'type' => 'string', 'default' => '', 'required' => false,
278
			],
279
			'customer.address.vatid' => [
280
				'label' => 'VAT ID of the company',
281
				'type' => 'string', 'default' => '', 'required' => false,
282
			],
283
			'customer.address.title' => [
284
				'label' => 'Title of the customer',
285
				'type' => 'string', 'default' => '', 'required' => false,
286
			],
287
			'customer.address.firstname' => [
288
				'label' => 'First name of the customer',
289
				'type' => 'string', 'default' => '', 'required' => false,
290
			],
291
			'customer.address.lastname' => [
292
				'label' => 'Last name of the customer or full name',
293
				'type' => 'string', 'default' => '', 'required' => true,
294
			],
295
			'customer.address.address1' => [
296
				'label' => 'First address part like street',
297
				'type' => 'string', 'default' => '', 'required' => true,
298
			],
299
			'customer.address.address2' => [
300
				'label' => 'Second address part like house number',
301
				'type' => 'string', 'default' => '', 'required' => false,
302
			],
303
			'customer.address.address3' => [
304
				'label' => 'Third address part like flat number',
305
				'type' => 'string', 'default' => '', 'required' => false,
306
			],
307
			'customer.address.postal' => [
308
				'label' => 'Zip code of the city',
309
				'type' => 'string', 'default' => '', 'required' => false,
310
			],
311
			'customer.address.city' => [
312
				'label' => 'Name of the town/city',
313
				'type' => 'string', 'default' => '', 'required' => true,
314
			],
315
			'customer.address.state' => [
316
				'label' => 'Two letter code of the country state',
317
				'type' => 'string', 'default' => '', 'required' => false,
318
			],
319
			'customer.address.countryid' => [
320
				'label' => 'Two letter ISO country code',
321
				'type' => 'string', 'default' => '', 'required' => true,
322
			],
323
			'customer.address.languageid' => [
324
				'label' => 'Two or five letter ISO language code, e.g. "de" or "de_CH"',
325
				'type' => 'string', 'default' => '', 'required' => false,
326
			],
327
			'customer.address.telephone' => [
328
				'label' => 'Telephone number consisting of option leading "+" and digits without spaces',
329
				'type' => 'string', 'default' => '', 'required' => false,
330
			],
331
			'customer.address.telefax' => [
332
				'label' => 'Faximile number consisting of option leading "+" and digits without spaces',
333
				'type' => 'string', 'default' => '', 'required' => false,
334
			],
335
			'customer.address.email' => [
336
				'label' => 'E-mail address',
337
				'type' => 'string', 'default' => '', 'required' => false,
338
			],
339
			'customer.address.website' => [
340
				'label' => 'Web site including "http://" or "https://"',
341
				'type' => 'string', 'default' => '', 'required' => false,
342
			],
343
			'customer.address.longitude' => [
344
				'label' => 'Longitude of the customer location as float value',
345
				'type' => 'float', 'default' => '', 'required' => false,
346
			],
347
			'customer.address.latitude' => [
348
				'label' => 'Latitude of the customer location as float value',
349
				'type' => 'float', 'default' => '', 'required' => false,
350
			],
351
			'customer.address.label' => [
352
				'label' => 'Label to identify the customer, usually the full name',
353
				'type' => 'string', 'default' => '', 'required' => true,
354
			],
355
			'customer.address.code' => [
356
				'label' => 'Unique customer identifier, usually e-mail address',
357
				'type' => 'string', 'default' => '', 'required' => true,
358
			],
359
			'customer.address.birthday' => [
360
				'label' => 'ISO date in YYYY-MM-DD format of the birthday',
361
				'type' => 'string', 'default' => '', 'required' => false,
362
			],
363
			'customer.address.status' => [
364
				'label' => 'Customer account status, i.e. "0" for disabled, "1" for enabled',
365
				'type' => 'integer', 'default' => '1', 'required' => false,
366
			],
367
		];
368
369
		$tplconf = 'client/jsonapi/standard/template-options';
370
		$default = 'options-standard';
371
372
		$body = $view->render( $view->config( $tplconf, $default ) );
373
374
		return $response->withHeader( 'Allow', 'DELETE,GET,OPTIONS,PATCH,POST' )
375
			->withHeader( 'Cache-Control', 'max-age=300' )
376
			->withHeader( 'Content-Type', 'application/vnd.api+json' )
377
			->withBody( $view->response()->createStreamFromString( $body ) )
378
			->withStatus( 200 );
379
	}
380
381
382
	/**
383
	 * Returns the response object with the rendered header and body
384
	 *
385
	 * @param \Psr\Http\Message\ResponseInterface $response Response object
386
	 * @param \Aimeos\MW\View\Iface $view View instance
387
	 * @param integer $status HTTP status code
388
	 * @return \Psr\Http\Message\ResponseInterface Modified response object
389
	 */
390
	protected function render( ResponseInterface $response, \Aimeos\MW\View\Iface $view, $status )
391
	{
392
		/** client/jsonapi/customer/address/standard/template
393
		 * Relative path to the customer address JSON API template
394
		 *
395
		 * The template file contains the code and processing instructions
396
		 * to generate the result shown in the JSON API body. The
397
		 * configuration string is the path to the template file relative
398
		 * to the templates directory (usually in client/jsonapi/templates).
399
		 *
400
		 * You can overwrite the template file configuration in extensions and
401
		 * provide alternative templates. These alternative templates should be
402
		 * named like the default one but with the string "standard" replaced by
403
		 * an unique name. You may use the name of your project for this. If
404
		 * you've implemented an alternative client class as well, "standard"
405
		 * should be replaced by the name of the new class.
406
		 *
407
		 * @param string Relative path to the template creating the body for the JSON API
408
		 * @since 2017.07
409
		 * @category Developer
410
		 */
411
		$tplconf = 'client/jsonapi/customer/address/standard/template';
412
		$default = 'customer/address/standard';
413
414
		$body = $view->render( $view->config( $tplconf, $default ) );
415
416
		return $response->withHeader( 'Allow', 'DELETE,GET,OPTIONS,PATCH,POST' )
417
			->withHeader( 'Cache-Control', 'no-cache, private' )
418
			->withHeader( 'Content-Type', 'application/vnd.api+json' )
419
			->withBody( $view->response()->createStreamFromString( $body ) )
420
			->withStatus( $status );
421
	}
422
}
423