Esempio n. 1
0
 /**
  * @param int $length
  *
  * @throws \Spryker\Zed\Discount\Business\Exception\VoucherEngineException
  *
  * @return string
  */
 protected function getRandomVoucherCode($length)
 {
     $allowedCharacters = $this->discountConfig->getVoucherCodeCharacters();
     if (!$allowedCharacters) {
         throw new VoucherEngineException('Configuration for voucher code characters missing.');
     }
     $consonants = $allowedCharacters[DiscountConfig::KEY_VOUCHER_CODE_CONSONANTS];
     $vowels = $allowedCharacters[DiscountConfig::KEY_VOUCHER_CODE_VOWELS];
     $numbers = $allowedCharacters[DiscountConfig::KEY_VOUCHER_CODE_NUMBERS];
     $code = '';
     while (strlen($code) < $length) {
         if (count($consonants)) {
             $code .= $consonants[random_int(0, count($consonants) - 1)];
         }
         if (count($vowels)) {
             $code .= $vowels[random_int(0, count($vowels) - 1)];
         }
         if (count($numbers)) {
             $code .= $numbers[random_int(0, count($numbers) - 1)];
         }
     }
     return substr($code, 0, $length);
 }