Completed
Push — master ( ceb428...d148de )
by Aimeos
02:24
created

Standard::options()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

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