1 <?php
2
3 /* --------------------------------------------------------------
4 OrdersHistoryApiV2Controller.inc.php 2016-01-20
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 OrdersStatusHistoryApiV2Controller
17 *
18 * Notice: This controller is a sub-resource of the OrdersV2Controller.
19 *
20 * @category System
21 * @package ApiV2Controllers
22 */
23 class OrdersHistoryApiV2Controller 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 ID.
51 */
52 protected function __initialize()
53 {
54 if(!isset($this->uri[1]) && !is_numeric($this->uri[1]))
55 {
56 throw new HttpApiV2Exception('Order record ID was not provided in the resource URL or is not valid.', 400);
57 }
58
59 $this->orderWriteService = StaticGXCoreLoader::getService('OrderWrite');
60 $this->orderReadService = StaticGXCoreLoader::getService('OrderRead');
61 $this->orderJsonSerializer = MainFactory::create('OrderJsonSerializer');
62 }
63
64
65 /**
66 * @api {get} /orders/:id/history/:id Get Order History
67 * @apiVersion 2.1.0
68 * @apiName GetOrderHistory
69 * @apiGroup Orders
70 *
71 * @apiDescription
72 * Returns multiple or a single order status history records. This method supports all the GET parameters that are
73 * mentioned in the "Introduction" section of this documentation.
74 *
75 * @apiExample {curl} Get All Entries
76 * curl -i --user admin@shop.de:12345 http://shop.de/api.php/v2/orders/400210/history
77 *
78 * @apiExample {curl} Get Entry With ID = 3
79 * curl -i --user admin@shop.de:12345 http://shop.de/api.php/v2/orders/400210/history/3
80 */
81 public function get()
82 {
83 $order = $this->orderReadService->getOrderById(new IdType($this->uri[1]));
84
85 $response = array();
86
87 foreach($order->getStatusHistory()->getArray() as $orderStatusHistoryListItem)
88 {
89 $response[] = $this->orderJsonSerializer->serializeOrderStatusHistoryListItem($orderStatusHistoryListItem);
90 }
91
92 if(isset($this->uri[3]))
93 {
94 foreach($response as $item)
95 {
96 if($item['id'] === (int)$this->uri[3])
97 {
98 $response = $item;
99 break;
100 }
101 }
102 }
103 else if($this->api->request->get('q') !== null)
104 {
105 $this->_searchResponse($response, $this->api->request->get('q'));
106 }
107
108 $this->_paginateResponse($response);
109 $this->_sortResponse($response);
110 $this->_minimizeResponse($response);
111 $this->_linkResponse($response);
112 $this->_writeResponse($response);
113 }
114 }
115