class EmitController in Radioactivity 8.3
Same name and namespace in other branches
- 8.2 src/Controller/EmitController.php \Drupal\radioactivity\Controller\EmitController
- 4.0.x 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
- class \Drupal\radioactivity\Controller\EmitController implements ContainerInjectionInterface
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\ControllerView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
EmitController:: |
protected | property | The incident storage. | |
EmitController:: |
protected | function | Creates a JSON status message response. | |
EmitController:: |
public static | function |
Instantiates a new instance of this class. Overrides ContainerInjectionInterface:: |
|
EmitController:: |
public | function | Callback for /radioactivity/emit. | |
EmitController:: |
public | function | Constructs an EmitController object. |