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 
<?php

/* --------------------------------------------------------------
   OrdersHistoryApiV2Controller.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 OrdersStatusHistoryApiV2Controller
 *
 * Notice: This controller is a sub-resource of the OrdersV2Controller.
 *
 * @category   System
 * @package    ApiV2Controllers
 */
class OrdersHistoryApiV2Controller 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 ID.
     */
    protected function __initialize()
    {
        if(!isset($this->uri[1]) && !is_numeric($this->uri[1]))
        {
            throw new HttpApiV2Exception('Order record ID was not provided in the resource URL or is not valid.', 400);
        }

        $this->orderWriteService   = StaticGXCoreLoader::getService('OrderWrite');
        $this->orderReadService    = StaticGXCoreLoader::getService('OrderRead');
        $this->orderJsonSerializer = MainFactory::create('OrderJsonSerializer');
    }


    /**
     * @api        {get} /orders/:id/history/:id Get Order History
     * @apiVersion 2.1.0
     * @apiName    GetOrderHistory
     * @apiGroup   Orders
     *
     * @apiDescription
     * Returns multiple or a single order status history records. This method supports all the GET parameters that are
     * mentioned in the "Introduction" section of this documentation.
     *
     * @apiExample {curl} Get All Entries
     *             curl -i --user admin@shop.de:12345 http://shop.de/api.php/v2/orders/400210/history
     *
     * @apiExample {curl} Get Entry With ID = 3
     *             curl -i --user admin@shop.de:12345 http://shop.de/api.php/v2/orders/400210/history/3
     */
    public function get()
    {
        $order = $this->orderReadService->getOrderById(new IdType($this->uri[1]));

        $response = array();

        foreach($order->getStatusHistory()->getArray() as $orderStatusHistoryListItem)
        {
            $response[] = $this->orderJsonSerializer->serializeOrderStatusHistoryListItem($orderStatusHistoryListItem);
        }

        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);
    }
}