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

Standard::patch()   C

Complexity

Conditions 13
Paths 110

Size

Total Lines 50
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 50
rs 5.1846
cc 13
eloc 27
nc 110
nop 2

How to fix   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
6
 * @package Client
7
 * @subpackage JsonApi
8
 */
9
10
11
namespace Aimeos\Client\JsonApi\Basket\Product;
12
13
use Psr\Http\Message\ResponseInterface;
14
use Psr\Http\Message\ServerRequestInterface;
15
16
17
/**
18
 * JSON API basket/product 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 "basket/product"
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\Basket\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
			$this->controller->setType( $view->param( 'id', 'default' ) );
62
63
			if( $relId === '' || $relId === null )
64
			{
65
				if( ( $payload = json_decode( $body ) ) === null || !isset( $payload->data ) ) {
66
					throw new \Aimeos\Client\JsonApi\Exception( sprintf( 'Invalid JSON in body' ), 400 );
67
				}
68
69
				if( !is_array( $payload->data ) ) {
70
					$payload->data = [$payload->data];
71
				}
72
73
				foreach( $payload->data as $entry )
74
				{
75
					if( !isset( $entry->id ) ) {
76
						throw new \Aimeos\Client\JsonApi\Exception( sprintf( 'Position (ID) is missing' ) );
77
					}
78
79
					$this->controller->deleteProduct( $entry->id );
80
				}
81
			}
82
			else
83
			{
84
				$this->controller->deleteProduct( $relId );
85
			}
86
87
88
			$view->item = $this->controller->get();
89
			$status = 200;
90
		}
91
		catch( \Aimeos\MShop\Exception $e )
92
		{
93
			$status = 404;
94
			$view->errors = $this->getErrorDetails( $e, 'mshop' );
95
		}
96
		catch( \Exception $e )
97
		{
98
			$status = 500;
99
			$view->errors = $this->getErrorDetails( $e );
100
		}
101
102
		return $this->render( $response, $view, $status );
103
	}
104
105
106
	/**
107
	 * Updates the resource or the resource list partitially
108
	 *
109
	 * @param \Psr\Http\Message\ServerRequestInterface $request Request object
110
	 * @param \Psr\Http\Message\ResponseInterface $response Response object
111
	 * @return \Psr\Http\Message\ResponseInterface Modified response object
112
	 */
113
	public function patch( ServerRequestInterface $request, ResponseInterface $response )
114
	{
115
		$view = $this->getView();
116
117
		try
118
		{
119
			$body = (string) $request->getBody();
120
			$relId = $view->param( 'relatedid' );
121
			$this->controller->setType( $view->param( 'id', 'default' ) );
122
123
			if( ( $payload = json_decode( $body ) ) === null || !isset( $payload->data ) || !isset( $payload->data->attributes ) ) {
124
				throw new \Aimeos\Client\JsonApi\Exception( sprintf( 'Invalid JSON in body' ), 400 );
125
			}
126
127
			if( !is_array( $payload->data ) ) {
128
				$payload->data = [$payload->data];
129
			}
130
131
			foreach( $payload->data as $entry )
132
			{
133
				if( $relId !== '' && $relId !== null ) {
134
					$entry->id = $relId;
135
				}
136
137
				if( !isset( $entry->id ) ) {
138
					throw new \Aimeos\Client\JsonApi\Exception( sprintf( 'Position (ID) is missing' ) );
139
				}
140
141
				$qty = ( isset( $entry->attributes->quantity ) ? $entry->attributes->quantity : 1 );
142
				$cfgAttrCodes = ( isset( $entry->attributes->codes ) ? (array) $entry->attributes->codes : [] );
143
144
				$this->controller->editProduct( $entry->id, $qty, $cfgAttrCodes );
145
			}
146
147
			$view->item = $this->controller->get();
148
			$status = 200;
149
		}
150
		catch( \Aimeos\MShop\Exception $e )
151
		{
152
			$status = 404;
153
			$view->errors = $this->getErrorDetails( $e, 'mshop' );
154
		}
155
		catch( \Exception $e )
156
		{
157
			$status = 500;
158
			$view->errors = $this->getErrorDetails( $e );
159
		}
160
161
		return $this->render( $response, $view, $status );
162
	}
163
164
165
	/**
166
	 * Creates or updates the resource or the resource list
167
	 *
168
	 * @param \Psr\Http\Message\ServerRequestInterface $request Request object
169
	 * @param \Psr\Http\Message\ResponseInterface $response Response object
170
	 * @return \Psr\Http\Message\ResponseInterface Modified response object
171
	 */
172
	public function post( ServerRequestInterface $request, ResponseInterface $response )
173
	{
174
		$view = $this->getView();
175
176
		try
177
		{
178
			$body = (string) $request->getBody();
179
			$this->controller->setType( $view->param( 'id', 'default' ) );
180
181
			if( ( $payload = json_decode( $body ) ) === null || !isset( $payload->data ) ) {
182
				throw new \Aimeos\Client\JsonApi\Exception( sprintf( 'Invalid JSON in body' ), 400 );
183
			}
184
185
			if( !is_array( $payload->data ) ) {
186
				$payload->data = [$payload->data];
187
			}
188
189
			foreach( $payload->data as $entry )
190
			{
191
				if( !isset( $entry->attributes ) || !isset( $entry->attributes->{'product.id'} ) ) {
192
					throw new \Aimeos\Client\JsonApi\Exception( sprintf( 'Product ID is missing' ) );
193
				}
194
195
				$qty = ( isset( $entry->attributes->quantity ) ? $entry->attributes->quantity : 1 );
196
				$stocktype = ( isset( $entry->attributes->stocktype ) ? $entry->attributes->stocktype : 'default' );
197
				$variantAttrIds = ( isset( $entry->attributes->variant ) ? (array) $entry->attributes->variant : [] );
198
				$configAttrIds = ( isset( $entry->attributes->config ) ? (array) $entry->attributes->config : [] );
199
				$hiddenAttrIds = ( isset( $entry->attributes->hidden ) ? (array) $entry->attributes->hidden : [] );
200
				$customAttrIds = ( isset( $entry->attributes->custom ) ? (array) $entry->attributes->custom : [] );
201
202
				$this->controller->addProduct( $entry->attributes->{'product.id'}, $qty, [],
203
					$variantAttrIds, $configAttrIds, $hiddenAttrIds, $customAttrIds, $stocktype );
204
			}
205
206
207
			$view->item = $this->controller->get();
208
			$status = 201;
209
		}
210
		catch( \Aimeos\MShop\Exception $e )
211
		{
212
			$status = 404;
213
			$view->errors = $this->getErrorDetails( $e, 'mshop' );
214
		}
215
		catch( \Exception $e )
216
		{
217
			$status = 500;
218
			$view->errors = $this->getErrorDetails( $e );
219
		}
220
221
		return $this->render( $response, $view, $status );
222
	}
223
224
225
	/**
226
	 * Returns the available REST verbs and the available parameters
227
	 *
228
	 * @param \Psr\Http\Message\ServerRequestInterface $request Request object
229
	 * @param \Psr\Http\Message\ResponseInterface $response Response object
230
	 * @param string|null $prefix Form parameter prefix when nesting parameters is required
231
	 * @return \Psr\Http\Message\ResponseInterface Modified response object
232
	 */
233
	public function options( ServerRequestInterface $request, ResponseInterface $response, $prefix = null )
234
	{
235
		$view = $this->getView();
236
		$view->attributes = [
237
			'product.id' => [
238
				'label' => 'Product ID from article, bundle or selection product (POST only)',
239
				'type' => 'string', 'default' => '', 'required' => true,
240
			],
241
			'quantity' => [
242
				'label' => 'Number of product items (POST only)',
243
				'type' => 'string', 'default' => '1', 'required' => false,
244
			],
245
			'stocktype' => [
246
				'label' => 'Code of the warehouse/location type (POST only)',
247
				'type' => 'string', 'default' => 'default', 'required' => false,
248
			],
249
			'variant' => [
250
				'label' => 'List of attribute IDs of the selected variant attributes (POST only)',
251
				'type' => 'array', 'default' => '[]', 'required' => false,
252
			],
253
			'config' => [
254
				'label' => 'List of attribute IDs of the selected config attributes (POST only)',
255
				'type' => 'array', 'default' => '[]', 'required' => false,
256
			],
257
			'hidden' => [
258
				'label' => 'List of attribute IDs of the hidden product attributes that will be added but should be invisible (POST only)',
259
				'type' => 'array', 'default' => '[]', 'required' => false,
260
			],
261
			'custom' => [
262
				'label' => 'List of values entered by the user for the custom attributes with the attribute IDs as keys (POST only)',
263
				'type' => 'array[<attrid>]', 'default' => '[]', 'required' => false,
264
			],
265
			'codes' => [
266
				'label' => 'List of product options (added via "config") that should be removed (PATCH only)',
267
				'type' => 'array', '' => '[]', 'required' => false,
268
			],
269
		];
270
271
		$tplconf = 'client/jsonapi/standard/template-options';
272
		$default = 'options-standard.php';
273
274
		$body = $view->render( $view->config( $tplconf, $default ) );
275
276
		return $response->withHeader( 'Allow', 'DELETE,GET,OPTIONS,PATCH,POST' )
277
			->withHeader( 'Content-Type', 'application/vnd.api+json' )
278
			->withBody( $view->response()->createStreamFromString( $body ) )
279
			->withStatus( 200 );
280
	}
281
282
283
	/**
284
	 * Returns the response object with the rendered header and body
285
	 *
286
	 * @param \Psr\Http\Message\ResponseInterface $response Response object
287
	 * @param \Aimeos\MW\View\Iface $view View instance
288
	 * @param integer $status HTTP status code
289
	 * @return \Psr\Http\Message\ResponseInterface Modified response object
290
	 */
291
	protected function render( ResponseInterface $response, \Aimeos\MW\View\Iface $view, $status )
292
	{
293
		$tplconf = 'client/jsonapi/basket/standard/template';
294
		$default = 'basket/standard.php';
295
296
		$body = $view->render( $view->config( $tplconf, $default ) );
297
298
		return $response->withHeader( 'Allow', 'DELETE,GET,OPTIONS,PATCH,POST' )
299
			->withHeader( 'Content-Type', 'application/vnd.api+json' )
300
			->withBody( $view->response()->createStreamFromString( $body ) )
301
			->withStatus( $status );
302
	}
303
}
304