Passed
Push — master ( eb8d43...236a69 )
by Aimeos
18:17 queued 15:47
created

Standard::getParts()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 11
c 2
b 0
f 0
dl 0
loc 20
rs 9.9
cc 3
nc 3
nop 1
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2017-2022
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, string $path )
35
	{
36
		parent::__construct( $context, $path );
37
38
		$this->controller = \Aimeos\Controller\Frontend\Basket\Factory::create( $this->context() );
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 ) : \Psr\Http\Message\ResponseInterface
50
	{
51
		$view = $this->view();
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 = $e->getCode() >= 100 && $e->getCode() < 600 ? $e->getCode() : 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 ) : \Psr\Http\Message\ResponseInterface
90
	{
91
		$allow = false;
92
		$view = $this->view();
93
		$id = $view->param( 'id', 'default' );
94
95
		$include = $view->param( 'include', 'basket/address,basket/coupon,basket/product,basket/service' );
96
		$include = explode( ',', str_replace( 'basket', 'order/base', $include ) );
97
98
		try
99
		{
100
			try
101
			{
102
				$view->item = $this->controller->load( $id, $include );
103
			}
104
			catch( \Aimeos\MShop\Exception $e )
105
			{
106
				$view->item = $this->controller->setType( $id )->get();
107
				$allow = true;
108
			}
109
110
			$status = 200;
111
		}
112
		catch( \Aimeos\MShop\Exception $e )
113
		{
114
			$status = 404;
115
			$view->errors = $this->getErrorDetails( $e, 'mshop' );
116
		}
117
		catch( \Exception $e )
118
		{
119
			$status = $e->getCode() >= 100 && $e->getCode() < 600 ? $e->getCode() : 500;
120
			$view->errors = $this->getErrorDetails( $e );
121
		}
122
123
		return $this->render( $response, $view, $status, $allow );
124
	}
125
126
127
	/**
128
	 * Updates the resource or the resource list partitially
129
	 *
130
	 * @param \Psr\Http\Message\ServerRequestInterface $request Request object
131
	 * @param \Psr\Http\Message\ResponseInterface $response Response object
132
	 * @return \Psr\Http\Message\ResponseInterface Modified response object
133
	 */
134
	public function patch( ServerRequestInterface $request, ResponseInterface $response ) : \Psr\Http\Message\ResponseInterface
135
	{
136
		$view = $this->view();
137
138
		try
139
		{
140
			$this->clearCache();
141
142
			$body = (string) $request->getBody();
143
144
			if( ( $payload = json_decode( $body ) ) === null || !isset( $payload->data->attributes ) ) {
145
				throw new \Aimeos\Client\JsonApi\Exception( sprintf( 'Invalid JSON in body' ), 400 );
146
			}
147
148
			$basket = $this->controller->setType( $view->param( 'id', 'default' ) )
149
				->add( (array) $payload->data->attributes )->save()->get();
150
151
			$view->item = $basket;
152
			$status = 200;
153
		}
154
		catch( \Aimeos\MShop\Plugin\Provider\Exception $e )
155
		{
156
			$status = 409;
157
			$errors = $this->translatePluginErrorCodes( $e->getErrorCodes() );
158
			$view->errors = $this->getErrorDetails( $e, 'mshop' ) + $errors;
159
		}
160
		catch( \Aimeos\MShop\Exception $e )
161
		{
162
			$status = 404;
163
			$view->errors = $this->getErrorDetails( $e, 'mshop' );
164
		}
165
		catch( \Exception $e )
166
		{
167
			$status = $e->getCode() >= 100 && $e->getCode() < 600 ? $e->getCode() : 500;
168
			$view->errors = $this->getErrorDetails( $e );
169
		}
170
171
		return $this->render( $response, $view, $status );
172
	}
173
174
175
	/**
176
	 * Creates or updates the resource or the resource list
177
	 *
178
	 * @param \Psr\Http\Message\ServerRequestInterface $request Request object
179
	 * @param \Psr\Http\Message\ResponseInterface $response Response object
180
	 * @return \Psr\Http\Message\ResponseInterface Modified response object
181
	 */
182
	public function post( ServerRequestInterface $request, ResponseInterface $response ) : \Psr\Http\Message\ResponseInterface
183
	{
184
		$view = $this->view();
185
186
		try
187
		{
188
			$this->controller->setType( $view->param( 'id', 'default' ) );
189
			$this->controller->get()->check();
190
			$this->clearCache();
191
192
			$item = $this->controller->store();
193
			$this->context()->session()->set( 'aimeos/order.baseid', $item->getId() );
194
195
			$view->item = $item;
196
			$status = 200;
197
		}
198
		catch( \Aimeos\MShop\Plugin\Provider\Exception $e )
199
		{
200
			$status = 409;
201
			$errors = $this->translatePluginErrorCodes( $e->getErrorCodes() );
202
			$view->errors = $this->getErrorDetails( $e, 'mshop' ) + $errors;
203
		}
204
		catch( \Aimeos\MShop\Exception $e )
205
		{
206
			$status = 404;
207
			$view->errors = $this->getErrorDetails( $e, 'mshop' );
208
		}
209
		catch( \Exception $e )
210
		{
211
			$status = $e->getCode() >= 100 && $e->getCode() < 600 ? $e->getCode() : 500;
212
			$view->errors = $this->getErrorDetails( $e );
213
		}
214
215
		return $this->render( $response, $view, $status );
216
	}
217
218
219
	/**
220
	 * Returns the available REST verbs and the available parameters
221
	 *
222
	 * @param \Psr\Http\Message\ServerRequestInterface $request Request object
223
	 * @param \Psr\Http\Message\ResponseInterface $response Response object
224
	 * @return \Psr\Http\Message\ResponseInterface Modified response object
225
	 */
226
	public function options( ServerRequestInterface $request, ResponseInterface $response ) : \Psr\Http\Message\ResponseInterface
227
	{
228
		$view = $this->view();
229
230
		$view->attributes = [
231
			'order.base.comment' => [
232
				'label' => 'Customer comment for the order',
233
				'type' => 'string', 'default' => '', 'required' => false,
234
			],
235
			'order.base.customerref' => [
236
				'label' => 'Own reference of the customer for the order',
237
				'type' => 'string', 'default' => '', 'required' => false,
238
			],
239
		];
240
241
		$tplconf = 'client/jsonapi/template-options';
242
		$default = 'options-standard';
243
244
		$body = $view->render( $view->config( $tplconf, $default ) );
245
246
		return $response->withHeader( 'Allow', 'DELETE,GET,OPTIONS,PATCH,POST' )
247
			->withHeader( 'Cache-Control', 'max-age=300' )
248
			->withHeader( 'Content-Type', 'application/vnd.api+json' )
249
			->withBody( $view->response()->createStreamFromString( $body ) )
250
			->withStatus( 200 );
251
	}
252
253
254
	/**
255
	 * Returns the response object with the rendered header and body
256
	 *
257
	 * @param \Psr\Http\Message\ResponseInterface $response Response object
258
	 * @param \Aimeos\MW\View\Iface $view View instance
259
	 * @param int $status HTTP status code
260
	 * @param bool $allow True to allow all HTTP methods, false for GET only
261
	 * @return \Psr\Http\Message\ResponseInterface Modified response object
262
	 */
263
	protected function render( ResponseInterface $response, \Aimeos\MW\View\Iface $view, int $status, bool $allow = true ) : \Psr\Http\Message\ResponseInterface
264
	{
265
		/** client/jsonapi/basket/template
266
		 * Relative path to the basket JSON API template
267
		 *
268
		 * The template file contains the code and processing instructions
269
		 * to generate the result shown in the JSON API body. The
270
		 * configuration string is the path to the template file relative
271
		 * to the templates directory (usually in client/jsonapi/templates).
272
		 *
273
		 * You can overwrite the template file configuration in extensions and
274
		 * provide alternative templates. These alternative templates should be
275
		 * named like the default one but with the string "standard" replaced by
276
		 * an unique name. You may use the name of your project for this. If
277
		 * you've implemented an alternative client class as well, "standard"
278
		 * should be replaced by the name of the new class.
279
		 *
280
		 * @param string Relative path to the template creating the body for the JSON API
281
		 * @since 2017.04
282
		 * @category Developer
283
		 */
284
		$tplconf = 'client/jsonapi/basket/template';
285
		$default = 'basket/standard';
286
287
		$body = $view->render( $view->config( $tplconf, $default ) );
288
289
		if( $allow === true ) {
290
			$methods = 'DELETE,GET,OPTIONS,PATCH,POST';
291
		} else {
292
			$methods = 'GET';
293
		}
294
295
		return $response->withHeader( 'Allow', $methods )
296
			->withHeader( 'Cache-Control', 'no-cache, private' )
297
			->withHeader( 'Content-Type', 'application/vnd.api+json' )
298
			->withBody( $view->response()->createStreamFromString( $body ) )
299
			->withStatus( $status );
300
	}
301
}
302