Completed
Branch master (7caa83)
by Aimeos
03:01
created

Standard::post()   A

Complexity

Conditions 6
Paths 16

Size

Total Lines 35
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 19
dl 0
loc 35
c 0
b 0
f 0
rs 9.0111
cc 6
nc 16
nop 2
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;
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
	/**
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
			$cntl = \Aimeos\Controller\Frontend::create( $this->getContext(), 'customer' );
41
			$cntl->deleteItem( $view->param( 'id' ) );
1 ignored issue
show
Bug introduced by
The method deleteItem() 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

41
			$cntl->/** @scrutinizer ignore-call */ 
42
          deleteItem( $view->param( 'id' ) );
Loading history...
42
			$status = 200;
43
		}
44
		catch( \Aimeos\Controller\Frontend\Customer\Exception $e )
45
		{
46
			$status = 403;
47
			$view->errors = $this->getErrorDetails( $e, 'controller/frontend' );
48
		}
49
		catch( \Aimeos\MShop\Exception $e )
50
		{
51
			$status = 404;
52
			$view->errors = $this->getErrorDetails( $e, 'mshop' );
53
		}
54
		catch( \Exception $e )
55
		{
56
			$status = 500;
57
			$view->errors = $this->getErrorDetails( $e );
58
		}
59
60
		return $this->render( $response, $view, $status );
61
	}
62
63
64
	/**
65
	 * Returns the resource or the resource list
66
	 *
67
	 * @param \Psr\Http\Message\ServerRequestInterface $request Request object
68
	 * @param \Psr\Http\Message\ResponseInterface $response Response object
69
	 * @return \Psr\Http\Message\ResponseInterface Modified response object
70
	 */
71
	public function get( ServerRequestInterface $request, ResponseInterface $response )
72
	{
73
		$view = $this->getView();
74
75
		try
76
		{
77
			$ref = $view->param( 'include', [] );
78
79
			if( is_string( $ref ) ) {
80
				$ref = explode( ',', $ref );
81
			}
82
83
			$cntl = \Aimeos\Controller\Frontend::create( $this->getContext(), 'customer' );
84
85
			$view->item = $cntl->getItem( $view->param( 'id' ), $ref );
1 ignored issue
show
Bug introduced by
The method getItem() 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

85
			/** @scrutinizer ignore-call */ 
86
   $view->item = $cntl->getItem( $view->param( 'id' ), $ref );
Loading history...
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
	 * Updates the resource or the resource list partitially
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 patch( ServerRequestInterface $request, ResponseInterface $response )
116
	{
117
		$view = $this->getView();
118
119
		try
120
		{
121
			$body = (string) $request->getBody();
122
123
			if( ( $payload = json_decode( $body ) ) === null || !isset( $payload->data->attributes ) ) {
124
				throw new \Aimeos\Client\JsonApi\Exception( sprintf( 'Invalid JSON in body' ), 400 );
125
			}
126
127
			$cntl = \Aimeos\Controller\Frontend::create( $this->getContext(), 'customer' );
128
129
			$view->item = $cntl->editItem( $view->param( 'id' ), (array) $payload->data->attributes );
1 ignored issue
show
Bug introduced by
The method editItem() 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

129
			/** @scrutinizer ignore-call */ 
130
   $view->item = $cntl->editItem( $view->param( 'id' ), (array) $payload->data->attributes );
Loading history...
130
			$status = 200;
131
		}
132
		catch( \Aimeos\Controller\Frontend\Customer\Exception $e )
133
		{
134
			$status = 403;
135
			$view->errors = $this->getErrorDetails( $e, 'controller/frontend' );
136
		}
137
		catch( \Aimeos\MShop\Exception $e )
138
		{
139
			$status = 404;
140
			$view->errors = $this->getErrorDetails( $e, 'mshop' );
141
		}
142
		catch( \Exception $e )
143
		{
144
			$status = 500;
145
			$view->errors = $this->getErrorDetails( $e );
146
		}
147
148
		return $this->render( $response, $view, $status );
149
	}
150
151
152
	/**
153
	 * Creates or updates the resource or the resource list
154
	 *
155
	 * @param \Psr\Http\Message\ServerRequestInterface $request Request object
156
	 * @param \Psr\Http\Message\ResponseInterface $response Response object
157
	 * @return \Psr\Http\Message\ResponseInterface Modified response object
158
	 */
159
	public function post( ServerRequestInterface $request, ResponseInterface $response )
160
	{
161
		$view = $this->getView();
162
163
		try
164
		{
165
			$body = (string) $request->getBody();
166
167
			if( ( $payload = json_decode( $body ) ) === null || !isset( $payload->data->attributes ) ) {
168
				throw new \Aimeos\Client\JsonApi\Exception( sprintf( 'Invalid JSON in body' ), 400 );
169
			}
170
171
			$cntl = \Aimeos\Controller\Frontend::create( $this->getContext(), 'customer' );
172
173
			$view->item = $cntl->addItem( (array) $payload->data->attributes );
1 ignored issue
show
Bug introduced by
The method addItem() 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
			/** @scrutinizer ignore-call */ 
174
   $view->item = $cntl->addItem( (array) $payload->data->attributes );
Loading history...
174
			$view->nodata = true; // only expose customer ID to attackers
175
			$status = 201;
176
		}
177
		catch( \Aimeos\Controller\Frontend\Customer\Exception $e )
178
		{
179
			$status = 403;
180
			$view->errors = $this->getErrorDetails( $e, 'controller/frontend' );
181
		}
182
		catch( \Aimeos\MShop\Exception $e )
183
		{
184
			$status = 404;
185
			$view->errors = $this->getErrorDetails( $e, 'mshop' );
186
		}
187
		catch( \Exception $e )
188
		{
189
			$status = 500;
190
			$view->errors = $this->getErrorDetails( $e );
191
		}
192
193
		return $this->render( $response, $view, $status );
194
	}
195
196
197
	/**
198
	 * Returns the available REST verbs and the available parameters
199
	 *
200
	 * @param \Psr\Http\Message\ServerRequestInterface $request Request object
201
	 * @param \Psr\Http\Message\ResponseInterface $response Response object
202
	 * @return \Psr\Http\Message\ResponseInterface Modified response object
203
	 */
204
	public function options( ServerRequestInterface $request, ResponseInterface $response )
205
	{
206
		$view = $this->getView();
207
208
		$view->attributes = [
209
			'customer.salutation' => [
210
				'label' => 'Customer salutation, i.e. "comany" ,"mr", "mrs", "miss" or ""',
211
				'type' => 'string', 'default' => '', 'required' => false,
212
			],
213
			'customer.company' => [
214
				'label' => 'Company name',
215
				'type' => 'string', 'default' => '', 'required' => false,
216
			],
217
			'customer.vatid' => [
218
				'label' => 'VAT ID of the company',
219
				'type' => 'string', 'default' => '', 'required' => false,
220
			],
221
			'customer.title' => [
222
				'label' => 'Title of the customer',
223
				'type' => 'string', 'default' => '', 'required' => false,
224
			],
225
			'customer.firstname' => [
226
				'label' => 'First name of the customer',
227
				'type' => 'string', 'default' => '', 'required' => false,
228
			],
229
			'customer.lastname' => [
230
				'label' => 'Last name of the customer or full name',
231
				'type' => 'string', 'default' => '', 'required' => true,
232
			],
233
			'customer.address1' => [
234
				'label' => 'First address part like street',
235
				'type' => 'string', 'default' => '', 'required' => true,
236
			],
237
			'customer.address2' => [
238
				'label' => 'Second address part like house number',
239
				'type' => 'string', 'default' => '', 'required' => false,
240
			],
241
			'customer.address3' => [
242
				'label' => 'Third address part like flat number',
243
				'type' => 'string', 'default' => '', 'required' => false,
244
			],
245
			'customer.postal' => [
246
				'label' => 'Zip code of the city',
247
				'type' => 'string', 'default' => '', 'required' => false,
248
			],
249
			'customer.city' => [
250
				'label' => 'Name of the town/city',
251
				'type' => 'string', 'default' => '', 'required' => true,
252
			],
253
			'customer.state' => [
254
				'label' => 'Two letter code of the country state',
255
				'type' => 'string', 'default' => '', 'required' => false,
256
			],
257
			'customer.countryid' => [
258
				'label' => 'Two letter ISO country code',
259
				'type' => 'string', 'default' => '', 'required' => true,
260
			],
261
			'customer.languageid' => [
262
				'label' => 'Two or five letter ISO language code, e.g. "de" or "de_CH"',
263
				'type' => 'string', 'default' => '', 'required' => false,
264
			],
265
			'customer.telephone' => [
266
				'label' => 'Telephone number consisting of option leading "+" and digits without spaces',
267
				'type' => 'string', 'default' => '', 'required' => false,
268
			],
269
			'customer.telefax' => [
270
				'label' => 'Faximile number consisting of option leading "+" and digits without spaces',
271
				'type' => 'string', 'default' => '', 'required' => false,
272
			],
273
			'customer.email' => [
274
				'label' => 'E-mail address',
275
				'type' => 'string', 'default' => '', 'required' => false,
276
			],
277
			'customer.website' => [
278
				'label' => 'Web site including "http://" or "https://"',
279
				'type' => 'string', 'default' => '', 'required' => false,
280
			],
281
			'customer.longitude' => [
282
				'label' => 'Longitude of the customer location as float value',
283
				'type' => 'float', 'default' => '', 'required' => false,
284
			],
285
			'customer.latitude' => [
286
				'label' => 'Latitude of the customer location as float value',
287
				'type' => 'float', 'default' => '', 'required' => false,
288
			],
289
			'customer.label' => [
290
				'label' => 'Label to identify the customer, will be firstname, lastname and company if empty',
291
				'type' => 'string', 'default' => '', 'required' => true,
292
			],
293
			'customer.code' => [
294
				'label' => 'Unique customer identifier, will be the e-mail address if empty',
295
				'type' => 'string', 'default' => '', 'required' => false,
296
			],
297
			'customer.password' => [
298
				'label' => 'Password of the customer, generated if emtpy',
299
				'type' => 'string', 'default' => '', 'required' => false,
300
			],
301
			'customer.birthday' => [
302
				'label' => 'ISO date in YYYY-MM-DD format of the birthday',
303
				'type' => 'string', 'default' => '', 'required' => false,
304
			],
305
			'customer.status' => [
306
				'label' => 'Customer account status, i.e. "0" for disabled, "1" for enabled and is enabled by default',
307
				'type' => 'integer', 'default' => '1', 'required' => false,
308
			],
309
		];
310
311
		$tplconf = 'client/jsonapi/standard/template-options';
312
		$default = 'options-standard';
313
314
		$body = $view->render( $view->config( $tplconf, $default ) );
315
316
		return $response->withHeader( 'Allow', 'DELETE,GET,OPTIONS,PATCH,POST' )
317
			->withHeader( 'Cache-Control', 'max-age=300' )
318
			->withHeader( 'Content-Type', 'application/vnd.api+json' )
319
			->withBody( $view->response()->createStreamFromString( $body ) )
320
			->withStatus( 200 );
321
	}
322
323
324
	/**
325
	 * Returns the response object with the rendered header and body
326
	 *
327
	 * @param \Psr\Http\Message\ResponseInterface $response Response object
328
	 * @param \Aimeos\MW\View\Iface $view View instance
329
	 * @param integer $status HTTP status code
330
	 * @return \Psr\Http\Message\ResponseInterface Modified response object
331
	 */
332
	protected function render( ResponseInterface $response, \Aimeos\MW\View\Iface $view, $status )
333
	{
334
		/** client/jsonapi/customer/standard/template
335
		 * Relative path to the customer JSON API template
336
		 *
337
		 * The template file contains the code and processing instructions
338
		 * to generate the result shown in the JSON API body. The
339
		 * configuration string is the path to the template file relative
340
		 * to the templates directory (usually in client/jsonapi/templates).
341
		 *
342
		 * You can overwrite the template file configuration in extensions and
343
		 * provide alternative templates. These alternative templates should be
344
		 * named like the default one but with the string "standard" replaced by
345
		 * an unique name. You may use the name of your project for this. If
346
		 * you've implemented an alternative client class as well, "standard"
347
		 * should be replaced by the name of the new class.
348
		 *
349
		 * @param string Relative path to the template creating the body for the JSON API
350
		 * @since 2017.04
351
		 * @category Developer
352
		 */
353
		$tplconf = 'client/jsonapi/customer/standard/template';
354
		$default = 'customer/standard';
355
356
		$body = $view->render( $view->config( $tplconf, $default ) );
357
358
		return $response->withHeader( 'Allow', 'DELETE,GET,OPTIONS,PATCH,POST' )
359
			->withHeader( 'Cache-Control', 'no-cache, private' )
360
			->withHeader( 'Content-Type', 'application/vnd.api+json' )
361
			->withBody( $view->response()->createStreamFromString( $body ) )
362
			->withStatus( $status );
363
	}
364
}
365