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 116 117 118 119 120 121 
<?php
/* --------------------------------------------------------------
   CountriesApiV2Controller.inc.php 2017-03-30
   Gambio GmbH
   http://www.gambio.de
   Copyright (c) 2017 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 CountriesApiV2Controller
 *
 * Provides a gateway to the CountryService which handles the shop country resources.
 *
 * @category   System
 * @package    ApiV2Controllers
 */
class CountriesApiV2Controller extends HttpApiV2Controller
{
    /**
     * @var CountryServiceInterface
     */
    protected $countryService;

    /**
     * @var CountryJsonSerializer
     */
    protected $countrySerializer;


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


    /**
     * @api        {get} /countries/:id Get Countries
     * @apiVersion 2.1.0
     * @apiName    GetCountry
     * @apiGroup   Countries
     *
     * @apiDescription
     * Get a single country or a specific country's zones. This method is currently limited to
     * only fetching a single country resource so make sure that you provide the country ID in
     * the request URI.
     *
     * @apiExample {curl} Get Country With ID = 81
     *             curl --user admin@shop.de:12345 http://shop.de/api.php/v2/countries/81
     *
     * @apiExample {curl} Get Zones of Country with ID = 81
     *             curl --user admin@shop.de:12345 http://shop.de/api.php/v2/countries/81/zones
     *
     * @apiSuccess Response-Body If successful, this method will return the country resource in JSON format.
     *
     * @apiError (Error 5xx) 500-InternalError If the record is not found or something else goes wrong the API will
     * return a 500 error status. Read the message for more info.
     *
     * @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()
    {
        // Get all countries is not supported.
        if(!isset($this->uri[1]))
        {
            throw new HttpApiV2Exception('Cannot fetch all registered countries, operation is not implemented yet.',
                                         501);
        }

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

        // Sub Resource Country Zones 
        if(isset($this->uri[2]) && $this->uri[2] === 'zones')
        {
            $this->_getCountryZones();

            return;
        }

        $country = $this->countryService->getCountryById(new IdType((int)$this->uri[1]));
        $response = $this->countrySerializer->serialize($country, false);
        $this->_linkResponse($response);
        $this->_writeResponse($response);
    }

    /**
     * Sub Resource for the country zones.
     *
     * The API consumer can easily get the zones that are related with a specific country.
     */
    protected function _getCountryZones()
    {
        $zoneSerializer = MainFactory::create('ZoneJsonSerializer');

        $zones = $this->countryService->findCountryZonesByCountryId(new IdType((int)$this->uri[1]));

        $response = array();
        foreach($zones as $zone)
        {
            $response[] = $zoneSerializer->serialize($zone, false);
        }
        $this->_sortResponse($response);
        $this->_paginateResponse($response);
        $this->_minimizeResponse($response);
        $this->_writeResponse($response);
    }
}