RestIncidentStorage.php in Radioactivity 8.3
File
src/RestIncidentStorage.php
View source
<?php
namespace Drupal\radioactivity;
use Drupal\Component\Serialization\Json;
class RestIncidentStorage implements RestIncidentStorageInterface {
protected $endpoint = NULL;
public function addIncident(IncidentInterface $incident) {
throw new \Exception("The Radioactivity rest endpoint expects incidents to be added somewhere else.");
}
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;
}
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 ?: [
[],
];
}
public function clearIncidents() {
$this
->clearIncidentStorage();
}
public function injectSettings(array &$page) {
$page['#attached']['drupalSettings']['radioactivity']['type'] = 'rest';
$page['#attached']['drupalSettings']['radioactivity']['endpoint'] = $this
->getEndpoint();
}
public function setEndpoint($endpoint = NULL) {
$this->endpoint = $endpoint;
}
protected function getEndpoint() {
if (is_null($this->endpoint)) {
$this->endpoint = $this
->getDefaultEndpoint();
}
return $this->endpoint;
}
protected function getDefaultEndpoint() {
global $base_url;
return $base_url . '/' . drupal_get_path('module', 'radioactivity') . '/endpoints/file/rest.php';
}
protected function getIncidentsFromStorage() {
return Json::decode(file_get_contents("{$this->getEndpoint()}?get"));
}
protected function clearIncidentStorage() {
file_get_contents("{$this->getEndpoint()}?clear");
}
}