You are here

public function SimpleGateway::send in SMS simple gateway 8

Sends out the sms by hitting the gateway.

Parameters

\Drupal\sms\Message\SmsMessageInterface $sms: The sms to be sent out to the user.

Return value

\Drupal\sms\Message\SmsMessageResultInterface A response object indicating whether the sms was sent or not.

Overrides SmsGatewayPluginInterface::send

File

src/Plugin/SmsGateway/SimpleGateway.php, line 318

Class

SimpleGateway
This plugin handles sending SMSes through most GET & POST based SMS Gateways.

Namespace

Drupal\sms_simplegateway\Plugin\SmsGateway

Code

public function send(SmsMessageInterface $sms) {
  $client = \Drupal::httpClient();
  $result = new SmsMessageResult();
  $report = new SmsDeliveryReport();
  $parameters = array_filter([
    $this->configuration['user_field'] => $this->configuration['user_value'],
    $this->configuration['pass_field'] => $this->configuration['pass_value'],
    $this->configuration['sender_field'] => $this->configuration['sender_value'],
    $this->configuration['number_field'] => $this->configuration['number_prefix'] . $sms
      ->getRecipients()[0],
    $this->configuration['message_field'] => $sms
      ->getMessage(),
  ]);
  $url = NULL;
  $options = [];
  switch ($this->configuration['method']) {
    case 'get':
      $url = $this->configuration['base_url'] . '?' . http_build_query($parameters) . '&' . $this->configuration['extra_params'];
      break;
    case 'post':
      $url = $this->configuration['base_url'];
      switch ($this->configuration['authorization']) {
        case 'basic':
          $options['auth'] = [
            $this->configuration['user_value'],
            $this->configuration['pass_value'],
          ];
          break;
        case 'digest':
          $options['auth'] = [
            $this->configuration['user_value'],
            $this->configuration['pass_value'],
            'digest',
          ];
          break;
        case 'ntlm':
          $options['auth'] = [
            $this->configuration['user_value'],
            $this->configuration['pass_value'],
            'ntlm',
          ];
          break;
      }
      switch ($this->configuration['content_type']) {
        case 'plain':
          $options['headers'] = [
            'Content-Type' => 'application/x-www-form-urlencoded',
          ];
          $options['body'] = http_build_query($parameters) . '&' . $this->configuration['extra_params'];
          break;
        case 'json':
          $options['headers'] = [
            'Content-Type' => 'application/json',
          ];
          $options['body'] = json_encode($parameters);
          break;
          break;
      }
  }
  try {
    $response = $client
      ->request($this->configuration['method'], $url, $options);
    if ($response
      ->getStatusCode() >= 200 and $response
      ->getStatusCode() <= 299) {
      return $result
        ->addReport($report
        ->setRecipient($sms
        ->getRecipients()[0])
        ->setStatus(SmsMessageReportStatus::QUEUED));
    }
    else {
      return $result
        ->addReport($report
        ->setRecipient($sms
        ->getRecipients()[0])
        ->setStatus(SmsMessageResultStatus::ERROR))
        ->setErrorMessage($response
        ->getBody());
    }
  } catch (HttpException $e) {
    return $result
      ->setError(SmsMessageResultStatus::ERROR)
      ->setErrorMessage($e
      ->getMessage());
  }
}