Exemple #1
0
 /**
  * Increase the length of Code if probability is low
  *
  * @return void
  */
 protected function increaseLength()
 {
     $maxProbability = $this->getMaxProbability() ? $this->getMaxProbability() : self::MAX_PROBABILITY_OF_GUESSING;
     $chars = count($this->salesRuleCoupon->getCharset($this->getFormat()));
     $size = $this->getQty();
     $length = (int) $this->getLength();
     $maxCodes = pow($chars, $length);
     $probability = $size / $maxCodes;
     if ($probability > $maxProbability) {
         do {
             $length++;
             $maxCodes = pow($chars, $length);
             $probability = $size / $maxCodes;
         } while ($probability > $maxProbability);
         $this->setLength($length);
     }
 }
 /**
  * Generate Coupons Pool
  *
  * @throws \Magento\Framework\Model\Exception
  * @return $this
  */
 public function generatePool()
 {
     $this->_generatedCount = 0;
     $size = $this->getQty();
     $maxProbability = $this->getMaxProbability() ? $this->getMaxProbability() : self::MAX_PROBABILITY_OF_GUESSING;
     $maxAttempts = $this->getMaxAttempts() ? $this->getMaxAttempts() : self::MAX_GENERATE_ATTEMPTS;
     /** @var $coupon \Magento\SalesRule\Model\Coupon */
     $coupon = $this->_couponFactory->create();
     $chars = count($this->_salesRuleCoupon->getCharset($this->getFormat()));
     $length = (int) $this->getLength();
     $maxCodes = pow($chars, $length);
     $probability = $size / $maxCodes;
     //increase the length of Code if probability is low
     if ($probability > $maxProbability) {
         do {
             $length++;
             $maxCodes = pow($chars, $length);
             $probability = $size / $maxCodes;
         } while ($probability > $maxProbability);
         $this->setLength($length);
     }
     $now = $this->dateTime->formatDate($this->_date->gmtTimestamp());
     for ($i = 0; $i < $size; $i++) {
         $attempt = 0;
         do {
             if ($attempt >= $maxAttempts) {
                 throw new \Magento\Framework\Model\Exception(__('We cannot create the requested Coupon Qty. Please check your settings and try again.'));
             }
             $code = $this->generateCode();
             $attempt++;
         } while ($this->getResource()->exists($code));
         $expirationDate = $this->getToDate();
         if ($expirationDate instanceof \Zend_Date) {
             $expirationDate = $expirationDate->toString(\Magento\Framework\Stdlib\DateTime::DATETIME_INTERNAL_FORMAT);
         }
         $coupon->setId(null)->setRuleId($this->getRuleId())->setUsageLimit($this->getUsesPerCoupon())->setUsagePerCustomer($this->getUsesPerCustomer())->setExpirationDate($expirationDate)->setCreatedAt($now)->setType(\Magento\SalesRule\Helper\Coupon::COUPON_TYPE_SPECIFIC_AUTOGENERATED)->setCode($code)->save();
         $this->_generatedCount++;
     }
     return $this;
 }
Exemple #3
0
 public function testGetCharset()
 {
     $format = 'format';
     $expected = ['a', 'b', 'c'];
     $this->assertEquals($expected, $this->helper->getCharset($format));
 }