class RestIncidentStorage in Radioactivity 4.0.x
Same name and namespace in other branches
- 8.3 src/RestIncidentStorage.php \Drupal\radioactivity\RestIncidentStorage
Defines a REST incident storage.
Hierarchy
- class \Drupal\radioactivity\RestIncidentStorage implements RestIncidentStorageInterface
Expanded class hierarchy of RestIncidentStorage
2 files declare their use of RestIncidentStorage
- RestIncidentStorageTest.php in tests/
src/ Unit/ RestIncidentStorageTest.php - StorageFactoryTest.php in tests/
src/ Unit/ StorageFactoryTest.php
1 string reference to 'RestIncidentStorage'
1 service uses RestIncidentStorage
File
- src/
RestIncidentStorage.php, line 10
Namespace
Drupal\radioactivityView source
class RestIncidentStorage implements RestIncidentStorageInterface {
/**
* REST endpoint URL for incidents.
*
* @var string
*/
protected $endpoint = NULL;
/**
* {@inheritdoc}
*/
public function addIncident(IncidentInterface $incident) {
throw new \Exception("The Radioactivity rest endpoint expects incidents to be added somewhere else.");
}
/**
* {@inheritdoc}
*/
public function getIncidents() {
$data = $this
->getIncidentsFromStorage();
$result = [];
foreach ($data as $rows) {
foreach ($rows as $row) {
$incident = Incident::createFromPostData($row);
if ($incident
->isValid()) {
$result[] = $incident;
}
}
}
return $result;
}
/**
* {@inheritdoc}
*/
public function getIncidentsByType($entity_type = '') {
$incidents = [];
$stored_incidents = $this
->getIncidents();
foreach ($stored_incidents as $incident) {
$incidents[$incident
->getEntityTypeId()][$incident
->getEntityId()][] = $incident;
}
if (isset($incidents[$entity_type])) {
return [
$entity_type => $incidents[$entity_type],
];
}
return $incidents ?: [
[],
];
}
/**
* {@inheritdoc}
*/
public function clearIncidents() {
$this
->clearIncidentStorage();
}
/**
* {@inheritdoc}
*/
public function injectSettings(array &$page) {
$page['#attached']['drupalSettings']['radioactivity']['type'] = 'rest';
$page['#attached']['drupalSettings']['radioactivity']['endpoint'] = $this
->getEndpoint();
}
/**
* {@inheritdoc}
*/
public function setEndpoint($endpoint = NULL) {
$this->endpoint = $endpoint;
}
/**
* Returns the endpoint URL.
*
* @return string
* The endpoint URL.
*/
protected function getEndpoint() {
if (is_null($this->endpoint)) {
$this->endpoint = $this
->getDefaultEndpoint();
}
return $this->endpoint;
}
/**
* Returns the default storage endpoint.
*
* @return string
* The storage endpoint.
*/
protected function getDefaultEndpoint() {
global $base_url;
return $base_url . '/' . drupal_get_path('module', 'radioactivity') . '/endpoints/file/rest.php';
}
/**
* Returns all incidents from the storage.
*
* @return array
* The incidents.
*/
protected function getIncidentsFromStorage() {
return Json::decode(file_get_contents("{$this->getEndpoint()}?get"));
}
/**
* Deletes all incidents from the storage.
*/
protected function clearIncidentStorage() {
file_get_contents("{$this->getEndpoint()}?clear");
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
IncidentStorageInterface:: |
constant | The key to identify the radioactivity incident storage. | ||
RestIncidentStorage:: |
protected | property | REST endpoint URL for incidents. | |
RestIncidentStorage:: |
public | function |
Adds an incident to the storage. Overrides IncidentStorageInterface:: |
|
RestIncidentStorage:: |
public | function |
Clears the incident storage. Overrides IncidentStorageInterface:: |
|
RestIncidentStorage:: |
protected | function | Deletes all incidents from the storage. | |
RestIncidentStorage:: |
protected | function | Returns the default storage endpoint. | |
RestIncidentStorage:: |
protected | function | Returns the endpoint URL. | |
RestIncidentStorage:: |
public | function |
Gets all incidents from the storage. Overrides IncidentStorageInterface:: |
|
RestIncidentStorage:: |
public | function |
Gets all incidents from the storage per entity type. Overrides IncidentStorageInterface:: |
|
RestIncidentStorage:: |
protected | function | Returns all incidents from the storage. | |
RestIncidentStorage:: |
public | function |
Add endpoint settings to the page. Overrides IncidentStorageInterface:: |
|
RestIncidentStorage:: |
public | function |
Sets the REST endpoint URL. Overrides RestIncidentStorageInterface:: |