You are here

class EmitController in Radioactivity 4.0.x

Same name and namespace in other branches
  1. 8.3 src/Controller/EmitController.php \Drupal\radioactivity\Controller\EmitController
  2. 8.2 src/Controller/EmitController.php \Drupal\radioactivity\Controller\EmitController

Controller for radioactivity emit routes.

Handles incoming emits, verifies the data and stores it in incident storage. Returns a JSON formatted response with a status.

Hierarchy

Expanded class hierarchy of EmitController

1 file declares its use of EmitController
EmitControllerTest.php in tests/src/Unit/EmitControllerTest.php

File

src/Controller/EmitController.php, line 19

Namespace

Drupal\radioactivity\Controller
View source
class EmitController implements ContainerInjectionInterface {

  /**
   * The incident storage.
   *
   * @var \Drupal\radioactivity\IncidentStorageInterface
   */
  protected $incidentStorage;

  /**
   * Constructs an EmitController object.
   *
   * @param \Drupal\radioactivity\StorageFactory $storageFactory
   *   Radioactivity storage factory.
   */
  public function __construct(StorageFactory $storageFactory) {
    $this->incidentStorage = $storageFactory
      ->get('default');
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('radioactivity.storage'));
  }

  /**
   * Callback for /radioactivity/emit.
   *
   * @param \Symfony\Component\HttpFoundation\Request $request
   *   The request object.
   *
   * @return \Symfony\Component\HttpFoundation\JsonResponse
   *   Response object.
   */
  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");
  }

  /**
   * Creates a JSON status message response.
   *
   * @param string $status
   *   The status content of the response.
   * @param string $message
   *   The message content of the response.
   *
   * @return \Symfony\Component\HttpFoundation\JsonResponse
   *   The response.
   */
  protected function buildJsonStatusResponse($status, $message) {
    return new JsonResponse([
      'status' => $status,
      'message' => $message,
    ]);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
EmitController::$incidentStorage protected property The incident storage.
EmitController::buildJsonStatusResponse protected function Creates a JSON status message response.
EmitController::create public static function Instantiates a new instance of this class. Overrides ContainerInjectionInterface::create
EmitController::emit public function Callback for /radioactivity/emit.
EmitController::__construct public function Constructs an EmitController object.