1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
<?php
/* --------------------------------------------------------------
OrdersTotalsApiV2Controller.inc.php 2016-09-06
Gambio GmbH
http://www.gambio.de
Copyright (c) 2016 Gambio GmbH
Released under the GNU General Public License (Version 2)
[http://www.gnu.org/licenses/gpl-2.0.html]
--------------------------------------------------------------
*/
MainFactory::load_class('HttpApiV2Controller');
/**
* Class OrdersTotalsApiV2Controller
*
* Notice: This controller is a sub-resource of the OrdersV2Controller.
*
* @category System
* @package ApiV2Controllers
*/
class OrdersTotalsApiV2Controller extends HttpApiV2Controller
{
/**
* Order write service.
*
* @var OrderWriteService
*/
protected $orderWriteService;
/**
* Order read service.
*
* @var OrderReadService
*/
protected $orderReadService;
/**
* Order JSON serializer.
*
* @var OrderJsonSerializer
*/
protected $orderJsonSerializer;
/**
* Initializes API Controller
*
* @throws HttpApiV2Exception On missing order ID.
*/
protected function __initialize()
{
// Check if the order ID was provided
if(!isset($this->uri[1]) || !is_numeric($this->uri[1]))
{
throw new HttpApiV2Exception('Order record ID was not provided in the resource URL.', 400);
}
$this->orderWriteService = StaticGXCoreLoader::getService('OrderWrite');
$this->orderReadService = StaticGXCoreLoader::getService('OrderRead');
$this->orderJsonSerializer = MainFactory::create('OrderJsonSerializer');
}
/**
* @api {post} /orders/:id/totals Create Order Total
* @apiVersion 2.1.0
* @apiName CreateOrderTotal
* @apiGroup Orders
*
* @apiDescription
* Creates a new order total entry to the existing order. The order total JSON format must be the same with the
* "totals" entries in the original order total.
*
* @apiExample {json} Request-Body
* {
* "title": "Zwischensumme:",
* "value": 50,
* "valueText": "50,00 EUR",
* "class": "ot_subtotal",
* "sortOrder": 10
* }
*
* @apiSuccess (Success 201) Response-Body If successful, this method returns a complete Order Total resource in the
* response body.
*
* @apiError 400-BadRequest The request body was empty.
*
* @apiError (Error 5xx) 500-InternalError One of the given properties has an invalid value type.
*
* @apiErrorExample Error-Response (Empty request body)
* HTTP/1.1 400 Bad Request
* {
* "code": 400,
* "status": "error",
* "message": "Order total data were not provided."
* }
*/
public function post()
{
$orderTotalJsonString = $this->api->request->getBody();
if(empty($orderTotalJsonString))
{
throw new HttpApiV2Exception('Order total data were not provided.', 400);
}
$orderId = new IdType($this->uri[1]);
$orderTotal = $this->orderJsonSerializer->deserializeOrderTotal(json_decode($orderTotalJsonString));
$orderTotalId = $this->orderWriteService->addOrderTotal($orderId, $orderTotal);
$order = $this->orderReadService->getOrderById($orderId);
foreach($order->getOrderTotals()->getArray() as $storedOrderTotal)
{
/** @var StoredOrderTotalInterface $storedOrderTotal */
if($storedOrderTotal->getOrderTotalId() === $orderTotalId)
{
$response = $this->orderJsonSerializer->serializeOrderTotal($storedOrderTotal);
}
}
$this->_writeResponse($response, 201);
}
/**
* @api {put} /orders/:id/totals/:id Update Order Total
* @apiVersion 2.1.0
* @apiName UpdateOrderTotal
* @apiGroup Orders
*
* @apiDescription
* Use this method to update an existing order total. Use the same order total JSON format as in the POST method.
*
* @apiSuccess Response-Body If successful, this method returns the updated Order Total resource in the
* response body.
*
* @apiError 400-BadRequest The order total ID in the URI was not provided or is invalid.
* @apiErrorExample Error-Response (Missing ID)
* HTTP/1.1 400 Bad Request
* {
* "code": 400,
* "status": "error",
* "message": "Order total record ID was not provided or is invalid."
* }
*
* @apiError 400-BadRequest The request body is empty.
* @apiErrorExample Error-Response (Empty request body)
* HTTP/1.1 400 Bad Request
* {
* "code": 400,
* "status": "error",
* "message": "Order total data were not provided."
* }
*
* @apiError 404-NotFound The provided order total was not found in the given order.
* @apiErrorExample Error-Response (Not found in order)
* HTTP/1.1 404 Not Found
* {
* "code": 404,
* "status": "error",
* "message": "The provided order total ID does not exist in the given order!"
* }
*/
public function put()
{
if(!isset($this->uri[3]) || !is_numeric($this->uri[3]))
{
throw new HttpApiV2Exception('Order total record ID was not provided or is invalid: '
. gettype($this->uri[3]), 400);
}
$orderTotalJsonString = $this->api->request->getBody();
if(empty($orderTotalJsonString))
{
throw new HttpApiV2Exception('Order total data were not provided.', 400);
}
$orderId = new IdType($this->uri[1]);
$order = $this->orderReadService->getOrderById($orderId);
$baseOrderTotal = null;
foreach($order->getOrderTotals()->getArray() as $orderTotal)
{
if($orderTotal->getOrderTotalId() === (int)$this->uri[3])
{
$baseOrderTotal = $orderTotal;
break;
}
}
if($baseOrderTotal === null)
{
throw new HttpApiV2Exception('The provided order total ID does not exist in the given order!', 404);
}
// Ensure that the order total has the correct order total id of the request url
$orderTotalJsonString = $this->_setJsonValue($orderTotalJsonString, 'id', (int)$this->uri[3]);
$storedOrderTotal = $this->orderJsonSerializer->deserializeOrderTotal(json_decode($orderTotalJsonString),
$baseOrderTotal);
$this->orderWriteService->updateOrderTotal($storedOrderTotal);
$response = $this->orderJsonSerializer->serializeOrderTotal($storedOrderTotal);
$this->_writeResponse($response);
}
/**
* @api {delete} /orders/:id/totals/:id Delete Order Total
* @apiVersion 2.1.0
* @apiName DeleteOrderTotal
* @apiGroup Orders
*
* @apiDescription
* Use this method to remove an order total from an existing order.
*
* @apiSuccessExample {json} Success-Response
* {
* "code" => 200,
* "status" => "success",
* "action" => "delete",
* "resource" => "OrderTotal",
* "orderId" => 400345,
* "orderTotalId" => 1
* }
*
* @apiError 400-BadRequest The order total ID in the URI was not provided or is invalid.
* @apiError 404-NotFound The provided order total was not found in the given order.
*
* @apiErrorExample Error-Response
* HTTP/1.1 400 Bad Request
* {
* "code": 400,
* "status": "error",
* "message": "Order total record ID was not provided in the resource URL."
* }
*
*/
public function delete()
{
// Check if record ID was provided.
if(!isset($this->uri[3]) || !is_numeric($this->uri[3]))
{
throw new HttpApiV2Exception('Order total record ID was not provided in the resource URL.', 400);
}
$orderId = new IdType($this->uri[1]);
$order = $this->orderReadService->getOrderById($orderId);
$storedOrderTotal = null;
foreach($order->getOrderTotals()->getArray() as $orderTotal)
{
if($orderTotal->getOrderTotalId() === (int)$this->uri[3])
{
$storedOrderTotal = $orderTotal;
break;
}
}
if($storedOrderTotal === null)
{
throw new HttpApiV2Exception('The provided order total ID does not exist in the given order!', 404);
}
// Remove order total record from database.
$this->orderWriteService->removeOrderTotal($storedOrderTotal);
// Return response JSON.
$response = array(
'code' => 200,
'status' => 'success',
'action' => 'delete',
'resource' => 'OrderTotal',
'orderId' => (int)$this->uri[1],
'orderTotalId' => (int)$this->uri[3]
);
$this->_writeResponse($response);
}
/**
* @api {get} /orders/:id/totals/:id Get Order Total
* @apiVersion 2.1.0
* @apiName GetOrderTotal
* @apiGroup Orders
*
* @apiDescription
* Returns all or just a single order total from an existing orders. All the GET manipulation parameters are applied
* with this method (search, sort, minimize, paginate etc).
*
* @apiExample {curl} Get All Entries
* curl -i --user admin@shop.de:12345 http://shop.de/api.php/v2/orders/400785/totals
*
* @apiExample {curl} Get Entry With ID = 4
* curl -i --user admin@shop.de:12345 http://shop.de/api.php/v2/orders/400242/totals/4
*/
public function get()
{
$orderId = new IdType($this->uri[1]);
$order = $this->orderReadService->getOrderById($orderId);
$response = array();
foreach($order->getOrderTotals()->getArray() as $orderTotal)
{
$response[] = $this->orderJsonSerializer->serializeOrderTotal($orderTotal);
}
if(isset($this->uri[3]))
{
foreach($response as $item)
{
if($item['id'] === (int)$this->uri[3])
{
$response = $item;
break;
}
}
}
else if($this->api->request->get('q') !== null)
{
$this->_searchResponse($response, $this->api->request->get('q'));
}
$this->_sortResponse($response);
$this->_paginateResponse($response);
$this->_minimizeResponse($response);
$this->_linkResponse($response);
$this->_writeResponse($response);
}
}