You are here

public function RequestVerificationCodeResource::get in Mobile Number 2.0.x

Same name and namespace in other branches
  1. 8 src/Plugin/rest/resource/RequestVerificationCodeResource.php \Drupal\mobile_number\Plugin\rest\resource\RequestVerificationCodeResource::get()

Responds send verification code POST request.

Parameters

string|null $number: Callable mobile number.

Return value

\Drupal\rest\ResourceResponse The HTTP response object.

Throws

\Symfony\Component\HttpKernel\Exception\HttpException

\Symfony\Component\HttpKernel\Exception\BadRequestHttpException

MobileNumberException

File

src/Plugin/rest/resource/RequestVerificationCodeResource.php, line 103

Class

RequestVerificationCodeResource
Request verification code resource.

Namespace

Drupal\mobile_number\Plugin\rest\resource

Code

public function get($number = NULL) {
  if (!$number) {
    throw new BadRequestHttpException('Mobile number not provided.');
  }
  $number = "+{$number}";
  $mobile_number = $this->util
    ->testMobileNumber($number);
  if (!$this->util
    ->checkFlood($mobile_number)) {
    throw new AccessDeniedHttpException('Too many verification attempts, please try again in a few hours.');
  }
  if (!$this->util
    ->checkFlood($mobile_number, 'sms')) {
    throw new AccessDeniedHttpException('Too many verification code requests, please try again shortly..');
  }
  $message = MobileNumberUtilInterface::MOBILE_NUMBER_DEFAULT_SMS_MESSAGE;
  $code = $this->util
    ->generateVerificationCode();
  $token = $this->util
    ->sendVerification($mobile_number, $message, $code);
  if (!$token) {
    throw new HttpException(500, 'An error occurred while sending sms.');
  }
  $response = new Response(json_encode([
    'verification_token' => $token,
  ]));
  return $response;
}