126 $userId = (int) $userId;
130 static::clearByUser($userId);
132 $randomVector = Random::getString(static::CODES_PER_USER * 8);
133 $randomVector = str_split($randomVector, 4);
134 for ($i = 0; $i < static::CODES_PER_USER; $i++)
137 'USER_ID' => $userId,
139 'CODE' => sprintf(
'%s-%s', $randomVector[$i * 2], $randomVector[($i * 2) + 1])
157 public static function useCode($userId, $searchCode)
159 $userId = (int) $userId;
163 if (!preg_match(static::CODE_PATTERN, $searchCode))
164 throw new ArgumentTypeException(
'searchCode', sprintf(
'string, check pattern "%s"', static::CODE_PATTERN));
166 $codes = static::getList(array(
167 'select' => array(
'ID',
'CODE'),
168 'filter' => array(
'=USER_ID' => $userId,
'=USED' =>
'N'),
172 while (($code = $codes->fetch()))
174 if($code[
'CODE'] === $searchCode)
176 static::update($code[
'ID'], array(