You are here

public function EmitController::emit in Radioactivity 8.3

Same name and namespace in other branches
  1. 8.2 src/Controller/EmitController.php \Drupal\radioactivity\Controller\EmitController::emit()
  2. 4.0.x src/Controller/EmitController.php \Drupal\radioactivity\Controller\EmitController::emit()

Callback for /radioactivity/emit.

Parameters

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

Return value

\Symfony\Component\HttpFoundation\JsonResponse Response object.

1 string reference to 'EmitController::emit'
radioactivity.routing.yml in ./radioactivity.routing.yml
radioactivity.routing.yml

File

src/Controller/EmitController.php, line 56

Class

EmitController
Controller for radioactivity emit routes.

Namespace

Drupal\radioactivity\Controller

Code

public function emit(Request $request) {
  $postData = $request
    ->getContent();
  if (empty($postData)) {
    return new JsonResponse([
      'status' => 'error',
      'message' => 'Empty request.',
    ]);
  }
  $count = 0;
  $incidents = Json::decode($postData);
  foreach ($incidents as $data) {
    $incident = Incident::createFromPostData($data);
    if (!$incident
      ->isValid()) {
      return $this
        ->buildJsonStatusResponse('error', "invalid incident ({$count}).");
    }
    $this->incidentStorage
      ->addIncident($incident);
    $count++;
  }
  return $this
    ->buildJsonStatusResponse('ok', "{$count} incidents added.");
}