Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CouponGenerator.php
Go to the documentation of this file.
1 <?php
7 
17 {
28  private $keyMap = [
29  'quantity' => 'qty'
30  ];
31 
35  private $couponManagementService;
36 
40  private $generationSpecFactory;
41 
49  public function __construct(
50  \Magento\SalesRule\Model\Service\CouponManagementService $couponManagementService,
51  \Magento\SalesRule\Api\Data\CouponGenerationSpecInterfaceFactory $generationSpecFactory
52  ) {
53  $this->couponManagementService = $couponManagementService;
54  $this->generationSpecFactory = $generationSpecFactory;
55  }
56 
68  public function generateCodes(array $parameters)
69  {
70  $couponSpecData = $this->convertCouponSpecData($parameters);
71  $couponSpec = $this->generationSpecFactory->create(['data' => $couponSpecData]);
72  return $this->couponManagementService->generate($couponSpec);
73  }
74 
82  private function convertCouponSpecData(array $data)
83  {
84  foreach ($this->keyMap as $mapKey => $mapValue) {
85  $data[$mapKey] = isset($data[$mapValue]) ? $data[$mapValue] : null;
86  }
87 
88  return $data;
89  }
90 }
__construct(\Magento\SalesRule\Model\Service\CouponManagementService $couponManagementService, \Magento\SalesRule\Api\Data\CouponGenerationSpecInterfaceFactory $generationSpecFactory)