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 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546
<?php
/* --------------------------------------------------------------
CustomerGroupsApiV2Controller.inc.php 2017-09-28
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]
--------------------------------------------------------------
*/
class CustomerGroupsApiV2Controller extends HttpApiV2Controller
{
/**
* @var \CustomerGroupReadServiceInterface
*/
protected $readService;
/**
* @var \CustomerGroupWriteServiceInterface
*/
protected $writeService;
/**
* Initialize controller components.
*/
protected function __initialize()
{
$this->readService = StaticGXCoreLoader::getService('CustomerGroupRead');
$this->writeService = StaticGXCoreLoader::getService('CustomerGroupWrite');
}
/**
* @api {get} /customer_groups/:id Get customer groups
* @apiVersion 2.5.0
* @apiName GetCustomerGroups
* @apiGroup CustomerGroups
*
* @apiDescription
* Get multiple or a single customer groups record through the GET method
*
* @apiExample {curl} Get All CustomerGroups records
* curl -i --user admin@shop.de:12345 http://shop.de/api.php/v2/customer_groups
*
* @apiExample {curl} Get CustomerGroups record With ID = 982
* curl -i --user admin@shop.de:12345 http://shop.de/api.php/v2/customer_groups/982
*
* @apiError 400-BadRequest CustomerGroups data were not provided or customer groups record ID was not
* provided or is invalid.
*
* @apiErrorExample Error-Response (Missing or invalid ID)
* HTTP/1.1 400 Bad Request
* {
* "code": 400,
* "status": "error",
* "message": "CustomerGroups record ID was not provided or is invalid."
* }
*
* @apiError 404-NotFound CustomerGroups record could not be found.
*
* @apiErrorExample Error-Response (CustomerGroups Not Found)
* HTTP/1.1 404 Not Found
* {
* "code": 404,
* "status": "error",
* "message": "CustomerGroups record could not be found."
* }
*/
public function get()
{
if(isset($this->uri[1]) && is_numeric($this->uri[1]))
{
try
{
$customerGroup = $this->readService->getById(new IntType((int)$this->uri[1]));
return $this->_writeResponse($this->_serializeCustomerGroup($customerGroup));
}
catch(EntityNotFoundException $e)
{
throw new HttpApiV2Exception($e->getMessage(), 404, $e);
}
}
if(isset($this->uri[1]) && !is_numeric($this->uri[1]))
{
throw new HttpApiV2Exception('Customer group record is was not provided', 400);
}
return $this->_writeResponse($this->_serializeCustomerGroupCollection($this->readService->getAll()));
}
/**
* @api {post} /customer_groups Create CustomerGroups
* @apiVersion 2.5.0
* @apiName CreateCustomerGroups
* @apiGroup CustomerGroups
*
* @apiDescription
* This method enables the creation of a new CustomerGroups into the system.
*
* @apiParamExample {json} CustomerGroups entity
* {
* "names": {
* "EN": "Kids",
* "DE": "Kinder"
* },
* "settings": {
* "public": false,
* "otDiscountFlag": false,
* "graduatedPrices": false,
* "showPrice": true,
* "showPriceTax": false,
* "addTaxOt": false,
* "discountAttributes": false,
* "fsk18": false,
* "fsk18Display": false,
* "writeReviews": false,
* "readReviews": false
* },
* "configurations": {
* "minOrder": 2.50,
* "maxOrder": 20.00,
* "discount": 0.5,
* "otDiscount": 0,
* "unallowedPaymentModules": [
* "paypal", "cod", "moneyorder"
* ],
* "unallowedShippingModules": [
* "selfpickup"
* ]
* }
* }
*
* @apiParam {Object} names Object with language code as keys and name as values.
* @apiParam {Object} settings Object with customer group settings, visit the example for further information.
* @apiParam {Object} configurations Object with customer group configurations, visit the example for further
* information.
*
* @apiSuccess (201) Request-Body If successful, this method returns the complete customerGroups resource
* in the response body.
*
* @apiSuccessExample {json} Success-Response
* {
* "id": 2,
* "names": {
* "EN": "Kids",
* "DE": "Kinder"
* },
* "settings": {
* "public": false,
* "otDiscountFlag": false,
* "graduatedPrices": false,
* "showPrice": true,
* "showPriceTax": false,
* "addTaxOt": false,
* "discountAttributes": false,
* "fsk18": false,
* "fsk18Display": false,
* "writeReviews": false,
* "readReviews": false
* },
* "configurations": {
* "minOrder": 2.50,
* "maxOrder": 20.00,
* "discount": 0.5,
* "otDiscount": 0,
* "unallowedPaymentModules": [
* "paypal", "cod", "moneyorder"
* ],
* "unallowedShippingModules": [
* "selfpickup"
* ]
* }
* }
*
* @apiError 400-BadRequest The body of the request was empty.
* @apiErrorExample Error-Response
* HTTP/1.1 400 Bad Request
* {
* "code": 400,
* "status": "error",
* "message": "CustomerGroups data were not provided."
* }
*/
public function post()
{
$customerGroup = $this->_deserializeCustomerGroup($this->api->request->getBody());
$customerGroup->store();
$this->_writeResponse($this->_serializeCustomerGroup($customerGroup));
}
/**
* @api {patch} /customer_groups/:id Updates CustomerGroup entity
* @apiVersion 2.5.0
* @apiName UpdateCustomerGroups
* @apiGroup CustomerGroups
*
* @apiDescription
* Use this method if you want to update an existing customer groups record.
*
* @apiParamExample {json} CustomerGroups entity
* {
* "names": {
* "EN": "Kids",
* "DE": "Kinder"
* },
* "settings": {
* "public": false,
* "otDiscountFlag": false,
* "graduatedPrices": false,
* "showPrice": true,
* "showPriceTax": false,
* "addTaxOt": false,
* "discountAttributes": false,
* "fsk18": false,
* "fsk18Display": false,
* "writeReviews": false,
* "readReviews": false
* },
* "configurations": {
* "minOrder": 2.50,
* "maxOrder": 20.00,
* "discount": 0.5,
* "otDiscount": 0,
* "unallowedPaymentModules": [
* "paypal", "cod", "moneyorder"
* ],
* "unallowedShippingModules": [
* "selfpickup"
* ]
* }
* }
*
* @apiParam {Object} names Object with language code as keys and name as values.
* @apiParam {Object} settings Object with customer group settings, visit the example for further information.
* @apiParam {Object} configurations Object with customer group configurations, visit the example for further
* information.
*
* @apiSuccess (200) Request-Body If successful, this method returns the complete customerGroups resource
* in the response body.
*
* @apiSuccessExample {json} Success-Response
* {
* "id": 2,
* "names": {
* "EN": "Kids",
* "DE": "Kinder"
* },
* "settings": {
* "public": false,
* "otDiscountFlag": false,
* "graduatedPrices": false,
* "showPrice": true,
* "showPriceTax": false,
* "addTaxOt": false,
* "discountAttributes": false,
* "fsk18": false,
* "fsk18Display": false,
* "writeReviews": false,
* "readReviews": false
* },
* "configurations": {
* "minOrder": 2.50,
* "maxOrder": 20.00,
* "discount": 0.5,
* "otDiscount": 0,
* "unallowedPaymentModules": [
* "paypal", "cod", "moneyorder"
* ],
* "unallowedShippingModules": [
* "selfpickup"
* ]
* }
* }
*
* @apiError 400-BadRequest CustomerGroups data were not provided or customerGroups record ID was not
* provided or is invalid.
*
* @apiErrorExample Error-Response (Empty request body)
* HTTP/1.1 400 Bad Request
* {
* "code": 400,
* "status": "error",
* "message": "CustomerGroups data were not provided."
* }
*
* @apiErrorExample Error-Response (Missing or invalid ID)
* HTTP/1.1 400 Bad Request
* {
* "code": 400,
* "status": "error",
* "message": "CustomerGroups record ID was not provided or is invalid."
* }
*
* @apiError 404-NotFoundRequest CustomerGroups data were not provided or customerGroups record ID was not
* provided or is invalid.
*
* @apiErrorExample Error-Response (Entity not found)
* HTTP/1.1 400 Bad Request
* {
* "code": 404,
* "status": "error",
* "message": "Customer group entity was not found with provided id [ID]"
* }
*/
public function patch()
{
if(!isset($this->uri[1]) || !is_numeric($this->uri[1]))
{
throw new HttpApiV2Exception('Customer group record ID was not provided or is invalid in the Request-URI');
}
$requestBody = $this->api->request->getBody();
if(empty($requestBody))
{
throw new HttpApiV2Exception('Customer group data were not provided.', 400);
}
try
{
$customerGroup = $this->_deserializeCustomerGroup($requestBody, $this->uri[1]);
$customerGroup->update();
$this->_writeResponse($this->_serializeCustomerGroup($customerGroup));
}
catch(EntityNotFoundException $e)
{
throw new HttpApiV2Exception($e->getMessage(), 404, $e);
}
}
/**
* @api {delete} /customer_groups/:id Delete CustomerGroups
* @apiVersion 2.5.0
* @apiName DeleteCustomerGroups
* @apiGroup CustomerGroups
*
* @apiDescription
* Removes a customer groups record from the system. This method will always return success
* even if the customer group does not exist.
*
* @apiExample {curl} Delete CustomerGroups with ID = 84
* curl -X DELETE --user admin@shop.de:12345 http://shop.de/api.php/v2/customer_groups/84
*
* @apiSuccessExample {json} Success-Response
* {
* "code": 200,
* "status": "success",
* "action": "delete",
* "customerGroupId": 84
* }
*/
public function delete()
{
if(!isset($this->uri[1]) || !is_numeric($this->uri[1]))
{
throw new HttpApiV2Exception('Customer group record ID was not provided or is invalid in the Request-URI');
}
$customerGroup = $this->readService->getById(new IdType($this->uri[1]));
$customerGroup->delete();
$this->_writeResponse([
'code' => 200,
'status' => 'success',
'action' => 'delete',
'customerGroupId' => $customerGroup->getId()
]);
}
/**
* Serializes customer group collections.
*
* @param \CustomerGroupCollection $customerGroupCollection Customer group collection to be serialized.
*
* @return array Serialized customer group collection array.
*/
protected function _serializeCustomerGroupCollection(CustomerGroupCollection $customerGroupCollection)
{
$data = [];
foreach($customerGroupCollection->getArray() as $customerGroup)
{
$data[] = $this->_serializeCustomerGroup($customerGroup);
}
return $data;
}
/**
* Serializes customer group entities.
*
* @param \CustomerGroupInterface $customerGroup Customer group entity to be serialized.
*
* @return array Serialized customer group array.
*/
protected function _serializeCustomerGroup(CustomerGroupInterface $customerGroup)
{
return [
'id' => $customerGroup->getId(),
'names' => $this->_serializeCustomerGroupNames($customerGroup->getNames()),
'settings' => $this->_serializeCustomerGroupSettings($customerGroup->getSettings()),
'configurations' => $this->_serializeCustomerGroupConfigurations($customerGroup->getConfigurations())
];
}
/**
* Deserialize a json string to a customer unit entity.
*
* @param string $customerGroupJson Customer group json to be serialized.
* @param int|null $id (Optional) Id of entity to be deserialize.
*
* @return \QuantityUnitInterface Deserialize customer group entity.
*/
protected function _deserializeCustomerGroup($customerGroupJson, $id = null)
{
$customerGroupData = json_decode($customerGroupJson, true);
$customerGroup = $id ? $this->readService->getById(new IntType($id)) : $this->readService->create();
foreach($customerGroupData['names'] as $languageCode => $name)
{
$customerGroup->setName(new StringType($name), new LanguageCode(new StringType($languageCode)));
}
$customerGroup->setSettings($this->_deserializeCustomerGroupSettings($customerGroupData['settings']));
$customerGroup->setConfigurations($this->_deserializeCustomerGroupConfigurations($customerGroupData['configurations']));
return $customerGroup;
}
/**
* Serializes customer group names array.
*
* @param array $customerGroupNames Customer group names array to be serialized.
*
* @return array Serialized customer group names array.
*/
protected function _serializeCustomerGroupNames(array $customerGroupNames)
{
$data = [];
foreach($customerGroupNames as $languageCode => $name)
{
$data[$languageCode] = $name;
}
return $data;
}
/**
* Serializes customer group settings value objects.
*
* @param \CustomerGroupSettingsInterface $settings Customer group settings object to be serialized.
*
* @return array Serialized customer group settings array.
*/
protected function _serializeCustomerGroupSettings(CustomerGroupSettingsInterface $settings)
{
return [
'public' => $settings->isPublic(),
'otDiscountFlag' => $settings->isOtDiscountFlag(),
'graduatedPrices' => $settings->isGraduatedPrices(),
'showPrice' => $settings->isShowPrice(),
'showPriceTax' => $settings->isShowPriceTax(),
'addTaxOt' => $settings->isAddTaxOt(),
'discountAttributes' => $settings->isDiscountAttributes(),
'fsk18' => $settings->isFsk18(),
'fsk18Display' => $settings->isFsk18Display(),
'writeReviews' => $settings->isWriteReviews(),
'readReviews' => $settings->isReadReviews()
];
}
/**
* Deserialize customer group settings.
*
* @param array $settings Customer group settings to be serialzed.
*
* @return \CustomerGroupSettingsInterface Deserialize customer group settings.
*/
protected function _deserializeCustomerGroupSettings(array $settings)
{
return $this->writeService->getFactory()
->createSettings(new BoolType($settings['public']),
new BoolType($settings['otDiscountFlag']),
new BoolType($settings['graduatedPrices']),
new BoolType($settings['showPrice']),
new BoolType($settings['showPriceTax']),
new BoolType($settings['addTaxOt']),
new BoolType($settings['discountAttributes']),
new BoolType($settings['fsk18']),
new BoolType($settings['fsk18Display']),
new BoolType($settings['writeReviews']),
new BoolType($settings['readReviews']));
}
/**
* Serializes customer group configuration value objects.
*
* @param \CustomerGroupConfigurationsInterface $configurations Customer group configuration object to be
* serialized.
*
* @return array Serialized customer group configuration array.
*/
protected function _serializeCustomerGroupConfigurations(CustomerGroupConfigurationsInterface $configurations)
{
return [
'minOrder' => $configurations->getMinOrder(),
'maxOrder' => $configurations->getMaxOrder(),
'discount' => $configurations->getDiscount(),
'otDiscount' => $configurations->getOtDiscount(),
'unallowedPaymentModules' => $configurations->getUnallowedPaymentModules(),
'unallowedShippingModules' => $configurations->getUnallowedShippingModules()
];
}
/**
* Deserialize customer group configurations.
*
* @param array $configurations Customer group configurations to be serialzed.
*
* @return \CustomerGroupConfigurationsInterface Deserialize customer group configurations.
*/
protected function _deserializeCustomerGroupConfigurations(array $configurations)
{
return $this->writeService->getFactory()
->createConfigurations(new DecimalType($configurations['discount']),
new DecimalType($configurations['otDiscount']),
$configurations['minOrder'] ? new DecimalType($configurations['minOrder']) : null,
$configurations['maxOrder'] ? new DecimalType($configurations['maxOrder']) : null,
$configurations['unallowedPaymentModules'],
$configurations['unallowedShippingModules']);
}
}