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 
<?php
/* --------------------------------------------------------------
   ZonesApiV2Controller.inc.php 2016-03-07
   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 ZonesApiV2Controller
 *
 * Provides a gateway to the CountryService which handles the shop zone resources.
 *
 * @category   System
 * @package    ApiV2Controllers
 */
class ZonesApiV2Controller extends HttpApiV2Controller
{
    /**
     * @var CountryServiceInterface
     */
    protected $countryService;

    /**
     * @var ZoneJsonSerializer
     */
    protected $zoneSerializer;


    /**
     * Initialize Controller
     */
    public function __initialize()
    {
        $this->countryService = StaticGXCoreLoader::getService('Country');
        $this->zoneSerializer = MainFactory::create('ZoneJsonSerializer');
    }


    /**
     * @api        {get} /zones/:id Get Zones
     * @apiVersion 2.1.0
     * @apiName    GetZone
     * @apiGroup   Zones
     *
     * @apiDescription
     * Get a single registered zone resource. This method is currently limited to only fetching
     * a single zone and might be updated in a future version of the API.
     *
     * @apiParam {int} id (required) The zone record ID to be returned.
     *
     * @apiExample {curl} Get Zone With ID = 84
     *             curl --user admin@shop.de:12345 http://shop.de/api.php/v2/zones/84
     *
     * @apiSuccess Response-Body If successful, this method will return the zone resource in JSON format.
     *
     * @apiError (Error 5xx) 501-NotImplemented If the ID is not included in the request URI then the API will return
     * a 501 error status because it cannot return all the registered countries.
     */
    public function get()
    {
        if(!isset($this->uri[1]))
        {
            throw new HttpApiV2Exception('Cannot fetch all registered zones, operation is not implemented yet.', 501);
        }

        if(!is_numeric($this->uri[1]))
        {
            throw new HttpApiV2Exception('Provided zone ID is invalid, integer expected: ' . gettype($this->uri[1]),
                                         400);
        }

        $zone = $this->countryService->getCountryZoneById(new IdType((int)$this->uri[1]));
        $this->_writeResponse($this->zoneSerializer->serialize($zone, false));
    }


    /**
     * POST operation is not supported by the CountryService.
     *
     * @throws HttpApiV2Exception
     */
    public function post()
    {
        throw new HttpApiV2Exception('Operation is not currently allowed for the zone resources.', 405);
    }


    /**
     * PUT operation is not supported by the CountryService.
     *
     * @throws HttpApiV2Exception
     */
    public function put()
    {
        throw new HttpApiV2Exception('Operation is not currently allowed for the zone resources.', 405);
    }


    /**
     * DELETE operation is not supported by the CountryService.
     *
     * @throws HttpApiV2Exception
     */
    public function delete()
    {
        throw new HttpApiV2Exception('Operation is not currently allowed for the zone resources.', 405);
    }
}