You are here

public function PhoneNumberVerification::updatePhoneVerificationByEntity in SMS Framework 8

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

Detect modifications to phone numbers on an entity.

Detects if phone numbers are modified on an entity. If new phone numbers are detected, new phone number verifications are created. If phone numbers are removed, the associated phone number verification is deleted.

Parameters

\Drupal\Core\Entity\EntityInterface $entity: Update phone number verifications for this entity.

Overrides PhoneNumberVerificationInterface::updatePhoneVerificationByEntity

File

src/Provider/PhoneNumberVerification.php, line 180

Class

PhoneNumberVerification
Phone number verification provider.

Namespace

Drupal\sms\Provider

Code

public function updatePhoneVerificationByEntity(EntityInterface $entity) {
  try {
    $phone_number_settings = $this
      ->getPhoneNumberSettingsForEntity($entity);
    $field_name = $phone_number_settings
      ->getFieldName('phone_number');
    if (!empty($field_name)) {
      $items_original = isset($entity->original) ? $entity->original->{$field_name} : NULL;
      $items = $entity->{$field_name};
    }
  } catch (PhoneNumberSettingsException $e) {

    // Missing phone number configuration for this entity.
  }

  // $items can be unassigned because field_name is not configured, or is NULL
  // because there is no items.
  if (!isset($items)) {
    return;
  }
  $numbers = [];
  assert($items instanceof FieldItemListInterface);
  foreach ($items as $item) {
    $phone_number = $item->value;
    $numbers[] = $phone_number;
    if (!$this
      ->getPhoneVerificationByEntity($entity, $phone_number)) {
      $this
        ->newPhoneVerification($entity, $phone_number);
    }
  }
  if (isset($items_original) && !$items
    ->equals($items_original)) {
    foreach ($items_original as $item) {
      $phone_number = $item->value;

      // A phone number was deleted.
      if (!in_array($phone_number, $numbers)) {
        if ($phone_verification = $this
          ->getPhoneVerificationByEntity($entity, $phone_number)) {
          $phone_verification
            ->delete();
        }
      }
    }
  }
}