DefaultIncidentStorage.php in Radioactivity 4.0.x
File
src/DefaultIncidentStorage.php
View source
<?php
namespace Drupal\radioactivity;
use Drupal\Core\State\StateInterface;
class DefaultIncidentStorage implements IncidentStorageInterface {
protected $state;
public function __construct(StateInterface $state) {
$this->state = $state;
}
public function addIncident(IncidentInterface $incident) {
$incidents = $this->state
->get(self::STORAGE_KEY, []);
$incidents[] = $incident;
$this->state
->set(self::STORAGE_KEY, $incidents);
}
public function getIncidents() {
return $this->state
->get(self::STORAGE_KEY, []);
}
public function getIncidentsByType($entity_type = '') {
$incidents = [];
$stored_incidents = $this->state
->get(self::STORAGE_KEY, []);
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->state
->set(self::STORAGE_KEY, []);
}
public function injectSettings(array &$page) {
global $base_url;
$page['#attached']['drupalSettings']['radioactivity']['type'] = 'default';
$page['#attached']['drupalSettings']['radioactivity']['endpoint'] = $base_url . '/radioactivity/emit';
}
}