1 <?php
2 /* --------------------------------------------------------------
3 ZonesApiV2Controller.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 ZonesApiV2Controller
16 *
17 * Provides a gateway to the CountryService which handles the shop zone resources.
18 *
19 * @category System
20 * @package ApiV2Controllers
21 */
22 class ZonesApiV2Controller extends HttpApiV2Controller
23 {
24 /**
25 * @var CountryServiceInterface
26 */
27 protected $countryService;
28
29 /**
30 * @var ZoneJsonSerializer
31 */
32 protected $zoneSerializer;
33
34
35 /**
36 * Initialize Controller
37 */
38 public function __initialize()
39 {
40 $this->countryService = StaticGXCoreLoader::getService('Country');
41 $this->zoneSerializer = MainFactory::create('ZoneJsonSerializer');
42 }
43
44
45 /**
46 * @api {get} /zones/:id Get Zones
47 * @apiVersion 2.1.0
48 * @apiName GetZone
49 * @apiGroup Zones
50 *
51 * @apiDescription
52 * Get a single registered zone resource. This method is currently limited to only fetching
53 * a single zone and might be updated in a future version of the API.
54 *
55 * @apiParam {int} id (required) The zone record ID to be returned.
56 *
57 * @apiExample {curl} Get Zone With ID = 84
58 * curl --user admin@shop.de:12345 http://shop.de/api.php/v2/zones/84
59 *
60 * @apiSuccess Response-Body If successful, this method will return the zone resource in JSON format.
61 *
62 * @apiError (Error 5xx) 501-NotImplemented If the ID is not included in the request URI then the API will return
63 * a 501 error status because it cannot return all the registered countries.
64 */
65 public function get()
66 {
67 if(!isset($this->uri[1]))
68 {
69 throw new HttpApiV2Exception('Cannot fetch all registered zones, operation is not implemented yet.', 501);
70 }
71
72 if(!is_numeric($this->uri[1]))
73 {
74 throw new HttpApiV2Exception('Provided zone ID is invalid, integer expected: ' . gettype($this->uri[1]),
75 400);
76 }
77
78 $zone = $this->countryService->getCountryZoneById(new IdType((int)$this->uri[1]));
79 $this->_writeResponse($this->zoneSerializer->serialize($zone, false));
80 }
81
82
83 /**
84 * POST operation is not supported by the CountryService.
85 *
86 * @throws HttpApiV2Exception
87 */
88 public function post()
89 {
90 throw new HttpApiV2Exception('Operation is not currently allowed for the zone resources.', 405);
91 }
92
93
94 /**
95 * PUT operation is not supported by the CountryService.
96 *
97 * @throws HttpApiV2Exception
98 */
99 public function put()
100 {
101 throw new HttpApiV2Exception('Operation is not currently allowed for the zone resources.', 405);
102 }
103
104
105 /**
106 * DELETE operation is not supported by the CountryService.
107 *
108 * @throws HttpApiV2Exception
109 */
110 public function delete()
111 {
112 throw new HttpApiV2Exception('Operation is not currently allowed for the zone resources.', 405);
113 }
114 }