smsplatform-1.0.x-dev/src/EventSubscriber/SmsEntityPhoneNumberProcessor.php

src/EventSubscriber/SmsEntityPhoneNumberProcessor.php
<?php

declare(strict_types=1);

namespace Drupal\smsplatform\EventSubscriber;

use Drupal\smsplatform\Event\SmsEntityPhoneNumber;
use Drupal\smsplatform\Event\SmsEvents;
use Drupal\smsplatform\Exception\PhoneNumberSettingsException;
use Drupal\smsplatform\Provider\PhoneNumberVerificationInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

/**
 * Resolve phone numbers for an entity using phone verification system.
 */
class SmsEntityPhoneNumberProcessor implements EventSubscriberInterface {

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

  /**
   * Constructs a new SmsEntityPhoneNumberProcessor object.
   *
   * @param \Drupal\smsplatform\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\smsplatform\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;
  }

}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc