You are here

public function SendSmsMessageAction::execute in Business Rules 8

Same name and namespace in other branches
  1. 2.x modules/br_sms/src/Plugin/BusinessRulesAction/SendSmsMessageAction.php \Drupal\br_sms\Plugin\BusinessRulesAction\SendSmsMessageAction::execute()

Execute the action.

Parameters

\Drupal\business_rules\ActionInterface $action: The configured action.

\Drupal\business_rules\Events\BusinessRulesEvent $event: The event that has triggered the action.

Return value

array The render array to be showed on debug block.

Overrides BusinessRulesActionPlugin::execute

File

modules/br_sms/src/Plugin/BusinessRulesAction/SendSmsMessageAction.php, line 105

Class

SendSmsMessageAction
Class SendSmsMessageAction.

Namespace

Drupal\br_sms\Plugin\BusinessRulesAction

Code

public function execute(ActionInterface $action, BusinessRulesEvent $event) {
  $variables = $event
    ->getArgument('variables');
  $phone_number = $action
    ->getSettings('phone_number');
  $phone_number = $this
    ->processVariables($phone_number, $variables);
  $message = $action
    ->getSettings('message');
  $message = $this
    ->processVariables($message, $variables);
  $gateway = $action
    ->getSettings('gateway');
  $skip_queue = $action
    ->getSettings('skip_queue');
  $automated = $action
    ->getSettings('automated');
  $send_on = $action
    ->getSettings('send_on');
  $send_on = $this
    ->processVariables($send_on, $variables);
  $message = SmsMessage::create()
    ->addRecipient($phone_number)
    ->setMessage($message)
    ->setAutomated($automated)
    ->setDirection(Direction::OUTGOING);
  $send_date = new DrupalDateTime($send_on);
  if ($send_date instanceof DrupalDateTime) {
    $message
      ->setSendTime($send_date
      ->format('U'));
  }
  $message
    ->setGateway(SmsGateway::load($gateway));
  $result = [
    '#type' => 'markup',
    '#markup' => t('SMS Message could not be sent.'),
  ];
  try {
    $smsProvider = $this->util->container
      ->get('sms.provider');
    if ($skip_queue) {
      $messages = $smsProvider
        ->send($message);
      foreach ($messages as $m) {
        $result_m = $m
          ->getResult();
        if ($result_m
          ->getError()) {
          $status_message = $result_m
            ->getErrorMessage();
          $result = [
            '#type' => 'markup',
            '#markup' => t('A problem occurred while attempting to process SMS message: (code: @code) @message', [
              '@code' => $result_m
                ->getError(),
              '@message' => $status_message,
            ]),
          ];
        }
        elseif (count($result_m
          ->getReports())) {
          $result = [
            '#type' => 'markup',
            '#markup' => t('SMS message was processed, @count delivery reports were generated.', [
              '@count' => count($result_m
                ->getReports()),
            ]),
          ];
        }
        else {
          $result = [
            '#type' => 'markup',
            '#markup' => t('An unknown error occurred while attempting to process SMS message. No result or reports were generated by the gateway.'),
          ];
        }
      }
    }
    else {
      $smsProvider
        ->queue($message);
      $result = [
        '#type' => 'markup',
        '#markup' => t('SMS message added to the outgoing queue.'),
      ];
    }
  } catch (SmsException $e) {
    $result = [
      '#type' => 'markup',
      '#markup' => t('SMS message could not be sent: @error', [
        '@error' => $e
          ->getMessage(),
      ]),
    ];
  }
  return $result;
}