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