Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Massgenerator.php
Go to the documentation of this file.
1 <?php
7 
14  \Magento\SalesRule\Model\Coupon\CodegeneratorInterface
15 {
20 
25 
30  protected $generatedCount = 0;
31 
35  protected $generatedCodes = [];
36 
42  protected $salesRuleCoupon;
43 
47  protected $date;
48 
52  protected $couponFactory;
53 
57  protected $dateTime;
58 
70  public function __construct(
71  \Magento\Framework\Model\Context $context,
72  \Magento\Framework\Registry $registry,
73  \Magento\SalesRule\Helper\Coupon $salesRuleCoupon,
74  \Magento\SalesRule\Model\CouponFactory $couponFactory,
75  \Magento\Framework\Stdlib\DateTime\DateTime $date,
76  \Magento\Framework\Stdlib\DateTime $dateTime,
77  \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
78  \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
79  array $data = []
80  ) {
81  $this->salesRuleCoupon = $salesRuleCoupon;
82  $this->date = $date;
83  $this->couponFactory = $couponFactory;
84  $this->dateTime = $dateTime;
85  parent::__construct($context, $registry, $resource, $resourceCollection, $data);
86  }
87 
93  protected function _construct()
94  {
95  $this->_init(\Magento\SalesRule\Model\ResourceModel\Coupon::class);
96  }
97 
103  public function generateCode()
104  {
105  $format = $this->getFormat();
106  if (empty($format)) {
108  }
109 
110  $splitChar = $this->getDelimiter();
111  $charset = $this->salesRuleCoupon->getCharset($format);
112 
113  $code = '';
114  $charsetSize = count($charset);
115  $split = max(0, (int)$this->getDash());
116  $length = max(1, (int)$this->getLength());
117  for ($i = 0; $i < $length; ++$i) {
118  $char = $charset[\Magento\Framework\Math\Random::getRandomNumber(0, $charsetSize - 1)];
119  if (($split > 0) && (($i % $split) === 0) && ($i !== 0)) {
120  $char = $splitChar . $char;
121  }
122  $code .= $char;
123  }
124 
125  return $this->getPrefix() . $code . $this->getSuffix();
126  }
127 
133  public function getDelimiter()
134  {
135  if ($this->hasData('delimiter')) {
136  return $this->getData('delimiter');
137  } else {
138  return $this->salesRuleCoupon->getCodeSeparator();
139  }
140  }
141 
148  public function generatePool()
149  {
150  $this->generatedCount = 0;
151  $this->generatedCodes = [];
152  $size = $this->getQty();
153  $maxAttempts = $this->getMaxAttempts() ? $this->getMaxAttempts() : self::MAX_GENERATE_ATTEMPTS;
154  $this->increaseLength();
156  $coupon = $this->couponFactory->create();
157  $nowTimestamp = $this->dateTime->formatDate($this->date->gmtTimestamp());
158 
159  for ($i = 0; $i < $size; $i++) {
160  $attempt = 0;
161  do {
162  if ($attempt >= $maxAttempts) {
163  throw new \Magento\Framework\Exception\LocalizedException(
164  __('We cannot create the requested Coupon Qty. Please check your settings and try again.')
165  );
166  }
167  $code = $this->generateCode();
168  ++$attempt;
169  } while ($this->getResource()->exists($code));
170 
171  $expirationDate = $this->getToDate();
172  if ($expirationDate instanceof \DateTimeInterface) {
173  $expirationDate = $expirationDate->format('Y-m-d H:i:s');
174  }
175 
176  $coupon->setId(null)
177  ->setRuleId($this->getRuleId())
178  ->setUsageLimit($this->getUsesPerCoupon())
179  ->setUsagePerCustomer($this->getUsagePerCustomer())
180  ->setExpirationDate($expirationDate)
181  ->setCreatedAt($nowTimestamp)
182  ->setType(\Magento\SalesRule\Helper\Coupon::COUPON_TYPE_SPECIFIC_AUTOGENERATED)
183  ->setCode($code)
184  ->save();
185 
186  $this->generatedCount += 1;
187  $this->generatedCodes[] = $code;
188  }
189 
190  return $this;
191  }
192 
198  protected function increaseLength()
199  {
200  $maxProbability = $this->getMaxProbability() ? $this->getMaxProbability() : self::MAX_PROBABILITY_OF_GUESSING;
201  $chars = count($this->salesRuleCoupon->getCharset($this->getFormat()));
202  $size = $this->getQty();
203  $length = (int)$this->getLength();
204  $maxCodes = pow($chars, $length);
205  $probability = $size / $maxCodes;
206 
207  if ($probability > $maxProbability) {
208  do {
209  $length++;
210  $maxCodes = pow($chars, $length);
211  $probability = $size / $maxCodes;
212  } while ($probability > $maxProbability);
213  $this->setLength($length);
214  }
215  }
216 
223  public function validateData($data)
224  {
225  return !empty($data)
226  && !empty($data['qty'])
227  && !empty($data['rule_id'])
228  && !empty($data['length'])
229  && !empty($data['format'])
230  && (int)$data['qty'] > 0
231  && (int)$data['rule_id'] > 0
232  && (int)$data['length'] > 0;
233  }
234 
240  public function getGeneratedCodes()
241  {
242  return $this->generatedCodes;
243  }
244 
250  public function getGeneratedCount()
251  {
252  return $this->generatedCount;
253  }
254 }
static getRandomNumber($min=0, $max=null)
Definition: Random.php:62
getData($key='', $index=null)
Definition: DataObject.php:119
$coupon
__()
Definition: __.php:13
$resource
Definition: bulk.php:12
$format
Definition: list.phtml:12
__construct(\Magento\Framework\Model\Context $context, \Magento\Framework\Registry $registry, \Magento\SalesRule\Helper\Coupon $salesRuleCoupon, \Magento\SalesRule\Model\CouponFactory $couponFactory, \Magento\Framework\Stdlib\DateTime\DateTime $date, \Magento\Framework\Stdlib\DateTime $dateTime, \Magento\Framework\Model\ResourceModel\AbstractResource $resource=null, \Magento\Framework\Data\Collection\AbstractDb $resourceCollection=null, array $data=[])
$i
Definition: gallery.phtml:31
$code
Definition: info.phtml:12