You are here

public function Incoming::processIncoming in SMS Framework 8

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

Process an incoming message POST request.

This callback expects a 'messages' POST value containing JSON.

Parameters

\Symfony\Component\HttpFoundation\Request $request: The request object.

\Drupal\sms\Entity\SmsGatewayInterface $sms_gateway: The gateway instance.

Return value

\Drupal\sms\SmsProcessingResponse A SMS processing response task.

File

tests/modules/sms_test_gateway/src/Plugin/SmsGateway/Incoming.php, line 48

Class

Incoming
Defines a gateway supporting incoming route.

Namespace

Drupal\sms_test_gateway\Plugin\SmsGateway

Code

public function processIncoming(Request $request, SmsGatewayInterface $sms_gateway) {
  $json = Json::decode($request
    ->getContent());
  $raw_messages = $json['messages'];

  // JSON property to SmsMessage method setters mapping.
  $sms_properties = [
    'sender_number' => 'setSenderNumber',
    'message' => 'setMessage',
    'recipients' => 'addRecipients',
  ];
  $messages = [];
  foreach ($raw_messages as $raw_message) {
    $result = new SmsMessageResult();
    foreach ($raw_message['recipients'] as $recipient) {
      $report = (new SmsDeliveryReport())
        ->setRecipient($recipient);
      $result
        ->addReport($report);
    }
    $message = (new SmsMessage())
      ->setDirection(Direction::INCOMING)
      ->setGateway($sms_gateway)
      ->setResult($result);
    foreach ($sms_properties as $property => $method) {
      if (array_key_exists($property, $raw_message)) {
        $value = $raw_message[$property];
        call_user_func_array([
          $message,
          $method,
        ], [
          $value,
        ]);
      }
    }
    $messages[] = $message;
  }
  $response = new Response('', 204);
  $task = (new SmsProcessingResponse())
    ->setResponse($response)
    ->setMessages($messages);
  return $task;
}