Standard::post()   B
last analyzed

Complexity

Conditions 8
Paths 31

Size

Total Lines 34
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

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

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