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

Standard::patch()   A

Complexity

Conditions 6
Paths 14

Size

Total Lines 35
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

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