You are here

public function MobileNumberUtil::verifyCode in Mobile Number 2.0.x

Same name and namespace in other branches
  1. 8 src/MobileNumberUtil.php \Drupal\mobile_number\MobileNumberUtil::verifyCode()

Verifies input code matches code sent to user.

Parameters

\libphonenumber\PhoneNumber $mobile_number: Phone number object.

string $code: Input code.

string|null $token: Verification token, if verification code was not sent in this session.

Return value

bool TRUE if matches

Overrides MobileNumberUtilInterface::verifyCode

File

src/MobileNumberUtil.php, line 308

Class

MobileNumberUtil
Turns a render array into a HTML string.

Namespace

Drupal\mobile_number

Code

public function verifyCode(PhoneNumber $mobile_number, $code, $token = NULL) {
  $token = $token ? $token : $this
    ->getToken($mobile_number);
  if ($code && $token) {
    $hash = $this
      ->codeHash($mobile_number, $token, $code);
    $query = \Drupal::database()
      ->select('mobile_number_verification', 'm');
    $query
      ->fields('m', [
      'token',
    ])
      ->condition('token', $token)
      ->condition('timestamp', time() - 60 * 60 * 24, '>')
      ->condition('verification_code', $hash);
    $result = $query
      ->execute()
      ->fetchAssoc();
    if ($result) {
      $_SESSION['mobile_number_verification'][$this
        ->getCallableNumber($mobile_number)]['verified'] = TRUE;
      return TRUE;
    }
    $this->flood
      ->register('mobile_number_verification', $this::VERIFY_ATTEMPTS_INTERVAL, $this
      ->getCallableNumber($mobile_number));
    return FALSE;
  }
  return FALSE;
}