Completed
Push — master ( e8440a...f62092 )
by Aimeos
02:50
created

Standard::options()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 112
Code Lines 82

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 112
rs 8.2857
cc 1
eloc 82
nc 1
nop 3

How to fix   Long Method   

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
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
	private $controller;
28
29
30
	/**
31
	 * Initializes the client
32
	 *
33
	 * @param \Aimeos\MShop\Context\Item\Iface $context MShop context object
34
	 * @param \Aimeos\MW\View\Iface $view View object
35
	 * @param array $templatePaths List of file system paths where the templates are stored
36
	 * @param string $path Name of the client, e.g "customer/address"
37
	 */
38
	public function __construct( \Aimeos\MShop\Context\Item\Iface $context, \Aimeos\MW\View\Iface $view, array $templatePaths, $path )
39
	{
40
		parent::__construct( $context, $view, $templatePaths, $path );
41
42
		$this->controller = \Aimeos\Controller\Frontend\Customer\Factory::createController( $this->getContext() );
43
	}
44
45
46
	/**
47
	 * Deletes the resource or the resource list
48
	 *
49
	 * @param \Psr\Http\Message\ServerRequestInterface $request Request object
50
	 * @param \Psr\Http\Message\ResponseInterface $response Response object
51
	 * @return \Psr\Http\Message\ResponseInterface Modified response object
52
	 */
53
	public function delete( ServerRequestInterface $request, ResponseInterface $response )
54
	{
55
		$view = $this->getView();
56
57
		try
58
		{
59
			$relId = $view->param( 'relatedid' );
60
			$body = (string) $request->getBody();
61
62
			if( $relId === '' || $relId === null )
63
			{
64
				if( ( $payload = json_decode( $body ) ) === null || !isset( $payload->data ) ) {
65
					throw new \Aimeos\Client\JsonApi\Exception( sprintf( 'Invalid JSON in body' ), 400 );
66
				}
67
68
				if( !is_array( $payload->data ) ) {
69
					$payload->data = [$payload->data];
70
				}
71
72
				foreach( $payload->data as $entry )
73
				{
74
					if( !isset( $entry->id ) ) {
75
						throw new \Aimeos\Client\JsonApi\Exception( sprintf( 'ID is missing' ), 400 );
76
					}
77
78
					$this->controller->deleteAddressItem( $entry->id );
79
				}
80
			}
81
			else
82
			{
83
				$this->controller->deleteAddressItem( $relId );
84
			}
85
86
			$status = 200;
87
		}
88
		catch( \Aimeos\Controller\Frontend\Customer\Exception $e )
89
		{
90
			$status = 403;
91
			$view->errors = $this->getErrorDetails( $e, 'controller/frontend' );
92
		}
93
		catch( \Aimeos\MShop\Exception $e )
94
		{
95
			$status = 404;
96
			$view->errors = $this->getErrorDetails( $e, 'mshop' );
97
		}
98
		catch( \Exception $e )
99
		{
100
			$status = 500;
101
			$view->errors = $this->getErrorDetails( $e );
102
		}
103
104
		return $this->render( $response, $view, $status );
105
	}
106
107
108
	/**
109
	 * Returns the resource or the resource list
110
	 *
111
	 * @param \Psr\Http\Message\ServerRequestInterface $request Request object
112
	 * @param \Psr\Http\Message\ResponseInterface $response Response object
113
	 * @return \Psr\Http\Message\ResponseInterface Modified response object
114
	 */
115
	public function get( ServerRequestInterface $request, ResponseInterface $response )
116
	{
117
		$view = $this->getView();
118
119
		try
120
		{
121
			$relId = $view->param( 'relatedid' );
122
			$cntl = \Aimeos\Controller\Frontend\Factory::createController( $this->getContext(), 'customer' );
123
124
			if( $relId == null ) {
125
				$view->items = $cntl->getItem( $view->param( 'id' ), ['customer/address'] )->getAddressItems();
126
			} else {
127
				$view->items = $cntl->getAddressItem( $relId );
128
			}
129
130
			$view->total = count( $view->items );
131
			$status = 200;
132
		}
133
		catch( \Aimeos\Controller\Frontend\Customer\Exception $e )
134
		{
135
			$status = 403;
136
			$view->errors = $this->getErrorDetails( $e, 'controller/frontend' );
137
		}
138
		catch( \Aimeos\MShop\Exception $e )
139
		{
140
			$status = 404;
141
			$view->errors = $this->getErrorDetails( $e, 'mshop' );
142
		}
143
		catch( \Exception $e )
144
		{
145
			$status = 500;
146
			$view->errors = $this->getErrorDetails( $e );
147
		}
148
149
		return $this->render( $response, $view, $status );
150
	}
151
152
153
	/**
154
	 * Updates the resource or the resource list partitially
155
	 *
156
	 * @param \Psr\Http\Message\ServerRequestInterface $request Request object
157
	 * @param \Psr\Http\Message\ResponseInterface $response Response object
158
	 * @return \Psr\Http\Message\ResponseInterface Modified response object
159
	 */
160
	public function patch( ServerRequestInterface $request, ResponseInterface $response )
161
	{
162
		$view = $this->getView();
163
164
		try
165
		{
166
			$body = (string) $request->getBody();
167
168
			if( ( $payload = json_decode( $body ) ) === null || !isset( $payload->data->attributes ) ) {
169
				throw new \Aimeos\Client\JsonApi\Exception( sprintf( 'Invalid JSON in body' ), 400 );
170
			}
171
172
			$cntl = \Aimeos\Controller\Frontend\Factory::createController( $this->getContext(), 'customer' );
173
174
			$view->items = $cntl->editAddressItem( $view->param( 'relatedid' ), (array) $payload->data->attributes );
175
			$view->total = 1;
176
			$status = 200;
177
		}
178
		catch( \Aimeos\Controller\Frontend\Customer\Exception $e )
179
		{
180
			$status = 403;
181
			$view->errors = $this->getErrorDetails( $e, 'controller/frontend' );
182
		}
183
		catch( \Aimeos\MShop\Exception $e )
184
		{
185
			$status = 404;
186
			$view->errors = $this->getErrorDetails( $e, 'mshop' );
187
		}
188
		catch( \Exception $e )
189
		{
190
			$status = 500;
191
			$view->errors = $this->getErrorDetails( $e );
192
		}
193
194
		return $this->render( $response, $view, $status );
195
	}
196
197
198
	/**
199
	 * Creates or updates the resource or the resource list
200
	 *
201
	 * @param \Psr\Http\Message\ServerRequestInterface $request Request object
202
	 * @param \Psr\Http\Message\ResponseInterface $response Response object
203
	 * @return \Psr\Http\Message\ResponseInterface Modified response object
204
	 */
205
	public function post( ServerRequestInterface $request, ResponseInterface $response )
206
	{
207
		$view = $this->getView();
208
209
		try
210
		{
211
			$list = [];
212
			$body = (string) $request->getBody();
213
214
			if( ( $payload = json_decode( $body ) ) === null || !isset( $payload->data ) ) {
215
				throw new \Aimeos\Client\JsonApi\Exception( sprintf( 'Invalid JSON in body' ), 400 );
216
			}
217
218
			if( !is_array( $payload->data ) ) {
219
				$payload->data = [$payload->data];
220
			}
221
222
			foreach( $payload->data as $entry )
223
			{
224
				if( !isset( $entry->attributes ) ) {
225
					throw new \Aimeos\Client\JsonApi\Exception( sprintf( 'Attributes are missing' ) );
226
				}
227
228
				$list[] = $this->controller->addAddressItem( (array) $entry->attributes );
229
			}
230
231
232
			$view->total = count( $list );
233
			$view->items = $list;
234
			$status = 201;
235
		}
236
		catch( \Aimeos\Controller\Frontend\Customer\Exception $e )
237
		{
238
			$status = 403;
239
			$view->errors = $this->getErrorDetails( $e, 'controller/frontend' );
240
		}
241
		catch( \Aimeos\MShop\Exception $e )
242
		{
243
			$status = 404;
244
			$view->errors = $this->getErrorDetails( $e, 'mshop' );
245
		}
246
		catch( \Exception $e )
247
		{
248
			$status = 500;
249
			$view->errors = $this->getErrorDetails( $e );
250
		}
251
252
		return $this->render( $response, $view, $status );
253
	}
254
255
256
	/**
257
	 * Returns the available REST verbs and the available parameters
258
	 *
259
	 * @param \Psr\Http\Message\ServerRequestInterface $request Request object
260
	 * @param \Psr\Http\Message\ResponseInterface $response Response object
261
	 * @param string|null $prefix Form parameter prefix when nesting parameters is required
262
	 * @return \Psr\Http\Message\ResponseInterface Modified response object
263
	 */
264
	public function options( ServerRequestInterface $request, ResponseInterface $response, $prefix = null )
265
	{
266
		$view = $this->getView();
267
		$view->attributes = [
268
			'customer.address.salutation' => [
269
				'label' => 'Customer salutation, i.e. "comany" ,"mr", "mrs", "miss" or ""',
270
				'type' => 'string', 'default' => '', 'required' => false,
271
			],
272
			'customer.address.company' => [
273
				'label' => 'Company name',
274
				'type' => 'string', 'default' => '', 'required' => false,
275
			],
276
			'customer.address.vatid' => [
277
				'label' => 'VAT ID of the company',
278
				'type' => 'string', 'default' => '', 'required' => false,
279
			],
280
			'customer.address.title' => [
281
				'label' => 'Title of the customer',
282
				'type' => 'string', 'default' => '', 'required' => false,
283
			],
284
			'customer.address.firstname' => [
285
				'label' => 'First name of the customer',
286
				'type' => 'string', 'default' => '', 'required' => false,
287
			],
288
			'customer.address.lastname' => [
289
				'label' => 'Last name of the customer or full name',
290
				'type' => 'string', 'default' => '', 'required' => true,
291
			],
292
			'customer.address.address1' => [
293
				'label' => 'First address part like street',
294
				'type' => 'string', 'default' => '', 'required' => true,
295
			],
296
			'customer.address.address2' => [
297
				'label' => 'Second address part like house number',
298
				'type' => 'string', 'default' => '', 'required' => false,
299
			],
300
			'customer.address.address3' => [
301
				'label' => 'Third address part like flat number',
302
				'type' => 'string', 'default' => '', 'required' => false,
303
			],
304
			'customer.address.postal' => [
305
				'label' => 'Zip code of the city',
306
				'type' => 'string', 'default' => '', 'required' => false,
307
			],
308
			'customer.address.city' => [
309
				'label' => 'Name of the town/city',
310
				'type' => 'string', 'default' => '', 'required' => true,
311
			],
312
			'customer.address.state' => [
313
				'label' => 'Two letter code of the country state',
314
				'type' => 'string', 'default' => '', 'required' => false,
315
			],
316
			'customer.address.countryid' => [
317
				'label' => 'Two letter ISO country code',
318
				'type' => 'string', 'default' => '', 'required' => true,
319
			],
320
			'customer.address.languageid' => [
321
				'label' => 'Two or five letter ISO language code, e.g. "de" or "de_CH"',
322
				'type' => 'string', 'default' => '', 'required' => false,
323
			],
324
			'customer.address.telephone' => [
325
				'label' => 'Telephone number consisting of option leading "+" and digits without spaces',
326
				'type' => 'string', 'default' => '', 'required' => false,
327
			],
328
			'customer.address.telefax' => [
329
				'label' => 'Faximile number consisting of option leading "+" and digits without spaces',
330
				'type' => 'string', 'default' => '', 'required' => false,
331
			],
332
			'customer.address.email' => [
333
				'label' => 'E-mail address',
334
				'type' => 'string', 'default' => '', 'required' => false,
335
			],
336
			'customer.address.website' => [
337
				'label' => 'Web site including "http://" or "https://"',
338
				'type' => 'string', 'default' => '', 'required' => false,
339
			],
340
			'customer.address.longitude' => [
341
				'label' => 'Longitude of the customer location as float value',
342
				'type' => 'float', 'default' => '', 'required' => false,
343
			],
344
			'customer.address.latitude' => [
345
				'label' => 'Latitude of the customer location as float value',
346
				'type' => 'float', 'default' => '', 'required' => false,
347
			],
348
			'customer.address.label' => [
349
				'label' => 'Label to identify the customer, usually the full name',
350
				'type' => 'string', 'default' => '', 'required' => true,
351
			],
352
			'customer.address.code' => [
353
				'label' => 'Unique customer identifier, usually e-mail address',
354
				'type' => 'string', 'default' => '', 'required' => true,
355
			],
356
			'customer.address.birthday' => [
357
				'label' => 'ISO date in YYYY-MM-DD format of the birthday',
358
				'type' => 'string', 'default' => '', 'required' => false,
359
			],
360
			'customer.address.status' => [
361
				'label' => 'Customer account status, i.e. "0" for disabled, "1" for enabled',
362
				'type' => 'integer', 'default' => '1', 'required' => false,
363
			],
364
		];
365
366
		$tplconf = 'client/jsonapi/standard/template-options';
367
		$default = 'options-standard.php';
368
369
		$body = $view->render( $view->config( $tplconf, $default ) );
370
371
		return $response->withHeader( 'Allow', 'DELETE,GET,OPTIONS,PATCH,POST' )
372
			->withHeader( 'Content-Type', 'application/vnd.api+json' )
373
			->withBody( $view->response()->createStreamFromString( $body ) )
374
			->withStatus( 200 );
375
	}
376
377
378
	/**
379
	 * Returns the response object with the rendered header and body
380
	 *
381
	 * @param \Psr\Http\Message\ResponseInterface $response Response object
382
	 * @param \Aimeos\MW\View\Iface $view View instance
383
	 * @param integer $status HTTP status code
384
	 * @return \Psr\Http\Message\ResponseInterface Modified response object
385
	 */
386
	protected function render( ResponseInterface $response, \Aimeos\MW\View\Iface $view, $status )
387
	{
388
		$tplconf = 'client/jsonapi/customer/address/standard/template';
389
		$default = 'customer/address/standard.php';
390
391
		$body = $view->render( $view->config( $tplconf, $default ) );
392
393
		return $response->withHeader( 'Allow', 'DELETE,GET,OPTIONS,PATCH,POST' )
394
			->withHeader( 'Content-Type', 'application/vnd.api+json' )
395
			->withBody( $view->response()->createStreamFromString( $body ) )
396
			->withStatus( $status );
397
	}
398
}
399