You are here

public function Memory::parseDeliveryReports in SMS Framework 2.1.x

Same name and namespace in other branches
  1. 8 tests/modules/sms_test_gateway/src/Plugin/SmsGateway/Memory.php \Drupal\sms_test_gateway\Plugin\SmsGateway\Memory::parseDeliveryReports()
  2. 2.x tests/modules/sms_test_gateway/src/Plugin/SmsGateway/Memory.php \Drupal\sms_test_gateway\Plugin\SmsGateway\Memory::parseDeliveryReports()

Parses incoming delivery reports and returns the created delivery reports.

The request contains delivery reports pushed to the site in a format supplied by the gateway API. This method transforms the raw request into delivery report objects usable by SMS Framework.

Parameters

\Symfony\Component\HttpFoundation\Request $request: Request object containing the unprocessed delivery reports.

\Symfony\Component\HttpFoundation\Response $response: HTTP response to return to the server pushing the raw delivery reports.

Return value

\Drupal\sms\Message\SmsDeliveryReportInterface[] An array of delivery reports created from the request.

Overrides SmsGatewayPluginBase::parseDeliveryReports

File

tests/modules/sms_test_gateway/src/Plugin/SmsGateway/Memory.php, line 111

Class

Memory
Defines a gateway storing transmitted SMS in memory.

Namespace

Drupal\sms_test_gateway\Plugin\SmsGateway

Code

public function parseDeliveryReports(Request $request, Response $response) {
  $gateway_id = $this->configuration['gateway_id'];
  $memory_reports = \Drupal::state()
    ->get('sms_test_gateway.memory.report', []);
  $data = Json::decode($request->request
    ->get('delivery_report'));
  $return = [];
  foreach ($data['reports'] as $report) {
    $message_id = $report['message_id'];
    $new_report = (new SmsDeliveryReport())
      ->setRecipient($report['recipient'])
      ->setMessageId($message_id)
      ->setStatus($report['status'])
      ->setStatusMessage($report['status_message'])
      ->setStatusTime($report['status_time']);

    // Backfill the specific values.
    if ($report['status'] === SmsMessageReportStatus::QUEUED) {
      $new_report
        ->setTimeQueued($report['status_time']);
    }
    if ($report['status'] === SmsMessageReportStatus::DELIVERED) {
      $new_report
        ->setTimeDelivered($report['status_time']);
    }

    // Set separately since this method should not have meaningful keys.
    $return[] = $new_report;

    // Reports in state must be keyed by message ID.
    $memory_reports[$gateway_id][$message_id] = $new_report;
  }
  \Drupal::state()
    ->set('sms_test_gateway.memory.report', $memory_reports);

  // Set the response.
  $response
    ->setContent('custom response content');
  return $return;
}