Completed
Push — master ( fa87fb...106664 )
by Aimeos
05:31
created

Standard   B

Complexity

Total Complexity 41

Size/Duplication

Total Lines 638
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 10

Importance

Changes 0
Metric Value
wmc 41
lcom 1
cbo 10
dl 0
loc 638
rs 8.1812
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
B delete() 0 68 4
B get() 0 93 4
B patch() 0 69 4
B post() 0 69 4
B put() 0 43 1
B options() 0 73 4
B deleteItems() 0 24 5
B getItems() 0 33 4
B patchItems() 0 33 5
B postItems() 0 34 6

How to fix   Complexity   

Complex Class

Complex classes like Standard often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Standard, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2015-2016
6
 * @package Admin
7
 * @subpackage JsonAdm
8
 */
9
10
11
namespace Aimeos\Admin\JsonAdm;
12
13
use Psr\Http\Message\ServerRequestInterface;
14
use Psr\Http\Message\ResponseInterface;
15
16
17
/**
18
 * JSON API standard client
19
 *
20
 * @package Admin
21
 * @subpackage JsonAdm
22
 */
23
class Standard
24
	extends \Aimeos\Admin\JsonAdm\Base
0 ignored issues
show
Coding Style introduced by
The extends keyword must be on the same line as the class name
Loading history...
Coding Style introduced by
Expected 0 spaces between "Base" and comma; 1 found
Loading history...
25
	implements \Aimeos\Admin\JsonAdm\Common\Iface
0 ignored issues
show
Coding Style introduced by
The implements keyword must be on the same line as the class name
Loading history...
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
			$response = $this->deleteItems( $view, $request, $response );
41
			$status = 200;
42
		}
43
		catch( \Aimeos\Admin\JsonAdm\Exception $e )
44
		{
45
			$status = $e->getCode();
46
			$view->errors = array( array(
47
				'title' => $this->getContext()->getI18n()->dt( 'admin/jsonadm', $e->getMessage() ),
48
				'detail' => $e->getTraceAsString(),
49
			) );
50
		}
51
		catch( \Aimeos\MShop\Exception $e )
52
		{
53
			$status = 404;
54
			$view->errors = array( array(
55
				'title' => $this->getContext()->getI18n()->dt( 'mshop', $e->getMessage() ),
56
				'detail' => $e->getTraceAsString(),
57
			) );
58
		}
59
		catch( \Exception $e )
60
		{
61
			$status = 500;
62
			$view->errors = array( array(
63
				'title' => $e->getMessage(),
64
				'detail' => $e->getTraceAsString(),
65
			) );
66
		}
67
68
		/** admin/jsonadm/standard/template-delete
69
		 * Relative path to the JSON API template for DELETE requests
70
		 *
71
		 * The template file contains the code and processing instructions
72
		 * to generate the result shown in the JSON API body. The
73
		 * configuration string is the path to the template file relative
74
		 * to the templates directory (usually in admin/jsonadm/templates).
75
		 *
76
		 * You can overwrite the template file configuration in extensions and
77
		 * provide alternative templates. These alternative templates should be
78
		 * named like the default one but with the string "standard" replaced by
79
		 * an unique name. You may use the name of your project for this. If
80
		 * you've implemented an alternative client class as well, "standard"
81
		 * should be replaced by the name of the new class.
82
		 *
83
		 * @param string Relative path to the template creating the body for the DELETE method of the JSON API
84
		 * @since 2015.12
85
		 * @category Developer
86
		 * @see admin/jsonadm/standard/template-aggregate
87
		 * @see admin/jsonadm/standard/template-get
88
		 * @see admin/jsonadm/standard/template-patch
89
		 * @see admin/jsonadm/standard/template-post
90
		 * @see admin/jsonadm/standard/template-put
91
		 * @see admin/jsonadm/standard/template-options
92
		 */
93
		$tplconf = 'admin/jsonadm/standard/template-delete';
94
		$default = 'delete-default.php';
95
96
		$body = $view->render( $view->config( $tplconf, $default ) );
97
98
		return $response->withHeader( 'Content-Type', 'application/vnd.api+json; supported-ext="bulk"' )
99
			->withBody( $view->response()->createStreamFromString( $body ) )
100
			->withStatus( $status );
101
	}
102
103
104
	/**
105
	 * Returns the requested resource or the resource list
106
	 *
107
	 * @param \Psr\Http\Message\ServerRequestInterface $request Request object
108
	 * @param \Psr\Http\Message\ResponseInterface $response Response object
109
	 * @return \Psr\Http\Message\ResponseInterface Modified response object
110
	 */
111
	public function get( ServerRequestInterface $request, ResponseInterface $response )
112
	{
113
		$view = $this->getView();
114
115
		try
116
		{
117
			$response = $this->getItems( $view, $request, $response );
118
			$status = 200;
119
		}
120
		catch( \Aimeos\MShop\Exception $e )
121
		{
122
			$status = 404;
123
			$view->errors = array( array(
124
				'title' => $this->getContext()->getI18n()->dt( 'mshop', $e->getMessage() ),
125
				'detail' => $e->getTraceAsString(),
126
			) );
127
		}
128
		catch( \Exception $e )
129
		{
130
			$status = 500;
131
			$view->errors = array( array(
132
				'title' => $e->getMessage(),
133
				'detail' => $e->getTraceAsString(),
134
			) );
135
		}
136
137
		if( $view->param( 'aggregate' ) !== null )
138
		{
139
			/** admin/jsonadm/standard/template-aggregate
140
			 * Relative path to the JSON API template for GET aggregate requests
141
			 *
142
			 * The template file contains the code and processing instructions
143
			 * to generate the result shown in the JSON API body. The
144
			 * configuration string is the path to the template file relative
145
			 * to the templates directory (usually in admin/jsonadm/templates).
146
			 *
147
			 * You can overwrite the template file configuration in extensions and
148
			 * provide alternative templates. These alternative templates should be
149
			 * named like the default one but with the string "standard" replaced by
150
			 * an unique name. You may use the name of your project for this. If
151
			 * you've implemented an alternative client class as well, "standard"
152
			 * should be replaced by the name of the new class.
153
			 *
154
			 * @param string Relative path to the template creating the body for the GET aggregate request of the JSON API
155
			 * @since 2016.07
156
			 * @category Developer
157
			 * @see admin/jsonadm/standard/template-delete
158
			 * @see admin/jsonadm/standard/template-get
159
			 * @see admin/jsonadm/standard/template-patch
160
			 * @see admin/jsonadm/standard/template-post
161
			 * @see admin/jsonadm/standard/template-put
162
			 * @see admin/jsonadm/standard/template-options
163
			 */
164
			$tplconf = 'admin/jsonadm/standard/template-aggregate';
165
			$default = 'aggregate-default.php';
166
		}
167
		else
168
		{
169
			/** admin/jsonadm/standard/template-get
170
			 * Relative path to the JSON API template for GET requests
171
			 *
172
			 * The template file contains the code and processing instructions
173
			 * to generate the result shown in the JSON API body. The
174
			 * configuration string is the path to the template file relative
175
			 * to the templates directory (usually in admin/jsonadm/templates).
176
			 *
177
			 * You can overwrite the template file configuration in extensions and
178
			 * provide alternative templates. These alternative templates should be
179
			 * named like the default one but with the string "standard" replaced by
180
			 * an unique name. You may use the name of your project for this. If
181
			 * you've implemented an alternative client class as well, "standard"
182
			 * should be replaced by the name of the new class.
183
			 *
184
			 * @param string Relative path to the template creating the body for the GET method of the JSON API
185
			 * @since 2015.12
186
			 * @category Developer
187
			 * @see admin/jsonadm/standard/template-aggregate
188
			 * @see admin/jsonadm/standard/template-delete
189
			 * @see admin/jsonadm/standard/template-patch
190
			 * @see admin/jsonadm/standard/template-post
191
			 * @see admin/jsonadm/standard/template-put
192
			 * @see admin/jsonadm/standard/template-options
193
			 */
194
			$tplconf = 'admin/jsonadm/standard/template-get';
195
			$default = 'get-default.php';
196
		}
197
198
		$body = $view->render( $view->config( $tplconf, $default ) );
199
200
		return $response->withHeader( 'Content-Type', 'application/vnd.api+json; supported-ext="bulk"' )
201
			->withBody( $view->response()->createStreamFromString( $body ) )
202
			->withStatus( $status );
203
	}
204
205
206
	/**
207
	 * Updates the resource or the resource list partitially
208
	 *
209
	 * @param \Psr\Http\Message\ServerRequestInterface $request Request object
210
	 * @param \Psr\Http\Message\ResponseInterface $response Response object
211
	 * @return \Psr\Http\Message\ResponseInterface Modified response object
212
	 */
213
	public function patch( ServerRequestInterface $request, ResponseInterface $response )
214
	{
215
		$header = array( 'Content-Type' => 'application/vnd.api+json; supported-ext="bulk"' );
0 ignored issues
show
Unused Code introduced by
$header is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
216
		$view = $this->getView();
217
218
		try
219
		{
220
			$response = $this->patchItems( $view, $request, $response );
221
			$status = 200;
222
		}
223
		catch( \Aimeos\Admin\JsonAdm\Exception $e )
224
		{
225
			$status = $e->getCode();
226
			$view->errors = array( array(
227
				'title' => $this->getContext()->getI18n()->dt( 'admin/jsonadm', $e->getMessage() ),
228
				'detail' => $e->getTraceAsString(),
229
			) );
230
		}
231
		catch( \Aimeos\MShop\Exception $e )
232
		{
233
			$status = 404;
234
			$view->errors = array( array(
235
				'title' => $this->getContext()->getI18n()->dt( 'mshop', $e->getMessage() ),
236
				'detail' => $e->getTraceAsString(),
237
			) );
238
		}
239
		catch( \Exception $e )
240
		{
241
			$status = 500;
242
			$view->errors = array( array(
243
				'title' => $e->getMessage(),
244
				'detail' => $e->getTraceAsString(),
245
			) );
246
		}
247
248
		/** admin/jsonadm/standard/template-patch
249
		 * Relative path to the JSON API template for PATCH requests
250
		 *
251
		 * The template file contains the code and processing instructions
252
		 * to generate the result shown in the JSON API body. The
253
		 * configuration string is the path to the template file relative
254
		 * to the templates directory (usually in admin/jsonadm/templates).
255
		 *
256
		 * You can overwrite the template file configuration in extensions and
257
		 * provide alternative templates. These alternative templates should be
258
		 * named like the default one but with the string "standard" replaced by
259
		 * an unique name. You may use the name of your project for this. If
260
		 * you've implemented an alternative client class as well, "standard"
261
		 * should be replaced by the name of the new class.
262
		 *
263
		 * @param string Relative path to the template creating the body for the PATCH method of the JSON API
264
		 * @since 2015.12
265
		 * @category Developer
266
		 * @see admin/jsonadm/standard/template-aggregate
267
		 * @see admin/jsonadm/standard/template-get
268
		 * @see admin/jsonadm/standard/template-post
269
		 * @see admin/jsonadm/standard/template-delete
270
		 * @see admin/jsonadm/standard/template-put
271
		 * @see admin/jsonadm/standard/template-options
272
		 */
273
		$tplconf = 'admin/jsonadm/standard/template-patch';
274
		$default = 'patch-default.php';
275
276
		$body = $view->render( $view->config( $tplconf, $default ) );
277
278
		return $response->withHeader( 'Content-Type', 'application/vnd.api+json; supported-ext="bulk"' )
279
			->withBody( $view->response()->createStreamFromString( $body ) )
280
			->withStatus( $status );
281
	}
282
283
284
	/**
285
	 * Creates or updates the resource or the resource list
286
	 *
287
	 * @param \Psr\Http\Message\ServerRequestInterface $request Request object
288
	 * @param \Psr\Http\Message\ResponseInterface $response Response object
289
	 * @return \Psr\Http\Message\ResponseInterface Modified response object
290
	 */
291
	public function post( ServerRequestInterface $request, ResponseInterface $response )
292
	{
293
		$header = array( 'Content-Type' => 'application/vnd.api+json; supported-ext="bulk"' );
0 ignored issues
show
Unused Code introduced by
$header is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
294
		$view = $this->getView();
295
296
		try
297
		{
298
			$response = $this->postItems( $view, $request, $response );
299
			$status = 201;
300
		}
301
		catch( \Aimeos\Admin\JsonAdm\Exception $e )
302
		{
303
			$status = $e->getCode();
304
			$view->errors = array( array(
305
				'title' => $this->getContext()->getI18n()->dt( 'admin/jsonadm', $e->getMessage() ),
306
				'detail' => $e->getTraceAsString(),
307
			) );
308
		}
309
		catch( \Aimeos\MShop\Exception $e )
310
		{
311
			$status = 404;
312
			$view->errors = array( array(
313
				'title' => $this->getContext()->getI18n()->dt( 'mshop', $e->getMessage() ),
314
				'detail' => $e->getTraceAsString(),
315
			) );
316
		}
317
		catch( \Exception $e )
318
		{
319
			$status = 500;
320
			$view->errors = array( array(
321
				'title' => $e->getMessage(),
322
				'detail' => $e->getTraceAsString(),
323
			) );
324
		}
325
326
		/** admin/jsonadm/standard/template-post
327
		 * Relative path to the JSON API template for POST requests
328
		 *
329
		 * The template file contains the code and processing instructions
330
		 * to generate the result shown in the JSON API body. The
331
		 * configuration string is the path to the template file relative
332
		 * to the templates directory (usually in admin/jsonadm/templates).
333
		 *
334
		 * You can overwrite the template file configuration in extensions and
335
		 * provide alternative templates. These alternative templates should be
336
		 * named like the default one but with the string "standard" replaced by
337
		 * an unique name. You may use the name of your project for this. If
338
		 * you've implemented an alternative client class as well, "standard"
339
		 * should be replaced by the name of the new class.
340
		 *
341
		 * @param string Relative path to the template creating the body for the POST method of the JSON API
342
		 * @since 2015.12
343
		 * @category Developer
344
		 * @see admin/jsonadm/standard/template-aggregate
345
		 * @see admin/jsonadm/standard/template-get
346
		 * @see admin/jsonadm/standard/template-patch
347
		 * @see admin/jsonadm/standard/template-delete
348
		 * @see admin/jsonadm/standard/template-put
349
		 * @see admin/jsonadm/standard/template-options
350
		 */
351
		$tplconf = 'admin/jsonadm/standard/template-post';
352
		$default = 'post-default.php';
353
354
		$body = $view->render( $view->config( $tplconf, $default ) );
355
356
		return $response->withHeader( 'Content-Type', 'application/vnd.api+json; supported-ext="bulk"' )
357
			->withBody( $view->response()->createStreamFromString( $body ) )
358
			->withStatus( $status );
359
	}
360
361
362
	/**
363
	 * Creates or updates the resource or the resource list
364
	 *
365
	 * @param \Psr\Http\Message\ServerRequestInterface $request Request object
366
	 * @param \Psr\Http\Message\ResponseInterface $response Response object
367
	 * @return \Psr\Http\Message\ResponseInterface Modified response object
368
	 */
369
	public function put( ServerRequestInterface $request, ResponseInterface $response )
370
	{
371
		$status = 501;
372
		$view = $this->getView();
373
374
		$view->errors = array( array(
375
			'title' => $this->getContext()->getI18n()->dt( 'admin/jsonadm', 'Not implemented, use PATCH instead' ),
376
		) );
377
378
		/** admin/jsonadm/standard/template-put
379
		 * Relative path to the JSON API template for PUT requests
380
		 *
381
		 * The template file contains the code and processing instructions
382
		 * to generate the result shown in the JSON API body. The
383
		 * configuration string is the path to the template file relative
384
		 * to the templates directory (usually in admin/jsonadm/templates).
385
		 *
386
		 * You can overwrite the template file configuration in extensions and
387
		 * provide alternative templates. These alternative templates should be
388
		 * named like the default one but with the string "standard" replaced by
389
		 * an unique name. You may use the name of your project for this. If
390
		 * you've implemented an alternative client class as well, "standard"
391
		 * should be replaced by the name of the new class.
392
		 *
393
		 * @param string Relative path to the template creating the body for the PUT method of the JSON API
394
		 * @since 2015.12
395
		 * @category Developer
396
		 * @see admin/jsonadm/standard/template-aggregate
397
		 * @see admin/jsonadm/standard/template-delete
398
		 * @see admin/jsonadm/standard/template-patch
399
		 * @see admin/jsonadm/standard/template-post
400
		 * @see admin/jsonadm/standard/template-get
401
		 * @see admin/jsonadm/standard/template-options
402
		 */
403
		$tplconf = 'admin/jsonadm/standard/template-put';
404
		$default = 'put-default.php';
405
406
		$body = $view->render( $view->config( $tplconf, $default ) );
407
408
		return $response->withHeader( 'Content-Type', 'application/vnd.api+json; supported-ext="bulk"' )
409
			->withBody( $view->response()->createStreamFromString( $body ) )
410
			->withStatus( $status );
411
	}
412
413
414
	/**
415
	 * Returns the available REST verbs and the available resources
416
	 *
417
	 * @param \Psr\Http\Message\ServerRequestInterface $request Request object
418
	 * @param \Psr\Http\Message\ResponseInterface $response Response object
419
	 * @return \Psr\Http\Message\ResponseInterface Modified response object
420
	 */
421
	public function options( ServerRequestInterface $request, ResponseInterface $response )
422
	{
423
		$context = $this->getContext();
424
		$view = $this->getView();
425
426
		try
427
		{
428
			$resources = $attributes = array();
429
430
			foreach( $this->getDomains( $view ) as $domain )
431
			{
432
				$manager = \Aimeos\MShop\Factory::createManager( $context, $domain );
433
				$resources = array_merge( $resources, $manager->getResourceType( true ) );
434
				$attributes = array_merge( $attributes, $manager->getSearchAttributes( true ) );
435
			}
436
437
			$view->resources = $resources;
438
			$view->attributes = $attributes;
439
440
			$status = 200;
441
		}
442
		catch( \Aimeos\MShop\Exception $e )
443
		{
444
			$status = 404;
445
			$view->errors = array( array(
446
				'title' => $context->getI18n()->dt( 'mshop', $e->getMessage() ),
447
				'detail' => $e->getTraceAsString(),
448
			) );
449
		}
450
		catch( \Exception $e )
451
		{
452
			$status = 500;
453
			$view->errors = array( array(
454
				'title' => $e->getMessage(),
455
				'detail' => $e->getTraceAsString(),
456
			) );
457
		}
458
459
		/** admin/jsonadm/standard/template-options
460
		 * Relative path to the JSON API template for OPTIONS requests
461
		 *
462
		 * The template file contains the code and processing instructions
463
		 * to generate the result shown in the JSON API body. The
464
		 * configuration string is the path to the template file relative
465
		 * to the templates directory (usually in admin/jsonadm/templates).
466
		 *
467
		 * You can overwrite the template file configuration in extensions and
468
		 * provide alternative templates. These alternative templates should be
469
		 * named like the default one but with the string "standard" replaced by
470
		 * an unique name. You may use the name of your project for this. If
471
		 * you've implemented an alternative client class as well, "standard"
472
		 * should be replaced by the name of the new class.
473
		 *
474
		 * @param string Relative path to the template creating the body for the OPTIONS method of the JSON API
475
		 * @since 2015.12
476
		 * @category Developer
477
		 * @see admin/jsonadm/standard/template-aggregate
478
		 * @see admin/jsonadm/standard/template-delete
479
		 * @see admin/jsonadm/standard/template-patch
480
		 * @see admin/jsonadm/standard/template-post
481
		 * @see admin/jsonadm/standard/template-get
482
		 * @see admin/jsonadm/standard/template-put
483
		 */
484
		$tplconf = 'admin/jsonadm/standard/template-options';
485
		$default = 'options-default.php';
486
487
		$body = $view->render( $view->config( $tplconf, $default ) );
488
489
		return $response->withHeader( 'Allow', 'DELETE,GET,PATCH,POST,OPTIONS' )
490
			->withHeader( 'Content-Type', 'application/vnd.api+json; supported-ext="bulk"' )
491
			->withBody( $view->response()->createStreamFromString( $body ) )
492
			->withStatus( $status );
493
	}
494
495
496
	/**
497
	 * Deletes one or more items
498
	 *
499
	 * @param \Aimeos\MW\View\Iface $view View instance with "param" view helper
500
	 * @param \Psr\Http\Message\ServerRequestInterface $request Request object
501
	 * @param \Psr\Http\Message\ResponseInterface $response Response object
502
	 * @return \Psr\Http\Message\ResponseInterface Modified response object
503
	 * @throws \Aimeos\Admin\JsonAdm\Exception If the request body is invalid
504
	 */
505
	protected function deleteItems( \Aimeos\MW\View\Iface $view, ServerRequestInterface $request, ResponseInterface $response )
506
	{
507
		$manager = \Aimeos\MShop\Factory::createManager( $this->getContext(), $this->getPath() );
508
509
		if( ( $id = $view->param( 'id' ) ) == null )
510
		{
511
			$body = (string) $request->getBody();
512
513
			if( ( $payload = json_decode( $body ) ) === null || !isset( $payload->data ) || !is_array( $payload->data ) ) {
514
				throw new \Aimeos\Admin\JsonAdm\Exception( sprintf( 'Invalid JSON in body' ), 400 );
515
			}
516
517
			$ids = $this->getIds( $payload );
518
			$manager->deleteItems( $ids );
519
			$view->total = count( $ids );
520
		}
521
		else
522
		{
523
			$manager->deleteItem( $id );
524
			$view->total = 1;
525
		}
526
527
		return $response;
528
	}
529
530
531
	/**
532
	 * Retrieves the item or items and adds the data to the view
533
	 *
534
	 * @param \Aimeos\MW\View\Iface $view View instance
535
	 * @param \Psr\Http\Message\ServerRequestInterface $request Request object
536
	 * @param \Psr\Http\Message\ResponseInterface $response Response object
537
	 * @return \Psr\Http\Message\ResponseInterface Modified response object
538
	 */
539
	protected function getItems( \Aimeos\MW\View\Iface $view, ServerRequestInterface $request, ResponseInterface $response )
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
540
	{
541
		$manager = \Aimeos\MShop\Factory::createManager( $this->getContext(), $this->getPath() );
542
543
		if( ( $key = $view->param( 'aggregate' ) ) !== null )
544
		{
545
			$search = $this->initCriteria( $manager->createSearch(), $view->param() );
546
			$view->data = $manager->aggregate( $search, $key );
547
			return $response;
548
		}
549
550
		$total = 1;
551
		$include = ( ( $include = $view->param( 'include' ) ) !== null ? explode( ',', $include ) : array() );
552
553
		if( ( $id = $view->param( 'id' ) ) == null )
554
		{
555
			$search = $this->initCriteria( $manager->createSearch(), $view->param() );
556
			$view->data = $manager->searchItems( $search, array(), $total );
557
			$view->childItems = $this->getChildItems( $view->data, $include );
558
			$view->listItems = $this->getListItems( $view->data, $include );
559
		}
560
		else
561
		{
562
			$view->data = $manager->getItem( $id, array() );
563
			$view->childItems = $this->getChildItems( array( $id => $view->data ), $include );
564
			$view->listItems = $this->getListItems( array( $id => $view->data ), $include );
565
		}
566
567
		$view->refItems = $this->getRefItems( $view->listItems );
568
		$view->total = $total;
569
570
		return $response;
571
	}
572
573
574
	/**
575
	 * Saves new attributes for one or more items
576
	 *
577
	 * @param \Aimeos\MW\View\Iface $view View that will contain the "data" and "total" properties afterwards
578
	 * @param \Psr\Http\Message\ServerRequestInterface $request Request object
579
	 * @param \Psr\Http\Message\ResponseInterface $response Response object
580
	 * @return \Psr\Http\Message\ResponseInterface Modified response object
581
	 * @throws \Aimeos\Admin\JsonAdm\Exception If "id" parameter isn't available or the body is invalid
582
	 */
583
	protected function patchItems( \Aimeos\MW\View\Iface $view, ServerRequestInterface $request, ResponseInterface $response )
584
	{
585
		$body = (string) $request->getBody();
586
587
		if( ( $payload = json_decode( $body ) ) === null || !isset( $payload->data ) ) {
588
			throw new \Aimeos\Admin\JsonAdm\Exception( sprintf( 'Invalid JSON in body' ), 400 );
589
		}
590
591
		$manager = \Aimeos\MShop\Factory::createManager( $this->getContext(), $this->getPath() );
592
593
		if( is_array( $payload->data ) )
594
		{
595
			$data = $this->saveData( $manager, $payload );
596
597
			$view->data = $data;
598
			$view->total = count( $data );
599
			$response = $response->withHeader( 'Content-Type', 'application/vnd.api+json; ext="bulk"; supported-ext="bulk"' );
600
		}
601
		elseif( ( $id = $view->param( 'id' ) ) != null )
602
		{
603
			$payload->data->id = $id;
604
			$data = $this->saveEntry( $manager, $payload->data );
605
606
			$view->data = $data;
607
			$view->total = 1;
608
		}
609
		else
610
		{
611
			throw new \Aimeos\Admin\JsonAdm\Exception( sprintf( 'No ID given' ), 400 );
612
		}
613
614
		return $response;
615
	}
616
617
618
	/**
619
	 * Creates one or more new items
620
	 *
621
	 * @param \Aimeos\MW\View\Iface $view View that will contain the "data" and "total" properties afterwards
622
	 * @param \Psr\Http\Message\ServerRequestInterface $request Request object
623
	 * @param \Psr\Http\Message\ResponseInterface $response Response object
624
	 * @return \Psr\Http\Message\ResponseInterface Modified response object
625
	 */
626
	protected function postItems( \Aimeos\MW\View\Iface $view, ServerRequestInterface $request, ResponseInterface $response )
627
	{
628
		$body = (string) $request->getBody();
629
630
		if( ( $payload = json_decode( $body ) ) === null || !isset( $payload->data ) ) {
631
			throw new \Aimeos\Admin\JsonAdm\Exception( sprintf( 'Invalid JSON in body' ), 400 );
632
		}
633
634
		if( isset( $payload->data->id ) || $view->param( 'id' ) != null ) {
635
			throw new \Aimeos\Admin\JsonAdm\Exception( sprintf( 'Client generated IDs are not supported' ), 403 );
636
		}
637
638
639
		$manager = \Aimeos\MShop\Factory::createManager( $this->getContext(), $this->getPath() );
640
641
		if( is_array( $payload->data ) )
642
		{
643
			$data = $this->saveData( $manager, $payload );
644
645
			$view->data = $data;
646
			$view->total = count( $data );
647
			$response = $response->withHeader( 'Content-Type', 'application/vnd.api+json; ext="bulk"; supported-ext="bulk"' );
648
		}
649
		else
650
		{
651
			$payload->data->id = null;
652
			$data = $this->saveEntry( $manager, $payload->data );
653
654
			$view->data = $data;
655
			$view->total = 1;
656
		}
657
658
		return $response;
659
	}
660
}
661