You are here

class RecipientGatewayEvent in SMS Framework 8

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

Event fired to determine valid gateways for a recipient.

Hierarchy

Expanded class hierarchy of RecipientGatewayEvent

3 files declare their use of RecipientGatewayEvent
RecipientGatewayEventTest.php in tests/src/Unit/Event/RecipientGatewayEventTest.php
SmsMessageProcessor.php in src/EventSubscriber/SmsMessageProcessor.php
SmsTestEventSubscriber.php in tests/modules/sms_test/src/EventSubscriber/SmsTestEventSubscriber.php

File

src/Event/RecipientGatewayEvent.php, line 11

Namespace

Drupal\sms\Event
View source
class RecipientGatewayEvent extends Event {

  /**
   * The recipient phone number.
   *
   * @var string
   */
  protected $recipient;

  /**
   * An array of gateway doubles.
   *
   * @var array
   *   The array of gateway/priority doubles where:
   *     - Key 0: SmsGatewayInterface $gateway
   *     - Key 1: int $priority
   */
  protected $gateways = [];

  /**
   * Constructs the object.
   *
   * @param string $recipient
   *   The recipient phone number.
   */
  public function __construct($recipient) {
    $this
      ->setRecipient($recipient);
  }

  /**
   * Get the phone number for this event.
   *
   * @return string
   *   The phone number for this event.
   */
  public function getRecipient() {
    return $this->recipient;
  }

  /**
   * Set the phone number for this event.
   *
   * @param string $recipient
   *   The phone number for this event.
   *
   * @return $this
   *   Return this event for chaining.
   */
  public function setRecipient($recipient) {
    $this->recipient = $recipient;
    return $this;
  }

  /**
   * Get the gateways for this event.
   *
   * @return array
   *   An array of doubles gateway/priority doubles.
   */
  public function getGateways() {
    return $this->gateways;
  }

  /**
   * Return gateways ordered by priority from highest to lowest.
   *
   * @return \Drupal\sms\Entity\SmsGatewayInterface[]
   *   An array of gateways ordered by priority from highest to lowest.
   */
  public function getGatewaysSorted() {
    $sorted = $this->gateways;
    uasort($sorted, function ($a, $b) {
      list(, $priority_a) = $a;
      list(, $priority_b) = $b;
      if ($priority_a == $priority_b) {
        return 0;
      }
      return $priority_a > $priority_b ? -1 : 1;
    });

    // Return the gateway object instead of tuples.
    $gateways = [];
    foreach ($sorted as $tuple) {
      list($gateway, ) = $tuple;
      $gateways[] = $gateway;
    }
    return $gateways;
  }

  /**
   * Add a gateway for the recipient on this event.
   *
   * @param \Drupal\sms\Entity\SmsGatewayInterface $gateway
   *   The gateway for the recipient.
   * @param int $priority
   *   The priority for this gateway.
   *
   * @return $this
   *   Return this event for chaining.
   */
  public function addGateway(SmsGatewayInterface $gateway, $priority = 0) {
    $this->gateways[] = [
      $gateway,
      $priority,
    ];
    return $this;
  }

  /**
   * Remove a gateway from this event.
   *
   * @param string $gateway_id
   *   A gateway plugin ID.
   * @param int|null $priority
   *   The priority of the gateway to remove, or NULL to remove all gateways
   *   with the identifier.
   *
   * @return $this
   *   Return this event for chaining.
   */
  public function removeGateway($gateway_id, $priority = NULL) {
    foreach ($this->gateways as $k => $tuple) {
      list($gateway, $gateway_priority) = $tuple;
      if ($gateway_id == $gateway
        ->id()) {
        if (!isset($priority) || $priority == $gateway_priority) {
          unset($this->gateways[$k]);
        }
      }
    }
    return $this;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
RecipientGatewayEvent::$gateways protected property An array of gateway doubles.
RecipientGatewayEvent::$recipient protected property The recipient phone number.
RecipientGatewayEvent::addGateway public function Add a gateway for the recipient on this event.
RecipientGatewayEvent::getGateways public function Get the gateways for this event.
RecipientGatewayEvent::getGatewaysSorted public function Return gateways ordered by priority from highest to lowest.
RecipientGatewayEvent::getRecipient public function Get the phone number for this event.
RecipientGatewayEvent::removeGateway public function Remove a gateway from this event.
RecipientGatewayEvent::setRecipient public function Set the phone number for this event.
RecipientGatewayEvent::__construct public function Constructs the object.