CouponGenerator.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\SalesRule\Model;
  7. /**
  8. * Allows to generate a pool of coupon codes.
  9. *
  10. * Generated coupon code - auto generated string, which is used on checkout in order to get
  11. * discount (fixed or in percents) on whole customer shopping cart or on items in this shopping cart.
  12. * Class was added due to Backward Compatibility and is used as proxy to:
  13. * @see \Magento\SalesRule\Model\Service\CouponManagementService
  14. */
  15. class CouponGenerator
  16. {
  17. /**
  18. * Map keys in old and new services
  19. *
  20. * Controller was used as old service
  21. * @see \Magento\SalesRule\Controller\Adminhtml\Promo\Quote\Generate
  22. * - key = key in new service
  23. * - value = key in old service
  24. *
  25. * @var array
  26. */
  27. private $keyMap = [
  28. 'quantity' => 'qty'
  29. ];
  30. /**
  31. * @var Service\CouponManagementService
  32. */
  33. private $couponManagementService;
  34. /**
  35. * @var \Magento\SalesRule\Api\Data\CouponGenerationSpecInterfaceFactory
  36. */
  37. private $generationSpecFactory;
  38. /**
  39. * All objects should be injected through constructor, because we need to have working service already
  40. * after it initializing
  41. *
  42. * @param Service\CouponManagementService $couponManagementService
  43. * @param \Magento\SalesRule\Api\Data\CouponGenerationSpecInterfaceFactory $generationSpecFactory
  44. */
  45. public function __construct(
  46. \Magento\SalesRule\Model\Service\CouponManagementService $couponManagementService,
  47. \Magento\SalesRule\Api\Data\CouponGenerationSpecInterfaceFactory $generationSpecFactory
  48. ) {
  49. $this->couponManagementService = $couponManagementService;
  50. $this->generationSpecFactory = $generationSpecFactory;
  51. }
  52. /**
  53. * Generate a pool of generated coupon codes
  54. *
  55. * This method is used as proxy, due to high coupling in constructor
  56. * @see \Magento\SalesRule\Controller\Adminhtml\Promo\Quote\Generate
  57. * In order to generate valid coupon codes, we need to initialize DTO object and run service.
  58. * @see \Magento\SalesRule\Api\Data\CouponGenerationSpecInterface -> DTO object
  59. *
  60. * @param array $parameters
  61. * @return string[]
  62. */
  63. public function generateCodes(array $parameters)
  64. {
  65. $couponSpecData = $this->convertCouponSpecData($parameters);
  66. $couponSpec = $this->generationSpecFactory->create(['data' => $couponSpecData]);
  67. return $this->couponManagementService->generate($couponSpec);
  68. }
  69. /**
  70. * We should map old values to new one
  71. * We need to do this, as new service with another key names was added
  72. *
  73. * @param array $data
  74. * @return array
  75. */
  76. private function convertCouponSpecData(array $data)
  77. {
  78. foreach ($this->keyMap as $mapKey => $mapValue) {
  79. $data[$mapKey] = isset($data[$mapValue]) ? $data[$mapValue] : null;
  80. }
  81. return $data;
  82. }
  83. }