You are here

class DefaultSmsProvider in SMS Framework 8

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

The SMS provider that provides default messaging functionality.

Hierarchy

Expanded class hierarchy of DefaultSmsProvider

1 string reference to 'DefaultSmsProvider'
sms.services.yml in ./sms.services.yml
sms.services.yml
1 service uses DefaultSmsProvider
sms.provider.default in ./sms.services.yml
Drupal\sms\Provider\DefaultSmsProvider

File

src/Provider/DefaultSmsProvider.php, line 23

Namespace

Drupal\sms\Provider
View source
class DefaultSmsProvider implements SmsProviderInterface {

  /**
   * The event dispatcher.
   *
   * @var \Symfony\Component\EventDispatcher\EventDispatcherInterface
   */
  protected $eventDispatcher;

  /**
   * Creates a new instance of the default SMS provider.
   *
   * @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $event_dispatcher
   *   The event dispatcher.
   */
  public function __construct(EventDispatcherInterface $event_dispatcher) {
    $this->eventDispatcher = $event_dispatcher;
  }

  /**
   * {@inheritdoc}
   */
  public function queue(SmsMessageInterface $sms_message) {
    if (!$sms_message
      ->getDirection()) {
      throw new SmsDirectionException('Missing direction for message.');
    }
    $sms_messages = $this
      ->dispatchEvent(SmsEvents::MESSAGE_PRE_PROCESS, [
      $sms_message,
    ])
      ->getMessages();
    $sms_messages = $this
      ->dispatchEvent(SmsEvents::MESSAGE_QUEUE_PRE_PROCESS, $sms_messages)
      ->getMessages();
    foreach ($sms_messages as $gateway_id => &$sms_message) {

      // Tag so SmsEvents::MESSAGE_PRE_PROCESS is not dispatched again.
      $sms_message
        ->setOption('_skip_preprocess_event', TRUE);

      // Validate SMS message entities.
      if ($sms_message instanceof SmsMessageEntityInterface) {
        $errors = [];
        $violations = $sms_message
          ->validate();
        foreach ($violations
          ->getFieldNames() as $field_name) {
          foreach ($violations
            ->getByField($field_name) as $violation) {
            $errors[] = "[{$field_name}]: " . strip_tags((string) $violation
              ->getMessage());
          }
        }
        if ($errors) {
          throw new SmsException(sprintf('Can not queue SMS message because there are %s validation error(s): %s', count($errors), implode(' ', $errors)));
        }
      }
      if ($sms_message
        ->getGateway()
        ->getSkipQueue()) {
        switch ($sms_message
          ->getDirection()) {
          case Direction::INCOMING:
            $this
              ->incoming($sms_message);
            break;
          case Direction::OUTGOING:
            $this
              ->send($sms_message);
            break;
        }
        continue;
      }
      $sms_message = SmsMessage::convertFromSmsMessage($sms_message);
      $sms_message
        ->save();
    }

    // Queue has different post-process events because there is no result.
    return $this
      ->dispatchEvent(SmsEvents::MESSAGE_QUEUE_POST_PROCESS, $sms_messages)
      ->getMessages();
  }

  /**
   * {@inheritdoc}
   */
  public function send(SmsMessageInterface $sms) {
    $sms
      ->setDirection(Direction::OUTGOING);
    $dispatch = !$sms
      ->getOption('_skip_preprocess_event');
    $sms_messages = $dispatch ? $this
      ->dispatchEvent(SmsEvents::MESSAGE_PRE_PROCESS, [
      $sms,
    ])
      ->getMessages() : [
      $sms,
    ];
    $sms_messages = $this
      ->dispatchEvent(SmsEvents::MESSAGE_OUTGOING_PRE_PROCESS, $sms_messages)
      ->getMessages();

    // Iterate over messages individually since pre-process can modify the
    // gateway used.
    foreach ($sms_messages as &$sms_message) {
      $plugin = $sms_message
        ->getGateway()
        ->getPlugin();
      $result = $plugin
        ->send($sms_message);
      $sms_message
        ->setResult($result);
      $this
        ->dispatchEvent(SmsEvents::MESSAGE_OUTGOING_POST_PROCESS, [
        $sms_message,
      ]);
      $this
        ->dispatchEvent(SmsEvents::MESSAGE_POST_PROCESS, [
        $sms_message,
      ]);
    }
    return $sms_messages;
  }

  /**
   * {@inheritdoc}
   */
  public function incoming(SmsMessageInterface $sms_message) {
    $sms_message
      ->setDirection(Direction::INCOMING);

    // Do not iterate over messages individually like outgoing, changing gateway
    // in pre-process events do not apply to incoming.
    $plugin = $sms_message
      ->getGateway()
      ->getPlugin();
    $dispatch = !$sms_message
      ->getOption('_skip_preprocess_event');
    $sms_messages = $dispatch ? $this
      ->dispatchEvent(SmsEvents::MESSAGE_PRE_PROCESS, [
      $sms_message,
    ])
      ->getMessages() : [
      $sms_message,
    ];
    $sms_messages = $this
      ->dispatchEvent(SmsEvents::MESSAGE_INCOMING_PRE_PROCESS, $sms_messages)
      ->getMessages();
    if ($plugin instanceof SmsIncomingEventProcessorInterface) {
      $event = new SmsMessageEvent($sms_messages);
      $plugin
        ->incomingEvent($event);
    }
    $this
      ->dispatchEvent(SmsEvents::MESSAGE_INCOMING_POST_PROCESS, $sms_messages);
    $this
      ->dispatchEvent(SmsEvents::MESSAGE_POST_PROCESS, $sms_messages);
    return $sms_messages;
  }

  /**
   * {@inheritdoc}
   */
  public function processDeliveryReport(Request $request, SmsGatewayInterface $sms_gateway) {
    $response = new Response();
    $reports = $sms_gateway
      ->getPlugin()
      ->parseDeliveryReports($request, $response);
    $event = new SmsDeliveryReportEvent();
    $event
      ->setResponse($response)
      ->setReports($reports);
    $this->eventDispatcher
      ->dispatch(SmsEvents::DELIVERY_REPORT_POST_PROCESS, $event);
    return $event
      ->getResponse();
  }

  /**
   * Dispatch an SmsMessageEvent event for messages.
   *
   * @param string $event_name
   *   The event to trigger.
   * @param \Drupal\sms\Message\SmsMessageInterface[] $sms_messages
   *   The messages to dispatch.
   *
   * @return \Drupal\sms\Event\SmsMessageEvent
   *   The dispatched event.
   */
  protected function dispatchEvent($event_name, array $sms_messages) {
    $event = new SmsMessageEvent($sms_messages);
    return $this->eventDispatcher
      ->dispatch($event_name, $event);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DefaultSmsProvider::$eventDispatcher protected property The event dispatcher.
DefaultSmsProvider::dispatchEvent protected function Dispatch an SmsMessageEvent event for messages.
DefaultSmsProvider::incoming public function Handles a message sent from the gateway to the site. Overrides SmsProviderInterface::incoming
DefaultSmsProvider::processDeliveryReport public function Handles delivery reports pushed to the site. Overrides SmsProviderInterface::processDeliveryReport
DefaultSmsProvider::queue public function Queue a SMS message for sending or receiving. Overrides SmsProviderInterface::queue
DefaultSmsProvider::send public function Sends an SMS using the active gateway. Overrides SmsProviderInterface::send
DefaultSmsProvider::__construct public function Creates a new instance of the default SMS provider.