1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @license LGPLv3, http://opensource.org/licenses/LGPL-3.0 |
5
|
|
|
* @copyright Aimeos (aimeos.org), 2017-2021 |
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->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 ) : \Psr\Http\Message\ResponseInterface |
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 = $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->getView(); |
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->getView(); |
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->getView(); |
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->getContext()->getSession()->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->getView(); |
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 integer constant for the basket parts that should be included |
256
|
|
|
* |
257
|
|
|
* @param \Aimeos\MW\View\Iface $view View instance |
258
|
|
|
* @return int Constant from Aimeos\MShop\Order\Item\Base\Base |
259
|
|
|
* @deprecated Use strings like "basket/product" directly |
260
|
|
|
*/ |
261
|
|
|
protected function getParts( \Aimeos\MW\View\Iface $view ) : int |
262
|
|
|
{ |
263
|
|
|
$available = array( |
264
|
|
|
'basket/address' => \Aimeos\MShop\Order\Item\Base\Base::PARTS_ADDRESS, |
265
|
|
|
'basket/coupon' => \Aimeos\MShop\Order\Item\Base\Base::PARTS_COUPON, |
266
|
|
|
'basket/product' => \Aimeos\MShop\Order\Item\Base\Base::PARTS_PRODUCT, |
267
|
|
|
'basket/service' => \Aimeos\MShop\Order\Item\Base\Base::PARTS_SERVICE, |
268
|
|
|
); |
269
|
|
|
|
270
|
|
|
$include = explode( ',', $view->param( 'include', 'basket/address,basket/coupon,basket/product,basket/service' ) ); |
271
|
|
|
|
272
|
|
|
$parts = \Aimeos\MShop\Order\Item\Base\Base::PARTS_NONE; |
273
|
|
|
foreach( $include as $type ) |
274
|
|
|
{ |
275
|
|
|
if( isset( $available[$type] ) ) { |
276
|
|
|
$parts |= $available[$type]; |
277
|
|
|
} |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
return $parts; |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
|
284
|
|
|
/** |
285
|
|
|
* Returns the response object with the rendered header and body |
286
|
|
|
* |
287
|
|
|
* @param \Psr\Http\Message\ResponseInterface $response Response object |
288
|
|
|
* @param \Aimeos\MW\View\Iface $view View instance |
289
|
|
|
* @param int $status HTTP status code |
290
|
|
|
* @param bool $allow True to allow all HTTP methods, false for GET only |
291
|
|
|
* @return \Psr\Http\Message\ResponseInterface Modified response object |
292
|
|
|
*/ |
293
|
|
|
protected function render( ResponseInterface $response, \Aimeos\MW\View\Iface $view, int $status, bool $allow = true ) : \Psr\Http\Message\ResponseInterface |
294
|
|
|
{ |
295
|
|
|
/** client/jsonapi/basket/template |
296
|
|
|
* Relative path to the basket JSON API template |
297
|
|
|
* |
298
|
|
|
* The template file contains the code and processing instructions |
299
|
|
|
* to generate the result shown in the JSON API body. The |
300
|
|
|
* configuration string is the path to the template file relative |
301
|
|
|
* to the templates directory (usually in client/jsonapi/templates). |
302
|
|
|
* |
303
|
|
|
* You can overwrite the template file configuration in extensions and |
304
|
|
|
* provide alternative templates. These alternative templates should be |
305
|
|
|
* named like the default one but with the string "standard" replaced by |
306
|
|
|
* an unique name. You may use the name of your project for this. If |
307
|
|
|
* you've implemented an alternative client class as well, "standard" |
308
|
|
|
* should be replaced by the name of the new class. |
309
|
|
|
* |
310
|
|
|
* @param string Relative path to the template creating the body for the JSON API |
311
|
|
|
* @since 2017.04 |
312
|
|
|
* @category Developer |
313
|
|
|
*/ |
314
|
|
|
$tplconf = 'client/jsonapi/basket/template'; |
315
|
|
|
$default = 'basket/standard'; |
316
|
|
|
|
317
|
|
|
$body = $view->render( $view->config( $tplconf, $default ) ); |
318
|
|
|
|
319
|
|
|
if( $allow === true ) { |
320
|
|
|
$methods = 'DELETE,GET,OPTIONS,PATCH,POST'; |
321
|
|
|
} else { |
322
|
|
|
$methods = 'GET'; |
323
|
|
|
} |
324
|
|
|
|
325
|
|
|
return $response->withHeader( 'Allow', $methods ) |
326
|
|
|
->withHeader( 'Cache-Control', 'no-cache, private' ) |
327
|
|
|
->withHeader( 'Content-Type', 'application/vnd.api+json' ) |
328
|
|
|
->withBody( $view->response()->createStreamFromString( $body ) ) |
329
|
|
|
->withStatus( $status ); |
330
|
|
|
} |
331
|
|
|
} |
332
|
|
|
|