You are here

class SmsEntityPhoneNumberProcessor in SMS Framework 8

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

Resolve phone numbers for an entity using phone verification system.

Hierarchy

Expanded class hierarchy of SmsEntityPhoneNumberProcessor

1 string reference to 'SmsEntityPhoneNumberProcessor'
sms.services.yml in ./sms.services.yml
sms.services.yml
1 service uses SmsEntityPhoneNumberProcessor
sms.entity_phone_number in ./sms.services.yml
Drupal\sms\EventSubscriber\SmsEntityPhoneNumberProcessor

File

src/EventSubscriber/SmsEntityPhoneNumberProcessor.php, line 14

Namespace

Drupal\sms\EventSubscriber
View source
class SmsEntityPhoneNumberProcessor implements EventSubscriberInterface {

  /**
   * The phone number verification service.
   *
   * @var \Drupal\sms\Provider\PhoneNumberVerificationInterface
   */
  protected $phoneNumberVerification;

  /**
   * Constructs a new SmsEntityPhoneNumberProcessor object.
   *
   * @param \Drupal\sms\Provider\PhoneNumberVerificationInterface $phone_number_verification
   *   The phone number verification service.
   */
  public function __construct(PhoneNumberVerificationInterface $phone_number_verification) {
    $this->phoneNumberVerification = $phone_number_verification;
  }

  /**
   * Get phone numbers for an entity using phone verification system.
   *
   * @param \Drupal\sms\Event\SmsEntityPhoneNumber $event
   *   The entity phone number event.
   */
  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);
        }
      }
    }
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    $events[SmsEvents::ENTITY_PHONE_NUMBERS][] = [
      'entityPhoneNumbers',
      1024,
    ];
    return $events;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
SmsEntityPhoneNumberProcessor::$phoneNumberVerification protected property The phone number verification service.
SmsEntityPhoneNumberProcessor::entityPhoneNumbers public function Get phone numbers for an entity using phone verification system.
SmsEntityPhoneNumberProcessor::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to.
SmsEntityPhoneNumberProcessor::__construct public function Constructs a new SmsEntityPhoneNumberProcessor object.