class RestProcessor in Radioactivity 8.3
Same name and namespace in other branches
- 4.0.x src/RestProcessor.php \Drupal\radioactivity\RestProcessor
The worker class for the Radioactivity rest endpoint.
The rest endpoint is a thin wrapper around the methods of this worker class. The rest functionality is split in two files to allow unit testing. The return values of the public functions are specific for the use in the endpoint to allow maximum test coverage.
@package Drupal\radioactivity
Hierarchy
- class \Drupal\radioactivity\RestProcessor
Expanded class hierarchy of RestProcessor
See also
endpoints/file/rest.php
2 files declare their use of RestProcessor
- rest.php in endpoints/
file/ rest.php - Rest endpoint for handling radioactivity incidents for radioactivity module.
- RestProcessorTest.php in tests/
src/ Unit/ RestProcessorTest.php
File
- src/
RestProcessor.php, line 17
Namespace
Drupal\radioactivityView source
class RestProcessor {
/**
* The path and file name where the payload data is stored.
*
* @var string
*/
private $payloadFile;
/**
* Constructor.
*
* @param array $config
* Processor configuration to override the default.
*/
public function __construct(array $config = []) {
$config += [
'payload_file' => sys_get_temp_dir() . '/radioactivity-payload.json',
];
$this->payloadFile = $config['payload_file'];
}
/**
* Processes incoming radioactivity incident data.
*
* @param string $data
* Json formatted incident data. The format must match the return value
* of \Drupal\radioactivity\Incident::toJson.
*
* @return string
* Json formatted result status message.
*/
public function processData($data) {
if (!$this
->verifyData($data)) {
return $this
->restStatus('error', 'Invalid json.');
}
$fh = fopen($this->payloadFile, 'a+');
fwrite($fh, $data . ',' . PHP_EOL);
fclose($fh);
return $this
->restStatus('ok', 'Inserted.');
}
/**
* Clears the rest data storage.
*
* @return string
* Json formatted result status message.
*/
public function clearData() {
if (file_exists($this->payloadFile)) {
unlink($this->payloadFile);
}
return $this
->restStatus('ok', 'Cleared.');
}
/**
* Returns the stored incident data.
*
* @return string
* Json formatted incident data.
*/
public function getData() {
if (file_exists($this->payloadFile)) {
$fh = fopen($this->payloadFile, 'r');
$data = fread($fh, filesize($this->payloadFile));
fclose($fh);
return '[' . rtrim($data, ',' . PHP_EOL) . ']';
}
else {
return '[]';
}
}
/**
* Returns a generic error message.
*
* @return string
* Json formatted error status message.
*/
public function error() {
return $this
->restStatus('error', 'Nothing to do.');
}
/**
* Simple verification of the incident data.
*
* @param string $data
* Json encoded emission data received in $_POST.
*
* @return bool
* True when $data is a valid object, false if not.
*/
private function verifyData($data) {
$incidents = json_decode($data, TRUE);
$keys = [
'fn',
'et',
'id',
'e',
'h',
];
foreach ($incidents as $incident) {
if (count($keys) !== count($incident) || count(array_intersect_key(array_flip($keys), $incident)) !== count($keys)) {
return FALSE;
}
}
return TRUE;
}
/**
* Returns status and message.
*
* @param string $status
* The status code (ok, error).
* @param string $message
* The message describing the status.
*/
private function restStatus($status, $message) {
return json_encode([
'status' => $status,
'message' => $message,
]);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
RestProcessor:: |
private | property | The path and file name where the payload data is stored. | |
RestProcessor:: |
public | function | Clears the rest data storage. | |
RestProcessor:: |
public | function | Returns a generic error message. | |
RestProcessor:: |
public | function | Returns the stored incident data. | |
RestProcessor:: |
public | function | Processes incoming radioactivity incident data. | |
RestProcessor:: |
private | function | Returns status and message. | |
RestProcessor:: |
private | function | Simple verification of the incident data. | |
RestProcessor:: |
public | function | Constructor. |