You are here

public function SmsEntityPhoneNumberProcessor::entityPhoneNumbers in SMS Framework 8

Same name and namespace in other branches
  1. 2.x src/EventSubscriber/SmsEntityPhoneNumberProcessor.php \Drupal\sms\EventSubscriber\SmsEntityPhoneNumberProcessor::entityPhoneNumbers()
  2. 2.1.x src/EventSubscriber/SmsEntityPhoneNumberProcessor.php \Drupal\sms\EventSubscriber\SmsEntityPhoneNumberProcessor::entityPhoneNumbers()

Get phone numbers for an entity using phone verification system.

Parameters

\Drupal\sms\Event\SmsEntityPhoneNumber $event: The entity phone number event.

File

src/EventSubscriber/SmsEntityPhoneNumberProcessor.php, line 39

Class

SmsEntityPhoneNumberProcessor
Resolve phone numbers for an entity using phone verification system.

Namespace

Drupal\sms\EventSubscriber

Code

public function entityPhoneNumbers(SmsEntityPhoneNumber $event) {
  $entity = $event
    ->getEntity();
  try {
    $phone_number_settings = $this->phoneNumberVerification
      ->getPhoneNumberSettingsForEntity($entity);
    $field_name = $phone_number_settings
      ->getFieldName('phone_number');
    if (!$field_name) {
      return;
    }
  } catch (PhoneNumberSettingsException $e) {
    return;
  }
  $phone_numbers = [];
  if (isset($entity->{$field_name})) {
    foreach ($entity->{$field_name} as $item) {
      $phone_numbers[] = $item->value;
    }
  }
  $verified = $event
    ->getRequiresVerification();
  foreach ($phone_numbers as $phone_number) {
    if (!isset($verified)) {
      $event
        ->addPhoneNumber($phone_number);
    }
    else {
      $verification = $this->phoneNumberVerification
        ->getPhoneVerificationByEntity($entity, $phone_number);
      if ($verification && $verification
        ->getStatus() == $verified) {
        $event
          ->addPhoneNumber($phone_number);
      }
    }
  }
}