| 1 | <?php |
||
| 2 | |||
| 3 | /** |
||
| 4 | * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0 |
||
| 5 | * @copyright Aimeos (aimeos.org), 2017-2026 |
||
| 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 | /** client/jsonapi/basket/name |
||
| 26 | * Class name of the used basket client implementation |
||
| 27 | * |
||
| 28 | * Each default JSON API client can be replace by an alternative imlementation. |
||
| 29 | * To use this implementation, you have to set the last part of the class |
||
| 30 | * name as configuration value so the client factory knows which class it |
||
| 31 | * has to instantiate. |
||
| 32 | * |
||
| 33 | * For example, if the name of the default class is |
||
| 34 | * |
||
| 35 | * \Aimeos\Client\JsonApi\Basket\Standard |
||
| 36 | * |
||
| 37 | * and you want to replace it with your own version named |
||
| 38 | * |
||
| 39 | * \Aimeos\Client\JsonApi\Basket\Mybasket |
||
| 40 | * |
||
| 41 | * then you have to set the this configuration option: |
||
| 42 | * |
||
| 43 | * client/jsonapi/basket/name = Mybasket |
||
| 44 | * |
||
| 45 | * The value is the last part of your own class name and it's case sensitive, |
||
| 46 | * so take care that the configuration value is exactly named like the last |
||
| 47 | * part of the class name. |
||
| 48 | * |
||
| 49 | * The allowed characters of the class name are A-Z, a-z and 0-9. No other |
||
| 50 | * characters are possible! You should always start the last part of the class |
||
| 51 | * name with an upper case character and continue only with lower case characters |
||
| 52 | * or numbers. Avoid chamel case names like "MyBasket"! |
||
| 53 | * |
||
| 54 | * @param string Last part of the class name |
||
| 55 | * @since 2017.03 |
||
| 56 | * @category Developer |
||
| 57 | */ |
||
| 58 | |||
| 59 | /** client/jsonapi/basket/decorators/excludes |
||
| 60 | * Excludes decorators added by the "common" option from the JSON API clients |
||
| 61 | * |
||
| 62 | * Decorators extend the functionality of a class by adding new aspects |
||
| 63 | * (e.g. log what is currently done), executing the methods of the underlying |
||
| 64 | * class only in certain conditions (e.g. only for logged in users) or |
||
| 65 | * modify what is returned to the caller. |
||
| 66 | * |
||
| 67 | * This option allows you to remove a decorator added via |
||
| 68 | * "client/jsonapi/common/decorators/default" before they are wrapped |
||
| 69 | * around the JsonApi client. |
||
| 70 | * |
||
| 71 | * client/jsonapi/decorators/excludes = array( 'decorator1' ) |
||
| 72 | * |
||
| 73 | * This would remove the decorator named "decorator1" from the list of |
||
| 74 | * common decorators ("\Aimeos\Client\JsonApi\Common\Decorator\*") added via |
||
| 75 | * "client/jsonapi/common/decorators/default" for the JSON API client. |
||
| 76 | * |
||
| 77 | * @param array List of decorator names |
||
| 78 | * @since 2017.07 |
||
| 79 | * @category Developer |
||
| 80 | * @see client/jsonapi/common/decorators/default |
||
| 81 | * @see client/jsonapi/basket/decorators/global |
||
| 82 | * @see client/jsonapi/basket/decorators/local |
||
| 83 | */ |
||
| 84 | |||
| 85 | /** client/jsonapi/basket/decorators/global |
||
| 86 | * Adds a list of globally available decorators only to the JsonApi client |
||
| 87 | * |
||
| 88 | * Decorators extend the functionality of a class by adding new aspects |
||
| 89 | * (e.g. log what is currently done), executing the methods of the underlying |
||
| 90 | * class only in certain conditions (e.g. only for logged in users) or |
||
| 91 | * modify what is returned to the caller. |
||
| 92 | * |
||
| 93 | * This option allows you to wrap global decorators |
||
| 94 | * ("\Aimeos\Client\JsonApi\Common\Decorator\*") around the JsonApi |
||
| 95 | * client. |
||
| 96 | * |
||
| 97 | * client/jsonapi/basket/decorators/global = array( 'decorator1' ) |
||
| 98 | * |
||
| 99 | * This would add the decorator named "decorator1" defined by |
||
| 100 | * "\Aimeos\Client\JsonApi\Common\Decorator\Decorator1" only to the |
||
| 101 | * "basket" JsonApi client. |
||
| 102 | * |
||
| 103 | * @param array List of decorator names |
||
| 104 | * @since 2017.07 |
||
| 105 | * @category Developer |
||
| 106 | * @see client/jsonapi/common/decorators/default |
||
| 107 | * @see client/jsonapi/basket/decorators/excludes |
||
| 108 | * @see client/jsonapi/basket/decorators/local |
||
| 109 | */ |
||
| 110 | |||
| 111 | /** client/jsonapi/basket/decorators/local |
||
| 112 | * Adds a list of local decorators only to the JsonApi client |
||
| 113 | * |
||
| 114 | * Decorators extend the functionality of a class by adding new aspects |
||
| 115 | * (e.g. log what is currently done), executing the methods of the underlying |
||
| 116 | * class only in certain conditions (e.g. only for logged in users) or |
||
| 117 | * modify what is returned to the caller. |
||
| 118 | * |
||
| 119 | * This option allows you to wrap local decorators |
||
| 120 | * ("\Aimeos\Client\JsonApi\Basket\Decorator\*") around the JsonApi |
||
| 121 | * client. |
||
| 122 | * |
||
| 123 | * client/jsonapi/basket/decorators/local = array( 'decorator2' ) |
||
| 124 | * |
||
| 125 | * This would add the decorator named "decorator2" defined by |
||
| 126 | * "\Aimeos\Client\JsonApi\Basket\Decorator\Decorator2" only to the |
||
| 127 | * "basket" JsonApi client. |
||
| 128 | * |
||
| 129 | * @param array List of decorator names |
||
| 130 | * @since 2017.07 |
||
| 131 | * @category Developer |
||
| 132 | * @see client/jsonapi/common/decorators/default |
||
| 133 | * @see client/jsonapi/basket/decorators/excludes |
||
| 134 | * @see client/jsonapi/basket/decorators/global |
||
| 135 | */ |
||
| 136 | |||
| 137 | |||
| 138 | private \Aimeos\Controller\Frontend\Basket\Iface $controller; |
||
| 139 | |||
| 140 | |||
| 141 | /** |
||
| 142 | * Initializes the client |
||
| 143 | * |
||
| 144 | * @param \Aimeos\MShop\ContextIface $context MShop context object |
||
| 145 | */ |
||
| 146 | public function __construct( \Aimeos\MShop\ContextIface $context ) |
||
| 147 | { |
||
| 148 | parent::__construct( $context ); |
||
| 149 | |||
| 150 | $this->controller = \Aimeos\Controller\Frontend::create( $this->context(), 'basket' ); |
||
| 151 | } |
||
| 152 | |||
| 153 | |||
| 154 | /** |
||
| 155 | * Deletes the resource or the resource list |
||
| 156 | * |
||
| 157 | * @param \Psr\Http\Message\ServerRequestInterface $request Request object |
||
| 158 | * @param \Psr\Http\Message\ResponseInterface $response Response object |
||
| 159 | * @return \Psr\Http\Message\ResponseInterface Modified response object |
||
| 160 | */ |
||
| 161 | public function delete( ServerRequestInterface $request, ResponseInterface $response ) : \Psr\Http\Message\ResponseInterface |
||
| 162 | { |
||
| 163 | $view = $this->view(); |
||
| 164 | |||
| 165 | try |
||
| 166 | { |
||
| 167 | $this->clearCache(); |
||
| 168 | |||
| 169 | $status = 200; |
||
| 170 | $type = $view->param( 'id', 'default' ); |
||
| 171 | $view->item = $this->controller->setType( $type )->clear()->get(); |
||
| 172 | } |
||
| 173 | catch( \Aimeos\MShop\Plugin\Provider\Exception $e ) |
||
| 174 | { |
||
| 175 | $status = 409; |
||
| 176 | $view->errors = $this->getErrorDetails( $e, 'mshop' ); |
||
| 177 | } |
||
| 178 | catch( \Aimeos\MShop\Exception $e ) |
||
| 179 | { |
||
| 180 | $status = 404; |
||
| 181 | $view->errors = $this->getErrorDetails( $e, 'mshop' ); |
||
| 182 | } |
||
| 183 | catch( \Exception $e ) |
||
| 184 | { |
||
| 185 | $status = $e->getCode() >= 100 && $e->getCode() < 600 ? $e->getCode() : 500; |
||
| 186 | $view->errors = $this->getErrorDetails( $e ); |
||
| 187 | } |
||
| 188 | |||
| 189 | return $this->render( $response, $view, $status ); |
||
| 190 | } |
||
| 191 | |||
| 192 | |||
| 193 | /** |
||
| 194 | * Returns the resource or the resource list |
||
| 195 | * |
||
| 196 | * @param \Psr\Http\Message\ServerRequestInterface $request Request object |
||
| 197 | * @param \Psr\Http\Message\ResponseInterface $response Response object |
||
| 198 | * @return \Psr\Http\Message\ResponseInterface Modified response object |
||
| 199 | */ |
||
| 200 | public function get( ServerRequestInterface $request, ResponseInterface $response ) : \Psr\Http\Message\ResponseInterface |
||
| 201 | { |
||
| 202 | $allow = false; |
||
| 203 | $view = $this->view(); |
||
| 204 | $id = $view->param( 'id', 'default' ); |
||
| 205 | |||
| 206 | $include = $view->param( 'include', 'basket/address,basket/coupon,basket/product,basket/service' ); |
||
| 207 | $include = explode( ',', str_replace( 'basket', 'order', str_replace( '.', '/', $include ) ) ); |
||
| 208 | |||
| 209 | try |
||
| 210 | { |
||
| 211 | try |
||
| 212 | { |
||
| 213 | $view->item = $this->controller->load( $id, $include ); |
||
| 214 | } |
||
| 215 | catch( \Aimeos\MShop\Exception $e ) |
||
| 216 | { |
||
| 217 | $view->item = $this->controller->setType( $id )->get(); |
||
| 218 | $allow = true; |
||
| 219 | } |
||
| 220 | |||
| 221 | $status = 200; |
||
| 222 | } |
||
| 223 | catch( \Aimeos\MShop\Exception $e ) |
||
| 224 | { |
||
| 225 | $status = 404; |
||
| 226 | $view->errors = $this->getErrorDetails( $e, 'mshop' ); |
||
| 227 | } |
||
| 228 | catch( \Exception $e ) |
||
| 229 | { |
||
| 230 | $status = $e->getCode() >= 100 && $e->getCode() < 600 ? $e->getCode() : 500; |
||
| 231 | $view->errors = $this->getErrorDetails( $e ); |
||
| 232 | } |
||
| 233 | |||
| 234 | return $this->render( $response, $view, $status, $allow ); |
||
| 235 | } |
||
| 236 | |||
| 237 | |||
| 238 | /** |
||
| 239 | * Updates the resource or the resource list partitially |
||
| 240 | * |
||
| 241 | * @param \Psr\Http\Message\ServerRequestInterface $request Request object |
||
| 242 | * @param \Psr\Http\Message\ResponseInterface $response Response object |
||
| 243 | * @return \Psr\Http\Message\ResponseInterface Modified response object |
||
| 244 | */ |
||
| 245 | public function patch( ServerRequestInterface $request, ResponseInterface $response ) : \Psr\Http\Message\ResponseInterface |
||
| 246 | { |
||
| 247 | $view = $this->view(); |
||
| 248 | |||
| 249 | try |
||
| 250 | { |
||
| 251 | $this->clearCache(); |
||
| 252 | |||
| 253 | $body = (string) $request->getBody(); |
||
| 254 | |||
| 255 | if( ( $payload = json_decode( $body ) ) === null || !isset( $payload->data->attributes ) ) { |
||
| 256 | throw new \Aimeos\Client\JsonApi\Exception( 'Invalid JSON in body', 400 ); |
||
| 257 | } |
||
| 258 | |||
| 259 | $basket = $this->controller->setType( $view->param( 'id', 'default' ) ) |
||
| 260 | ->add( (array) $payload->data->attributes )->save()->get(); |
||
| 261 | |||
| 262 | $view->item = $basket; |
||
| 263 | $status = 200; |
||
| 264 | } |
||
| 265 | catch( \Aimeos\MShop\Plugin\Provider\Exception $e ) |
||
| 266 | { |
||
| 267 | $status = 409; |
||
| 268 | $view->errors = $this->getErrorDetails( $e, 'mshop' ); |
||
| 269 | } |
||
| 270 | catch( \Aimeos\MShop\Exception $e ) |
||
| 271 | { |
||
| 272 | $status = 404; |
||
| 273 | $view->errors = $this->getErrorDetails( $e, 'mshop' ); |
||
| 274 | } |
||
| 275 | catch( \Exception $e ) |
||
| 276 | { |
||
| 277 | $status = $e->getCode() >= 100 && $e->getCode() < 600 ? $e->getCode() : 500; |
||
| 278 | $view->errors = $this->getErrorDetails( $e ); |
||
| 279 | } |
||
| 280 | |||
| 281 | return $this->render( $response, $view, $status ); |
||
| 282 | } |
||
| 283 | |||
| 284 | |||
| 285 | /** |
||
| 286 | * Creates or updates the resource or the resource list |
||
| 287 | * |
||
| 288 | * @param \Psr\Http\Message\ServerRequestInterface $request Request object |
||
| 289 | * @param \Psr\Http\Message\ResponseInterface $response Response object |
||
| 290 | * @return \Psr\Http\Message\ResponseInterface Modified response object |
||
| 291 | */ |
||
| 292 | public function post( ServerRequestInterface $request, ResponseInterface $response ) : \Psr\Http\Message\ResponseInterface |
||
| 293 | { |
||
| 294 | $view = $this->view(); |
||
| 295 | |||
| 296 | try |
||
| 297 | { |
||
| 298 | $userId = (string) $this->context()->user()?->getId(); |
||
| 299 | |||
| 300 | $this->controller->setType( $view->param( 'id', 'default' ) ); |
||
| 301 | $this->controller->get()->setChannel( 'jsonapi' )->setCustomerId( $userId )->check(); |
||
| 302 | $this->clearCache(); |
||
| 303 | |||
| 304 | $item = $this->controller->store(); |
||
| 305 | $this->context()->session()->set( 'aimeos/order.id', $item->getId() ); |
||
| 306 | |||
| 307 | $view->item = $item; |
||
| 308 | $status = 200; |
||
| 309 | } |
||
| 310 | catch( \Aimeos\MShop\Plugin\Provider\Exception $e ) |
||
| 311 | { |
||
| 312 | $status = 409; |
||
| 313 | $view->errors = $this->getErrorDetails( $e, 'mshop' ); |
||
| 314 | } |
||
| 315 | catch( \Aimeos\MShop\Exception $e ) |
||
| 316 | { |
||
| 317 | $status = 404; |
||
| 318 | $view->errors = $this->getErrorDetails( $e, 'mshop' ); |
||
| 319 | } |
||
| 320 | catch( \Exception $e ) |
||
| 321 | { |
||
| 322 | $status = $e->getCode() >= 100 && $e->getCode() < 600 ? $e->getCode() : 500; |
||
| 323 | $view->errors = $this->getErrorDetails( $e ); |
||
| 324 | } |
||
| 325 | |||
| 326 | return $this->render( $response, $view, $status ); |
||
| 327 | } |
||
| 328 | |||
| 329 | |||
| 330 | /** |
||
| 331 | * Returns the available REST verbs and the available parameters |
||
| 332 | * |
||
| 333 | * @param \Psr\Http\Message\ServerRequestInterface $request Request object |
||
| 334 | * @param \Psr\Http\Message\ResponseInterface $response Response object |
||
| 335 | * @return \Psr\Http\Message\ResponseInterface Modified response object |
||
| 336 | */ |
||
| 337 | public function options( ServerRequestInterface $request, ResponseInterface $response ) : \Psr\Http\Message\ResponseInterface |
||
| 338 | { |
||
| 339 | $view = $this->view(); |
||
| 340 | |||
| 341 | $view->attributes = [ |
||
| 342 | 'order.comment' => [ |
||
| 343 | 'label' => 'Customer comment for the order', |
||
| 344 | 'type' => 'string', 'default' => '', 'required' => false, |
||
| 345 | ], |
||
| 346 | 'order.customerref' => [ |
||
| 347 | 'label' => 'Own reference of the customer for the order', |
||
| 348 | 'type' => 'string', 'default' => '', 'required' => false, |
||
| 349 | ], |
||
| 350 | ]; |
||
| 351 | |||
| 352 | $tplconf = 'client/jsonapi/template-options'; |
||
| 353 | $default = 'options-standard'; |
||
| 354 | |||
| 355 | $body = $view->render( $view->config( $tplconf, $default ) ); |
||
| 356 | |||
| 357 | return $response->withHeader( 'Allow', 'DELETE,GET,OPTIONS,PATCH,POST' ) |
||
| 358 | ->withHeader( 'Cache-Control', 'max-age=300' ) |
||
| 359 | ->withHeader( 'Content-Type', 'application/vnd.api+json' ) |
||
| 360 | ->withBody( $view->response()->createStreamFromString( $body ) ) |
||
| 361 | ->withStatus( 200 ); |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 362 | } |
||
| 363 | |||
| 364 | |||
| 365 | /** |
||
| 366 | * Returns the response object with the rendered header and body |
||
| 367 | * |
||
| 368 | * @param \Psr\Http\Message\ResponseInterface $response Response object |
||
| 369 | * @param \Aimeos\Base\View\Iface $view View instance |
||
| 370 | * @param int $status HTTP status code |
||
| 371 | * @param bool $allow True to allow all HTTP methods, false for GET only |
||
| 372 | * @return \Psr\Http\Message\ResponseInterface Modified response object |
||
| 373 | */ |
||
| 374 | protected function render( ResponseInterface $response, \Aimeos\Base\View\Iface $view, int $status, bool $allow = true ) : \Psr\Http\Message\ResponseInterface |
||
| 375 | { |
||
| 376 | /** client/jsonapi/basket/template |
||
| 377 | * Relative path to the basket JSON API template |
||
| 378 | * |
||
| 379 | * The template file contains the code and processing instructions |
||
| 380 | * to generate the result shown in the JSON API body. The |
||
| 381 | * configuration string is the path to the template file relative |
||
| 382 | * to the templates directory (usually in templates/client/jsonapi). |
||
| 383 | * |
||
| 384 | * You can overwrite the template file configuration in extensions and |
||
| 385 | * provide alternative templates. These alternative templates should be |
||
| 386 | * named like the default one but with the string "standard" replaced by |
||
| 387 | * an unique name. You may use the name of your project for this. If |
||
| 388 | * you've implemented an alternative client class as well, "standard" |
||
| 389 | * should be replaced by the name of the new class. |
||
| 390 | * |
||
| 391 | * @param string Relative path to the template creating the body for the JSON API |
||
| 392 | * @since 2017.04 |
||
| 393 | * @category Developer |
||
| 394 | */ |
||
| 395 | $tplconf = 'client/jsonapi/basket/template'; |
||
| 396 | $default = 'basket/standard'; |
||
| 397 | |||
| 398 | $body = $view->render( $view->config( $tplconf, $default ) ); |
||
| 399 | |||
| 400 | if( $allow === true ) { |
||
| 401 | $methods = 'DELETE,GET,OPTIONS,PATCH,POST'; |
||
| 402 | } else { |
||
| 403 | $methods = 'GET'; |
||
| 404 | } |
||
| 405 | |||
| 406 | return $response->withHeader( 'Allow', $methods ) |
||
| 407 | ->withHeader( 'Cache-Control', 'no-cache, private' ) |
||
| 408 | ->withHeader( 'Content-Type', 'application/vnd.api+json' ) |
||
| 409 | ->withBody( $view->response()->createStreamFromString( $body ) ) |
||
| 410 | ->withStatus( $status ); |
||
| 411 | } |
||
| 412 | } |
||
| 413 |