1 <?php
2
3 /* --------------------------------------------------------------
4 OrdersItemsApiV2Controller.inc.php 2016-02-24
5 Gambio GmbH
6 http://www.gambio.de
7 Copyright (c) 2016 Gambio GmbH
8 Released under the GNU General Public License (Version 2)
9 [http://www.gnu.org/licenses/gpl-2.0.html]
10 --------------------------------------------------------------
11 */
12
13 MainFactory::load_class('HttpApiV2Controller');
14
15 /**
16 * Class OrdersItemsApiV2Controller
17 *
18 * Notice: This controller is a sub-resource of the OrdersApiV2Controller.
19 *
20 * @category System
21 * @package ApiV2Controllers
22 */
23 class OrdersItemsApiV2Controller extends HttpApiV2Controller
24 {
25 /**
26 * Order write service.
27 *
28 * @var OrderWriteService
29 */
30 protected $orderWriteService;
31
32 /**
33 * Order read service.
34 *
35 * @var OrderReadService
36 */
37 protected $orderReadService;
38
39 /**
40 * Order JSON serializer.
41 *
42 * @var OrderJsonSerializer
43 */
44 protected $orderJsonSerializer;
45
46 /**
47 * Sub resources.
48 *
49 * @var array
50 */
51 protected $subresource;
52
53
54 /**
55 * Initializes API Controller
56 * @throws HttpApiV2Exception On missing order ID.
57 */
58 protected function __initialize()
59 {
60 // Check if the order ID was provided
61 if(!isset($this->uri[1]) || !is_numeric($this->uri[1]))
62 {
63 throw new HttpApiV2Exception('Order record ID was not provided in the resource URL.', 400);
64 }
65
66 $this->orderWriteService = StaticGXCoreLoader::getService('OrderWrite');
67 $this->orderReadService = StaticGXCoreLoader::getService('OrderRead');
68 $this->orderJsonSerializer = MainFactory::create('OrderJsonSerializer');
69 $this->subresource = array(
70 'attributes' => 'OrdersItemsAttributesApiV2Controller',
71 'properties' => 'OrdersItemsAttributesApiV2Controller'
72 );
73 }
74
75
76 /**
77 * @api {post} /orders/:id/items Create Order Item
78 * @apiVersion 2.1.0
79 * @apiName CreateOrderItem
80 * @apiGroup Orders
81 *
82 * @apiDescription
83 * Use this method to create a new order item to an existing order. The order item JSON format must be the
84 * same with the "items" entries in the original order item.
85 *
86 * @apiExample {json} Request-Body
87 * {
88 * "model": "12345-s-black",
89 * "name": "Ein Artikel",
90 * "quantity": 1,
91 * "price": 11,
92 * "finalPrice": 11,
93 * "tax": 19,
94 * "isTaxAllowed": true,
95 * "discount": 0,
96 * "shippingTimeInformation": "",
97 * "checkoutInformation": "Checkout information goes here ...",
98 * "attributes": [
99 * {
100 * "id": 1,
101 * "name": "Farbe",
102 * "value": "rot",
103 * "price": 0,
104 * "priceType": "+",
105 * "optionId": 1,
106 * "optionValueId": 1,
107 * "combisId": null
108 * }
109 * ],
110 * "downloadInformation": {
111 * "filename": "Dokument.pdf",
112 * "maxDaysAllowed": 5,
113 * "countAvailable": 14
114 * },
115 * "addonValues": {
116 * "productId": "2"
117 * }
118 * }
119 *
120 * @apiSuccess (Success 201) Response-Body If successful, this method returns a complete Order Item resource in the
121 * response body.
122 *
123 * @apiError 400-BadRequest The request body was empty.
124 *
125 * @apiError (Error 5xx) 500-InternalError One of the given properties has an invalid value type.
126 *
127 * @apiErrorExample Error-Response
128 * HTTP/1.1 400 Bad Request
129 * {
130 * "code": 400,
131 * "status": "error",
132 * "message": "Order item data were not provided."
133 * }
134 */
135 public function post()
136 {
137 if($this->_mapResponse($this->subresource))
138 {
139 return;
140 }
141
142 $orderItemJsonString = $this->api->request->getBody();
143
144 if(empty($orderItemJsonString))
145 {
146 throw new HttpApiV2Exception('Order item data were not provided.', 400);
147 }
148
149 $orderItem = $this->orderJsonSerializer->deserializeOrderItem(json_decode($orderItemJsonString));
150
151 $orderId = new IdType($this->uri[1]);
152
153 $orderItemID = $this->orderWriteService->addOrderItem($orderId, $orderItem);
154
155 $storedOrderItem = $this->orderReadService->getOrderItemById(new IdType($orderItemID));
156 $response = $this->orderJsonSerializer->serializeOrderItem($storedOrderItem);
157
158 $this->_writeResponse($response, 201);
159 }
160
161
162 /**
163 * @api {put} /orders/:id/items/:id Update Order Item
164 * @apiVersion 2.1.0
165 * @apiName UpdateOrderItem
166 * @apiGroup Orders
167 *
168 * @apiDescription
169 * Use this method to update an existing order item. Use the same order item JSON format as in the POST method.
170 *
171 * @apiSuccess Response-Body If successful, this method returns the updated Order Item resource in the response body.
172 *
173 * @apiError 400-BadRequest The request body is empty or the order item ID in the URI was not provided or is invalid.
174 *
175 * @apiErrorExample Error-Response (Empty request body)
176 * HTTP/1.1 400 Bad Request
177 * {
178 * "code": 400,
179 * "status": "error",
180 * "message": "Order item data were not provided."
181 * }
182 *
183 * @apiErrorExample Error-Response (Missing or invalid ID)
184 * HTTP/1.1 400 Bad Request
185 * {
186 * "code": 400,
187 * "status": "error",
188 * "message": "Order item record ID was not provided or is invalid."
189 * }
190 *
191 * @apiError 404-NotFound The provided order item was not found in the given order.
192 *
193 * @apiErrorExample Error-Response (Not found in order)
194 * HTTP/1.1 404 Not Found
195 * {
196 * "code": 404,
197 * "status": "error",
198 * "message": "The provided order item ID does not exist in the given order!"
199 * }
200 */
201 public function put()
202 {
203 if(!isset($this->uri[3]) || !is_numeric($this->uri[3]))
204 {
205 throw new HttpApiV2Exception('Order item record ID was not provided or is invalid: '
206 . gettype($this->uri[3]), 400);
207 }
208
209 if($this->_mapResponse($this->subresource))
210 {
211 return;
212 }
213
214 $orderItemJsonString = $this->api->request->getBody();
215
216 if(empty($orderItemJsonString))
217 {
218 throw new HttpApiV2Exception('Order item data were not provided.', 400);
219 }
220
221 $orderItemId = new IdType($this->uri[3]);
222 $baseOrderItem = $this->orderReadService->getOrderItemById($orderItemId);
223
224 if($baseOrderItem === null)
225 {
226 throw new HttpApiV2Exception('The provided order item ID does not exist in the given order!', 404);
227 }
228
229 // Ensure that the order item has the correct order item id of the request url
230 $orderItemJsonString = $this->_setJsonValue($orderItemJsonString, 'id', $orderItemId->asInt());
231
232 $storedOrderItem = $this->orderJsonSerializer->deserializeOrderItem(json_decode($orderItemJsonString),
233 $baseOrderItem);
234
235 $this->orderWriteService->updateOrderItem($storedOrderItem);
236
237 $response = $this->orderJsonSerializer->serializeOrderItem($storedOrderItem);
238 $this->_linkResponse($response);
239 $this->_writeResponse($response, 200);
240 }
241
242
243 /**
244 * @api {delete} /orders/:id/items/:id Delete Order Item
245 * @apiVersion 2.1.0
246 * @apiName DeleteOrderItem
247 * @apiGroup Orders
248 *
249 * @apiDescription
250 * Use this method to remove an order item from an existing order.
251 *
252 * @apiSuccessExample {json} Success-Response
253 * {
254 * "code": 200,
255 * "status": "success",
256 * "action" :"delete",
257 * "resource": "OrderItem",
258 * "orderId": 400883,
259 * "orderItemId": 1
260 * }
261 *
262 * @apiError 400-BadRequest The order item ID in the URI was not provided or is invalid.
263 * @apiErrorExample Error-Response (Missing ID)
264 * HTTP/1.1 400 Bad Request
265 * {
266 * "code": 400,
267 * "status": "error",
268 * "message": "Order item record ID was not provided in the resource URL."
269 * }
270 *
271 * @apiError 404-NotFound The provided order item was not found in the given order.
272 * @apiErrorExample Error-Response (Not found in order)
273 * HTTP/1.1 404 Not Found
274 * {
275 * "code": 404,
276 * "status": "error",
277 * "message": "The provided order item ID does not exist in the given order!"
278 * }
279 */
280 public function delete()
281 {
282 // Check if record ID was provided.
283 if(!isset($this->uri[3]) || !is_numeric($this->uri[3]))
284 {
285 throw new HttpApiV2Exception('Order item record ID was not provided in the resource URL.', 400);
286 }
287
288 if($this->_mapResponse($this->subresource))
289 {
290 return;
291 }
292
293 $orderId = new IdType($this->uri[1]);
294
295 $order = $this->orderReadService->getOrderById($orderId);
296
297 $storedOrderItem = null;
298 foreach($order->getOrderItems()->getArray() as $orderItem)
299 {
300 if($orderItem->getOrderItemId() === (int)$this->uri[3])
301 {
302 $storedOrderItem = $orderItem;
303 break;
304 }
305 }
306
307 // @todo The delete method must not through exceptions if the record was not found.
308 if($storedOrderItem === null)
309 {
310 throw new HttpApiV2Exception('The provided order item ID does not exist in the given order!', 404);
311 }
312
313 // Remove order item record from database.
314 $this->orderWriteService->removeOrderItem($storedOrderItem);
315
316 // Return response JSON.
317 $response = array(
318 'code' => 200,
319 'status' => 'success',
320 'action' => 'delete',
321 'resource' => 'OrderItem',
322 'orderId' => (int)$this->uri[1],
323 'orderItemId' => (int)$this->uri[3]
324 );
325
326 $this->_writeResponse($response);
327 }
328
329
330 /**
331 * @api {get} /orders/:id/items/:id Get Order Item
332 * @apiVersion 2.1.0
333 * @apiName GetOrderItem
334 * @apiGroup Orders
335 *
336 * @apiDescription
337 * Get all or just a single order item from an existing orders. All the GET manipulation parameters are applied
338 * with this method (search, sort, minimize, paginate etc).
339 *
340 * @apiExample {curl} Get All Entries
341 * curl -i --user admin@shop.de:12345 http://shop.de/api.php/v2/orders/400785/items
342 *
343 * @apiExample {curl} Get Entry With ID=8
344 * curl -i --user admin@shop.de:12345 http://shop.de/api.php/v2/orders/400242/items/8
345 *
346 * @apiExample {curl} Minimize Responses
347 * curl -i --user admin@shop.de:12345
348 * http://shop.de/api.php/v2/orders/400871/items?fields=id,model,name,quanity
349 *
350 * @apiParam {Number} [id] Record ID of resource to be returned. If omitted all records
351 * will be included in the response.
352 */
353 public function get()
354 {
355 if($this->_mapResponse($this->subresource))
356 {
357 return;
358 }
359
360 $orderId = new IdType($this->uri[1]);
361 $order = $this->orderReadService->getOrderById($orderId);
362
363 $response = array();
364 foreach($order->getOrderItems()->getArray() as $orderItem)
365 {
366 $response[] = $this->orderJsonSerializer->serializeOrderItem($orderItem);
367 }
368
369 if(isset($this->uri[3]))
370 {
371 foreach($response as $item)
372 {
373 if($item['id'] === (int)$this->uri[3])
374 {
375 $response = $item;
376 break;
377 }
378 }
379 }
380 else if($this->api->request->get('q') !== null)
381 {
382 $this->_searchResponse($response, $this->api->request->get('q'));
383 }
384
385 $this->_paginateResponse($response);
386 $this->_sortResponse($response);
387 $this->_minimizeResponse($response);
388 $this->_linkResponse($response);
389 $this->_writeResponse($response);
390 }
391 }
392