You are here

class CouponCodeGenerator in Commerce Core 8.2

Hierarchy

Expanded class hierarchy of CouponCodeGenerator

1 string reference to 'CouponCodeGenerator'
commerce_promotion.services.yml in modules/promotion/commerce_promotion.services.yml
modules/promotion/commerce_promotion.services.yml
1 service uses CouponCodeGenerator
commerce_promotion.coupon_code_generator in modules/promotion/commerce_promotion.services.yml
Drupal\commerce_promotion\CouponCodeGenerator

File

modules/promotion/src/CouponCodeGenerator.php, line 7

Namespace

Drupal\commerce_promotion
View source
class CouponCodeGenerator implements CouponCodeGeneratorInterface {

  /**
   * The database connection.
   *
   * @var \Drupal\Core\Database\Connection
   */
  protected $connection;

  /**
   * Constructs a new CouponCodeGenerator object.
   *
   * @param \Drupal\Core\Database\Connection $connection
   *   The database connection.
   */
  public function __construct(Connection $connection) {
    $this->connection = $connection;
  }

  /**
   * {@inheritdoc}
   */
  public function validatePattern(CouponCodePattern $pattern, $quantity = 1) {
    $character_set = $this
      ->getCharacterSet($pattern
      ->getType());
    $combinations = pow(count($character_set), $pattern
      ->getLength());
    return !($quantity > $combinations);
  }

  /**
   * {@inheritdoc}
   */
  public function generateCodes(CouponCodePattern $pattern, $quantity = 1) {
    $character_set = $this
      ->getCharacterSet($pattern
      ->getType());
    $character_set_size = count($character_set);
    $length = $pattern
      ->getLength();
    $prefix = $pattern
      ->getPrefix();
    $suffix = $pattern
      ->getSuffix();

    // Generate twice the requested quantity, to improve chances of having
    // the needed quantity after removing non-unique/existing codes.
    $codes = [];
    for ($i = 0; $i < $quantity * 2; $i++) {
      $code = '';
      while (strlen($code) < $length) {
        $random_index = mt_rand(0, $character_set_size - 1);
        $code .= $character_set[$random_index];
      }
      $codes[] = $prefix . $code . $suffix;
    }
    $codes = array_unique($codes);

    // Remove codes which already exist in the database.
    $result = $this->connection
      ->select('commerce_promotion_coupon', 'c')
      ->fields('c', [
      'code',
    ])
      ->condition('code', $codes, 'IN')
      ->execute();
    $existing_codes = $result
      ->fetchCol();
    $codes = array_diff($codes, $existing_codes);
    return array_slice($codes, 0, $quantity);
  }

  /**
   * Gets the character set for the given pattern type.
   *
   * @param string $pattern_type
   *   The pattern type.
   *
   * @return string[]
   *   The character set.
   */
  protected function getCharacterSet($pattern_type) {
    $characters = [];
    switch ($pattern_type) {

      // No 'I', 'O', 'i', 'l', '0', '1' to avoid recognition issues.
      case CouponCodePattern::ALPHANUMERIC:
        $characters = [
          'A',
          'B',
          'C',
          'D',
          'E',
          'F',
          'G',
          'H',
          'J',
          'K',
          'L',
          'M',
          'N',
          'P',
          'Q',
          'R',
          'S',
          'T',
          'U',
          'V',
          'W',
          'X',
          'Y',
          'Z',
          'a',
          'b',
          'c',
          'd',
          'e',
          'f',
          'g',
          'h',
          'j',
          'k',
          'm',
          'n',
          'o',
          'p',
          'q',
          'r',
          's',
          't',
          'u',
          'v',
          'w',
          'x',
          'y',
          'z',
          '2',
          '3',
          '4',
          '5',
          '6',
          '7',
          '8',
          '9',
        ];
        break;
      case CouponCodePattern::ALPHABETIC:

        // No 'I', 'i', 'l' to avoid recognition issues.
        $characters = [
          'A',
          'B',
          'C',
          'D',
          'E',
          'F',
          'G',
          'H',
          'J',
          'K',
          'L',
          'M',
          'N',
          'O',
          'P',
          'Q',
          'R',
          'S',
          'T',
          'U',
          'V',
          'W',
          'X',
          'Y',
          'Z',
          'a',
          'b',
          'c',
          'd',
          'e',
          'f',
          'g',
          'h',
          'j',
          'k',
          'm',
          'n',
          'o',
          'p',
          'q',
          'r',
          's',
          't',
          'u',
          'v',
          'w',
          'x',
          'y',
          'z',
        ];
        break;
      case CouponCodePattern::NUMERIC:
        $characters = [
          '0',
          '1',
          '2',
          '3',
          '4',
          '5',
          '6',
          '7',
          '8',
          '9',
        ];
    }
    return $characters;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CouponCodeGenerator::$connection protected property The database connection.
CouponCodeGenerator::generateCodes public function Generates coupon codes. Overrides CouponCodeGeneratorInterface::generateCodes
CouponCodeGenerator::getCharacterSet protected function Gets the character set for the given pattern type.
CouponCodeGenerator::validatePattern public function Validates the given pattern for the specified quantity. Overrides CouponCodeGeneratorInterface::validatePattern
CouponCodeGenerator::__construct public function Constructs a new CouponCodeGenerator object.