You are here

public function PhoneNumberVerification::newPhoneVerification in SMS Framework 8

Same name and namespace in other branches
  1. 2.x src/Provider/PhoneNumberVerification.php \Drupal\sms\Provider\PhoneNumberVerification::newPhoneVerification()
  2. 2.1.x src/Provider/PhoneNumberVerification.php \Drupal\sms\Provider\PhoneNumberVerification::newPhoneVerification()

Generates a phone number verification for an entity and phone number pair.

The phone number verification is immediately saved to storage, and an SMS is sent to the phone number for verification.

Parameters

\Drupal\Core\Entity\EntityInterface $entity: An entity to get phone number verification.

string $phone_number: A phone number.

Return value

\Drupal\sms\Entity\PhoneNumberVerificationInterface|null A phone number verification.

Overrides PhoneNumberVerificationInterface::newPhoneVerification

1 call to PhoneNumberVerification::newPhoneVerification()
PhoneNumberVerification::updatePhoneVerificationByEntity in src/Provider/PhoneNumberVerification.php
Detect modifications to phone numbers on an entity.

File

src/Provider/PhoneNumberVerification.php, line 138

Class

PhoneNumberVerification
Phone number verification provider.

Namespace

Drupal\sms\Provider

Code

public function newPhoneVerification(EntityInterface $entity, $phone_number) {
  $config = $this
    ->getPhoneNumberSettingsForEntity($entity);
  $message = $config
    ->getVerificationMessage() ?: '';

  // @todo Replace with code generator.
  $random = new Random();
  $code = strtoupper($random
    ->name(6));

  /** @var \Drupal\sms\Entity\PhoneNumberVerificationInterface $phone_verification */
  $phone_verification = $this->phoneNumberVerificationStorage
    ->create();
  $phone_verification
    ->setCode($code)
    ->setStatus(FALSE)
    ->setPhoneNumber($phone_number)
    ->setEntity($entity)
    ->save();
  if ($phone_verification) {
    $sms_message = new SmsMessage();
    $sms_message
      ->addRecipient($phone_number)
      ->setOption('_is_verification_message', TRUE)
      ->setMessage($message)
      ->setDirection(Direction::OUTGOING);
    $data['sms-message'] = $sms_message;
    $data['sms_verification_code'] = $phone_verification
      ->getCode();
    $sms_message
      ->setMessage($this->token
      ->replace($message, $data))
      ->setAutomated(FALSE);
    $this->smsProvider
      ->queue($sms_message);
  }
  return $phone_verification;
}