1 <?php
2 /* --------------------------------------------------------------
3 CountriesApiV2Controller.inc.php 2016-03-07
4 Gambio GmbH
5 http://www.gambio.de
6 Copyright (c) 2016 Gambio GmbH
7 Released under the GNU General Public License (Version 2)
8 [http://www.gnu.org/licenses/gpl-2.0.html]
9 --------------------------------------------------------------
10 */
11
12 MainFactory::load_class('HttpApiV2Controller');
13
14 /**
15 * Class CountriesApiV2Controller
16 *
17 * Provides a gateway to the CountryService which handles the shop country resources.
18 *
19 * @category System
20 * @package ApiV2Controllers
21 */
22 class CountriesApiV2Controller extends HttpApiV2Controller
23 {
24 /**
25 * @var CountryServiceInterface
26 */
27 protected $countryService;
28
29 /**
30 * @var CountryJsonSerializer
31 */
32 protected $countrySerializer;
33
34
35 /**
36 * Initialize Controller
37 */
38 public function __initialize()
39 {
40 $this->countryService = StaticGXCoreLoader::getService('Country');
41 $this->countrySerializer = MainFactory::create('CountryJsonSerializer');
42 }
43
44
45 /**
46 * @api {get} /countries/:id Get Countries
47 * @apiVersion 2.1.0
48 * @apiName GetCountry
49 * @apiGroup Countries
50 *
51 * @apiDescription
52 * Get a single country or a specific country's zones. This method is currently limited to
53 * only fetching a single country resource so make sure that you provide the country ID in
54 * the request URI.
55 *
56 * @apiExample {curl} Get Country With ID = 81
57 * curl --user admin@shop.de:12345 http://shop.de/api.php/v2/countries/81
58 *
59 * @apiExample {curl} Get Zones of Country with ID = 81
60 * curl --user admin@shop.de:12345 http://shop.de/api.php/v2/countries/81/zones
61 *
62 * @apiSuccess Response-Body If successful, this method will return the country resource in JSON format.
63 *
64 * @apiError (Error 5xx) 500-InternalError If the record is not found or something else goes wrong the API will
65 * return a 500 error status. Read the message for more info.
66 *
67 * @apiError (Error 5xx) 501-NotImplemented If the ID is not included in the request URI then the API will return
68 * a 501 error status because it cannot return all the registered countries.
69 */
70 public function get()
71 {
72 // Get all countries is not supported.
73 if(!isset($this->uri[1]))
74 {
75 throw new HttpApiV2Exception('Cannot fetch all registered countries, operation is not implemented yet.',
76 501);
77 }
78
79 // Country ID was not provided.
80 if(!is_numeric($this->uri[1]))
81 {
82 throw new HttpApiV2Exception('Provided country ID is invalid, integer expected: ' . gettype($this->uri[1]),
83 400);
84 }
85
86 // Sub Resource Country Zones
87 if(isset($this->uri[2]) && $this->uri[2] === 'zones')
88 {
89 $this->_getCountryZones();
90
91 return;
92 }
93
94 $country = $this->countryService->getCountryById(new IdType((int)$this->uri[1]));
95 $this->_writeResponse($this->countrySerializer->serialize($country, false));
96 }
97
98 /**
99 * Sub Resource for the country zones.
100 *
101 * The API consumer can easily get the zones that are related with a specific country.
102 */
103 protected function _getCountryZones()
104 {
105 $zoneSerializer = MainFactory::create('ZoneJsonSerializer');
106
107 $zones = $this->countryService->findCountryZonesByCountryId(new IdType((int)$this->uri[1]));
108
109 $response = array();
110 foreach($zones as $zone)
111 {
112 $response[] = $zoneSerializer->serialize($zone, false);
113 }
114 $this->_sortResponse($response);
115 $this->_paginateResponse($response);
116 $this->_minimizeResponse($response);
117 $this->_writeResponse($response);
118 }
119 }