You are here

class Command in Twilio 8

Service class for Twilio API commands.

Hierarchy

  • class \Drupal\twilio\Services\Command

Expanded class hierarchy of Command

1 file declares its use of Command
TwilioAdminTestForm.php in src/Form/TwilioAdminTestForm.php
1 string reference to 'Command'
twilio.services.yml in ./twilio.services.yml
twilio.services.yml
1 service uses Command
twilio.command in ./twilio.services.yml
Drupal\twilio\Services\Command

File

src/Services/Command.php, line 12

Namespace

Drupal\twilio\Services
View source
class Command {
  private $sid;
  private $token;
  private $number;

  /**
   * Initialize properties.
   */
  public function __construct() {
    $this->sid = $this
      ->getSid();
    $this->token = $this
      ->getToken();
    $this->number = $this
      ->getNumber();
  }

  /**
   * Get the Twilio Account SID.
   *
   * @return string
   *   The configured Twilio Account SID.
   */
  public function getSid() {
    if (empty($this->sid)) {
      $this->sid = \Drupal::config('twilio.settings')
        ->get('account');
    }
    return $this->sid;
  }

  /**
   * Get the Twilio Auth Token.
   *
   * @return string
   *   The configured Twilio Auth Token.
   */
  public function getToken() {
    if (empty($this->token)) {
      $this->token = \Drupal::config('twilio.settings')
        ->get('token');
    }
    return $this->token;
  }

  /**
   * Get the Twilio Number.
   *
   * @return string
   *   The configured Twilio Number.
   */
  public function getNumber() {
    if (empty($this->number)) {
      $this->number = \Drupal::config('twilio.settings')
        ->get('number');
    }
    return $this->number;
  }

  /**
   * Send an SMS message.
   *
   * @param string $number
   *   The number to send the message to.
   * @param string|array $message
   *   Message text or an array:
   *   [
   *     from => number
   *     body => message string
   *     mediaUrl => absolute URL
   *   ].
   */
  public function messageSend(string $number, $message) {
    if (is_string($message)) {
      $message = [
        'from' => $this->number,
        'body' => $message,
      ];
    }
    $message['from'] = !empty($message['from']) ? $message['from'] : $this->number;
    if (empty($message['body'])) {
      throw new TwilioException("Message requires a body.");
    }
    if (!empty($message['mediaUrl']) && !UrlHelper::isValid($message['mediaUrl'], TRUE)) {
      throw new TwilioException("Media URL must be a valid, absolute URL.");
    }
    $client = new Client($this->sid, $this->token);
    $client->messages
      ->create($number, $message);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Command::$number private property
Command::$sid private property
Command::$token private property
Command::getNumber public function Get the Twilio Number.
Command::getSid public function Get the Twilio Account SID.
Command::getToken public function Get the Twilio Auth Token.
Command::messageSend public function Send an SMS message.
Command::__construct public function Initialize properties.