Passed
Push — master ( a6b03a...955296 )
by Aimeos
02:30
created

Standard::patch()   B

Complexity

Conditions 7
Paths 20

Size

Total Lines 41
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 22
dl 0
loc 41
rs 8.6346
c 0
b 0
f 0
cc 7
nc 20
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\Basket;
12
13
use Psr\Http\Message\ResponseInterface;
14
use Psr\Http\Message\ServerRequestInterface;
15
16
17
/**
18
 * JSON API basket client
19
 *
20
 * @package Client
21
 * @subpackage JsonApi
22
 */
23
class Standard extends Base implements \Aimeos\Client\JsonApi\Iface
24
{
25
	private $controller;
26
27
28
	/**
29
	 * Initializes the client
30
	 *
31
	 * @param \Aimeos\MShop\Context\Item\Iface $context MShop context object
32
	 * @param string $path Name of the client, e.g "basket"
33
	 */
34
	public function __construct( \Aimeos\MShop\Context\Item\Iface $context, $path )
35
	{
36
		parent::__construct( $context, $path );
37
38
		$this->controller = \Aimeos\Controller\Frontend\Basket\Factory::create( $this->getContext() );
39
	}
40
41
42
	/**
43
	 * Deletes the resource or the resource list
44
	 *
45
	 * @param \Psr\Http\Message\ServerRequestInterface $request Request object
46
	 * @param \Psr\Http\Message\ResponseInterface $response Response object
47
	 * @return \Psr\Http\Message\ResponseInterface Modified response object
48
	 */
49
	public function delete( ServerRequestInterface $request, ResponseInterface $response )
50
	{
51
		$view = $this->getView();
52
53
		try
54
		{
55
			$this->clearCache();
56
57
			$status = 200;
58
			$type = $view->param( 'id', 'default' );
59
			$view->item = $this->controller->setType( $type )->clear()->get();
60
		}
61
		catch( \Aimeos\MShop\Plugin\Provider\Exception $e )
62
		{
63
			$status = 409;
64
			$errors = $this->translatePluginErrorCodes( $e->getErrorCodes() );
65
			$view->errors = $this->getErrorDetails( $e, 'mshop' ) + $errors;
66
		}
67
		catch( \Aimeos\MShop\Exception $e )
68
		{
69
			$status = 404;
70
			$view->errors = $this->getErrorDetails( $e, 'mshop' );
71
		}
72
		catch( \Exception $e )
73
		{
74
			$status = 500;
75
			$view->errors = $this->getErrorDetails( $e );
76
		}
77
78
		return $this->render( $response, $view, $status );
79
	}
80
81
82
	/**
83
	 * Returns the resource or the resource list
84
	 *
85
	 * @param \Psr\Http\Message\ServerRequestInterface $request Request object
86
	 * @param \Psr\Http\Message\ResponseInterface $response Response object
87
	 * @return \Psr\Http\Message\ResponseInterface Modified response object
88
	 */
89
	public function get( ServerRequestInterface $request, ResponseInterface $response )
90
	{
91
		$view = $this->getView();
92
93
		$allow = false;
94
		$id = $view->param( 'id', 'default' );
95
96
		try
97
		{
98
			try
99
			{
100
				$view->item = $this->controller->load( $id, $this->getParts( $view ) );
101
			}
102
			catch( \Aimeos\MShop\Exception $e )
103
			{
104
				$view->item = $this->controller->setType( $id )->get();
105
				$allow = true;
106
			}
107
108
			$status = 200;
109
		}
110
		catch( \Aimeos\MShop\Exception $e )
111
		{
112
			$status = 404;
113
			$view->errors = $this->getErrorDetails( $e, 'mshop' );
114
		}
115
		catch( \Exception $e )
116
		{
117
			$status = 500;
118
			$view->errors = $this->getErrorDetails( $e );
119
		}
120
121
		return $this->render( $response, $view, $status, $allow );
122
	}
123
124
125
	/**
126
	 * Updates the resource or the resource list partitially
127
	 *
128
	 * @param \Psr\Http\Message\ServerRequestInterface $request Request object
129
	 * @param \Psr\Http\Message\ResponseInterface $response Response object
130
	 * @return \Psr\Http\Message\ResponseInterface Modified response object
131
	 */
132
	public function patch( ServerRequestInterface $request, ResponseInterface $response )
133
	{
134
		$view = $this->getView();
135
136
		try
137
		{
138
			$this->clearCache();
139
140
			$body = (string) $request->getBody();
141
142
			if( ( $payload = json_decode( $body ) ) === null || !isset( $payload->data->attributes ) ) {
143
				throw new \Aimeos\Client\JsonApi\Exception( sprintf( 'Invalid JSON in body' ), 400 );
144
			}
145
146
			$basket = $this->controller->setType( $view->param( 'id', 'default' ) )->get();
147
148
			if( isset( $payload->data->attributes->{'order.base.comment'} ) ) {
149
				$basket->setComment( $payload->data->attributes->{'order.base.comment'} );
150
			}
151
152
			$view->item = $basket;
153
			$status = 200;
154
		}
155
		catch( \Aimeos\MShop\Plugin\Provider\Exception $e )
156
		{
157
			$status = 409;
158
			$errors = $this->translatePluginErrorCodes( $e->getErrorCodes() );
159
			$view->errors = $this->getErrorDetails( $e, 'mshop' ) + $errors;
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 = 500;
169
			$view->errors = $this->getErrorDetails( $e );
170
		}
171
172
		return $this->render( $response, $view, $status );
173
	}
174
175
176
	/**
177
	 * Creates or updates 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 post( ServerRequestInterface $request, ResponseInterface $response )
184
	{
185
		$view = $this->getView();
186
187
		try
188
		{
189
			$this->clearCache();
190
191
			$item = $this->controller->setType( $view->param( 'id', 'default' ) )->store();
192
			$this->getContext()->getSession()->set( 'aimeos/order.baseid', $item->getId() );
193
194
			$view->item = $item;
195
			$status = 200;
196
		}
197
		catch( \Aimeos\MShop\Plugin\Provider\Exception $e )
198
		{
199
			$status = 409;
200
			$errors = $this->translatePluginErrorCodes( $e->getErrorCodes() );
201
			$view->errors = $this->getErrorDetails( $e, 'mshop' ) + $errors;
202
		}
203
		catch( \Aimeos\MShop\Exception $e )
204
		{
205
			$status = 404;
206
			$view->errors = $this->getErrorDetails( $e, 'mshop' );
207
		}
208
		catch( \Exception $e )
209
		{
210
			$status = 500;
211
			$view->errors = $this->getErrorDetails( $e );
212
		}
213
214
		return $this->render( $response, $view, $status );
215
	}
216
217
218
	/**
219
	 * Returns the available REST verbs and the available parameters
220
	 *
221
	 * @param \Psr\Http\Message\ServerRequestInterface $request Request object
222
	 * @param \Psr\Http\Message\ResponseInterface $response Response object
223
	 * @return \Psr\Http\Message\ResponseInterface Modified response object
224
	 */
225
	public function options( ServerRequestInterface $request, ResponseInterface $response )
226
	{
227
		$view = $this->getView();
228
229
		$view->attributes = [
230
			'order.base.comment' => [
231
				'label' => 'Customer comment for the order',
232
				'type' => 'string', 'default' => '', 'required' => false,
233
			],
234
		];
235
236
		$tplconf = 'client/jsonapi/standard/template-options';
237
		$default = 'options-standard';
238
239
		$body = $view->render( $view->config( $tplconf, $default ) );
240
241
		return $response->withHeader( 'Allow', 'DELETE,GET,OPTIONS,PATCH,POST' )
242
			->withHeader( 'Cache-Control', 'max-age=300' )
243
			->withHeader( 'Content-Type', 'application/vnd.api+json' )
244
			->withBody( $view->response()->createStreamFromString( $body ) )
245
			->withStatus( 200 );
246
	}
247
248
249
	/**
250
	 * Returns the integer constant for the basket parts that should be included
251
	 *
252
	 * @param \Aimeos\MW\View\Iface $view View instance
253
	 * @return integer Constant from Aimeos\MShop\Order\Item\Base\Base
254
	 */
255
	protected function getParts( \Aimeos\MW\View\Iface $view )
256
	{
257
		$available = array(
258
			'basket/address' => \Aimeos\MShop\Order\Item\Base\Base::PARTS_ADDRESS,
259
			'basket/coupon' => \Aimeos\MShop\Order\Item\Base\Base::PARTS_COUPON,
260
			'basket/product' => \Aimeos\MShop\Order\Item\Base\Base::PARTS_PRODUCT,
261
			'basket/service' => \Aimeos\MShop\Order\Item\Base\Base::PARTS_SERVICE,
262
		);
263
264
		$included = explode( ',', $view->param( 'included', 'basket/address,basket/coupon,basket/product,basket/service' ) );
265
266
		$parts = \Aimeos\MShop\Order\Item\Base\Base::PARTS_NONE;
267
		foreach( $included as $type )
268
		{
269
			if( isset( $available[$type] ) ) {
270
				$parts |= $available[$type];
271
			}
272
		}
273
274
		return $parts;
275
	}
276
277
278
	/**
279
	 * Returns the response object with the rendered header and body
280
	 *
281
	 * @param \Psr\Http\Message\ResponseInterface $response Response object
282
	 * @param \Aimeos\MW\View\Iface $view View instance
283
	 * @param integer $status HTTP status code
284
	 * @param boolean $allow True to allow all HTTP methods, false for GET only
285
	 * @return \Psr\Http\Message\ResponseInterface Modified response object
286
	 */
287
	protected function render( ResponseInterface $response, \Aimeos\MW\View\Iface $view, $status, $allow = true )
288
	{
289
		/** client/jsonapi/basket/standard/template
290
		 * Relative path to the basket JSON API template
291
		 *
292
		 * The template file contains the code and processing instructions
293
		 * to generate the result shown in the JSON API body. The
294
		 * configuration string is the path to the template file relative
295
		 * to the templates directory (usually in client/jsonapi/templates).
296
		 *
297
		 * You can overwrite the template file configuration in extensions and
298
		 * provide alternative templates. These alternative templates should be
299
		 * named like the default one but with the string "standard" replaced by
300
		 * an unique name. You may use the name of your project for this. If
301
		 * you've implemented an alternative client class as well, "standard"
302
		 * should be replaced by the name of the new class.
303
		 *
304
		 * @param string Relative path to the template creating the body for the JSON API
305
		 * @since 2017.04
306
		 * @category Developer
307
		 */
308
		$tplconf = 'client/jsonapi/basket/standard/template';
309
		$default = 'basket/standard';
310
311
		$body = $view->render( $view->config( $tplconf, $default ) );
312
313
		if( $allow === true ) {
314
			$methods = 'DELETE,GET,OPTIONS,PATCH,POST';
315
		} else {
316
			$methods = 'GET';
317
		}
318
319
		return $response->withHeader( 'Allow', $methods )
320
			->withHeader( 'Cache-Control', 'no-cache, private' )
321
			->withHeader( 'Content-Type', 'application/vnd.api+json' )
322
			->withBody( $view->response()->createStreamFromString( $body ) )
323
			->withStatus( $status );
324
	}
325
}
326